-1

In Azure DevOps, my PR policy is set as "Semi-Linear", which means when you merge a pull request, it gets rebased first and then merged.

This makes it really difficult to clean up local branches because my normal git branch --merged origin/master doesn't support this scenario.

How can I reliably determine, in bulk, which local branches have been "effectively merged" so that I may delete them?

Note: I have my push.default set to current, and I always keep my remote tracking branch set to origin/master. So any solution that relies on checking upstream remote tracking branches for each local branch will not work (although, I do push my branches to the remote using the same name; we just can't rely on the remote tracking branch for that). I wanted to mention this detail just in case any solution would leverage remote tracking branches.

I found a similar SO question, Git: How do I list local branches that are tracking remote branches that no longer exist?, but it has no acceptable answers as far as I'm concerned.

Machavity
  • 30,841
  • 27
  • 92
  • 100
void.pointer
  • 24,859
  • 31
  • 132
  • 243
  • 1
    Can you identify the scenario where you can't trust that a remote that no longer exists is correctly identifying your local branches that can be safely deleted? (i.e. what is it about the answers to the other question that doesn't work in your scenario? Specifically, I believe the answer by Anton Styagun is correct, though it hasn't been marked as such yet. If that question had a good answer, would it also answer yours?) – TTT Jul 31 '20 at 16:11

1 Answers1

1

one way I can think of is to attempt (dry-run) a rebase of the topic branch onto origin/master - if it creates no new commits then the topic branch can be safely removed

this is because by default rebase does not create empty commits

one other way (maybe cleaner) is to:

git switch -c temp-topic-branch topic-branch
git merge origin/master
git reset --mixed origin/master
git status -s

if git status -s results in empty output - the topic branch can be safely removed

Nitsan Avni
  • 753
  • 6
  • 6
  • 1
    I had this exact thought. I don't want to guess how long it would take (even if automated), but it should work... – TTT Jul 31 '20 at 16:39
  • One of my requirements was that I be able to do this in bulk. Doing a rebase of every individual branch *could* be automated; but it would be non-trivial and slow. I imagine there ought to be a way to use the lower level plumbing commands to achieve this better, especially in a way that only functions using the index if possible. – void.pointer Oct 01 '20 at 16:32