How to list branches that contain a given commit?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
When working with Git, a distributed version control system, it’s often necessary to determine which branches have a particular commit. This can be critical for debugging, code reviews, or understanding the flow and history of your codebase. In this article, we'll explore how to list branches that contain a specific commit.
Terminology and Concepts
Before diving into the commands and examples, it's essential to understand some key concepts:
- Commit: A snapshot of your changes in the version history.
- Branch: A parallel version of the repository, allowing for multiple lines of development.
- Ancestor: In Git, a branch is said to contain a commit if the commit is an ancestor of the branch’s head.
Using git branch --contains
The simplest method to list branches containing a given commit is by using the git branch --contains <commit> command. This command checks all branches in your repository and lists those that include the specific commit in their history.
Syntax
Here, <commit> is the hash identifier for the commit you are interested in. If you're unsure of a commit's hash, you can use git log to find it. A commit hash typically looks like a1d2e3f.
Example
Suppose we have a commit with the hash b7d4c3f, and we want to check which branches contain this commit.
The output will list all branches with this commit in their history.
Cross-checking with git log
To further ensure that your understanding aligns with the branch history, you can use git log. With git log, you can manually inspect commit histories to confirm the presence of a commit.
By examining the output, you can verify if b7d4c3f is present in the commit history.
Considering Remote Branches
By default, git branch --contains only checks local branches. To include remote branches, you should use the -r option for remote branches or -a for both local and remote branches:
Syntax for Remote Branches
Syntax for All Branches
Example of Checking All Branches
This command will result in a list of both local and remote branches containing the specified commit.
Performance Considerations
Running these commands in repositories with a high number of branches or commits may be resource-intensive and could take some time depending on the machine and the size of the repository.
Tables and Summary
Let's summarize the key commands and options in the table below:
| Command | Description |
git branch --contains <commit> | Lists all local branches containing a specific commit. |
git branch -r --contains <commit> | Lists remote branches containing a specific commit. |
git branch -a --contains <commit> | Lists all branches, both local and remote, containing a specific commit. |
git log <branch_name> | Verifies the presence of a commit in a branch's history. |
Conclusion
Identifying which branches contain a specific commit in a Git repository is a common task for developers. With the commands and techniques outlined above, you can efficiently and accurately determine the branches that include any particular commit. This understanding aids in better management and tracking of code changes across different development lines.

