How to List Git Branches that Contain a Given Commit

Sometimes you want to know whether your commit is on a particular branch or not. Git allows listing all the branches that contain a particular commit. Here, we will give you a solid plan on how to do it.

Listing Git branches containing the given commit

To list the branches containing the given commit, you should run the git branch command with the --contains option as follows:

git branch --contains <sha1-commit-hash>
If you want to track the remote-tracking branches, you should add the -r option. For tracking both the local and remote-tracking branches, use the-a option.

Checking whether the branch is merged

Imagine you want to know whether your current branch is merged with master or not.

Checking whether the commit exists

To check whether the branch is merged with the master or not, first, you should take the hash of your last commit as follows:

git log -1 <current-branch>

The result will look as follows:

commit d590f2ac0635ec0053c4a7377bd929943d475297
Author: Bob Quaranto <[email protected]>
Date:   Wed Apr 1 20:38:59 2009 -0400

Then, to check whether the master branch contains it or not, run the command below:

git branch --contains d590f2ac0635ec0053c4a7377bd929943d475297
  * current-branch

If the commit is on a remote-tracking branch, add the -a option:

git branch -a --contains d590f2ac0635ec0053c4a7377bd929943d475297

Checking whether the changes exist

For comparing the changeset rather than the commit hash, you can use git diff to find out if a commit you made locally has been applied <upstream> under a different commit hash:

git diff <current-branch> master


Generally, branching is available in almost all version control systems. You can use the git branch command for creating, deleting, or listing branches in Git. But it would be best if you consider that it won’t allow switching between branches or putting a forked history back together. Each new branch created by you summarizes the changes in case you wish to fix bugs or attach new features.