Home > Software design >  how to list all 'active' branches in git containing unmerged commits
how to list all 'active' branches in git containing unmerged commits

Time:01-12

I'm struggling to make sense of the history of a couple of very large repositories that have hundreds of (old) branches which never have been deleted (even though work on most of these branches is 'done').

I'm trying to find a way to generate a list of branches that

  • contain commits after the branch was created ('not empty')
  • have not been merged into another branch

If my assumption is correct, this should return a list of branches that contain unmerged/active code - everything else is safe to delete.

A nice gimmick would be to to visualize this via git log --graph - only displaying the 'current working tree', going back only to the first commit that's present in all of the 'currently active branches'.

Any suggestions/help is highly appreciated!

CodePudding user response:

TL;DR: git branch --no-merged HEAD is probably the answer you want. You may want to add -r or -a, or use something other than HEAD. You might want to run this (adjusted) command numerous times, once for each branch name (although in that case there are some ways to do this more efficiently, at this possible cost).

Long

It's important to realize here that Git does not actually merge branches. Or more precisely, we have to define what we mean by branch first (see What exactly do we mean by "branch"?); depending on which definition we use, Git doesn't have branches, or does not merge branches, or does merge branches but then they sometimes un-merge later; or there are other possibilities depending on what you mean by "branch".

  •  Tags:  
  • Related