Search code examples
gitbranchdvcs

Show branches that do not contain commit


git branch -a --contains <hash> gives me all those branches containing hash. what I want is git branch -a --no-contains <hash>. Unfortunately, there doesn't seem to be a command to accomplish this, so I'm thinking the solution is something like:

git branch -a | grep -v output of(git branch -a --contains) but my bash isn't up to the task.

Show all branches that commit A is on and commit B is not on? would seem to apply, but the approach seems more complicated than necessary.

What is the best/most simple approach to accomplish the above?


Solution

  • grep has a -F option which matches fixed strings. Would be useful for what you're doing.

    git branch -a | grep -vF "$(git branch -a --contains <hash>)"
    

    Unfortunately, -F will filter out branches names that have a partial match. As suggested by antak, we can use comm instead for a more reliable diff.

    git branch -a | sort | comm -3 - <(git branch -a --contains <hash> | sort)