Complete Guide to Finding Branches Containing a Specific Commit in Git

Nov 20, 2025 · Programming · 14 views · 7.8

Keywords: Git | branch query | version control | commit containment | remote branches

Abstract: This article provides a comprehensive guide on how to accurately identify branches that contain a specific commit in the Git version control system. Using the --contains option with git branch command, users can efficiently query local branches, remote branches, or all branches for commit inclusion. The article delves into command usage, parameter meanings, and practical applications, including handling remote tracking branches and special refspec configurations, while comparing differences with git cherry for equivalent commit detection.

Core Commands for Git Branch Query

In Git version control systems, identifying which branches contain a specific commit is a common requirement. The git branch command provides the --contains option to fulfill this need. This option lists all branches that contain the specified commit, defaulting to HEAD if no commit is specified.

Basic Usage and Examples

To query whether local branches contain a particular commit, use the following command:

git branch --contains <commit>

For example, to find all local branches containing commit d590f2ac0635ec0053c4a7377bd929943d475297:

$ git branch --contains d590f2
  tests
* master

The output shows that both the tests branch and master branch contain the commit, with * indicating the currently checked-out branch.

Querying Remote Branches

For remote tracking branch queries, add the -r option:

git branch -r --contains <commit>

This lists all remote tracking branches that contain the specified commit, meaning local references that have a direct relationship with remote branches.

Complete Branch List Query

To query both local and remote branches simultaneously, use the -a option:

git branch -a --contains <commit>

This command returns all branches containing the specified commit, whether they are local branches or remote tracking branches.

Handling Special Refspec Configurations

By default, git branch -r --contains only queries standard remote branch references. If the project uses special refspec configurations, such as Gerrit pull request references or custom namespaces, you need to configure the appropriate refspec and refetch first:

git config --add remote.origin.fetch "+refs/pull/*/head:refs/remotes/origin/pr/*"
git fetch
git branch -r --contains <commit>

This ensures that references from these special namespaces are included in the query.

Comparison with git cherry Command

It's important to note that git branch --contains only finds branches containing the exact same commit ID. To find branches containing equivalent changesets (e.g., commits applied via cherry-pick operations), use the git cherry command:

git cherry <upstream> <head>

git cherry compares changesets rather than commit IDs, enabling it to identify identical changes applied with different commit IDs.

Practical Application Scenarios

Querying branch containment relationships is valuable in various scenarios:

Best Practice Recommendations

When using these commands, it's recommended to:

Copyright Notice: All rights in this article are reserved by the operators of DevGex. Reasonable sharing and citation are welcome; any reproduction, excerpting, or re-publication without prior permission is prohibited.