Search code examples
gitbranching-and-merging

How to list old branches that are already fully included in HEAD


We have a big git repo with lots of history and remote origin contains lots of branches that are already included in master. Is there any simple way to list local and remote branches that are already included in HEAD?

I know that How to list branches that contain a given commit? helps if I want to do reverse, checking which branches already contain the commit HEAD.


Solution

  • A quick cheat sheet:

    git branch -a --merged HEAD
    

    will list all branches (local and remote) that are already fully included in HEAD. This can be used to list old branch labels that may be removed. Note that it will also list the currently checked out branch (marked with asterisk) so you probably shouldn't blindly remove all those branches. (This is the exact answer to original question, see below for additional info.)


    git branch -a --contains HEAD
    

    will do the reverse. It will list all branches that already contain the commit HEAD meaning those branches are logically forward in the DAG. This can be used to list branches that include a given commit (e.g. bugfix).


    git branch -a --no-merged HEAD
    

    will list all branches (local and remote) that are not yet included in HEAD. This can be used to list branches that probably contain code that's still missing from HEAD.


    git branch -a --no-contains HEAD
    

    will list all branches that do not yet include the commit HEAD. This can be used to list branches that are missing a given commit (e.g. bugfix).


    (If you're interested in local branches only, remove the flag -a the example commands. If you're only interested in remote branches, replace flag -a with -r. Flag -a means all branches, -r means remote branches.)