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:
- Verifying whether patches have been applied to specific branches
- Checking if important feature commits have been merged into release branches
- Determining the propagation scope of code changes
- Troubleshooting code synchronization issues between branches
Best Practice Recommendations
When using these commands, it's recommended to:
- Always use full commit hash values to ensure accuracy
- Regularly update remote references (
git fetch) to obtain the latest branch information - Verify query results in combination with
git logcommand - Pay attention to command output format when used in automation scripts