git
branches
commit
version control
git command

How to list branches that contain a given commit?

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

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

bash
git branch --contains <commit>

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.

bash
git branch --contains b7d4c3f

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.

bash
git log <branch_name>

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

bash
git branch -r --contains <commit>

Syntax for All Branches

bash
git branch -a --contains <commit>

Example of Checking All Branches

bash
git branch -a --contains b7d4c3f

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:

CommandDescription
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.


Related reading
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

All Rights Reserved.