How to List Git Branches that Contain a Given Commit
Here, you can find a short tutorial on how to list branches that contain a given commit in Git. Find the answer to one of the most common questions in Git.
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 <kbd class="highlighted">--contains</kbd> option as follows:
git contain branches
git branch --contains <sha1-commit-hash>If you want to track the remote-tracking branches, you should add the <kbd class="highlighted">-r</kbd> option. For tracking both the local and remote-tracking branches, use the <kbd class="highlighted">-a</kbd> 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
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 -0400Then, to check whether the master branch contains it or not, run the command below:
git contain branches
git branch --contains d590f2ac0635ec0053c4a7377bd929943d475297
* current-branch
masterIf the commit is on a remote-tracking branch, add the -a option:
git contains remote tracking branch add the -a
git branch -a --contains d590f2ac0635ec0053c4a7377bd929943d475297Checking whether the changes are merged
To verify if your current branch has been merged into master, you can use the --merged option:
git branch merged
git branch --merged masterThis command lists all branches whose tips are reachable from master, confirming that their changes have been integrated.
Branching
Generally, branching is available in almost all version control systems. You can use the <kbd class="highlighted">git branch</kbd> command for creating, deleting, or listing branches in Git. However, note that this command does not allow switching between branches or merging forked histories. Each new branch you create captures the current state of your changes, which is useful for fixing bugs or developing new features.