Tip: Find branches containing a specific Git commit

Git, Branch, Commit · May 25, 2023

Commits are the building blocks of Git, used to track changes to a repository. They can be used to identify specific points in a repository's history, and can be referenced by their commit hash. But apart from finding a commit, how can you find all the branches containing it? This sort of information can be useful when you want to find out which branches contain a specific bugfix or feature.

As usual, Git has a simple solution to this problem. Using git branch with the --contains flag will print all the branches containing a specific commit.

# Syntax: git branch --contains <commit>

git branch --contains 3050fc0
# development
# network-fixes

Similarly, you can look for branches that don't contain a specific commit by using the --no-contains flag.

# Syntax: git branch --no-contains <commit>

git branch --no-contains 3050fc0
# master
# adapter-feature

Written by Angelos Chalaris

I'm Angelos Chalaris, a JavaScript software engineer, based in Athens, Greece. The best snippets from my coding adventures are published here to help others learn to code.

If you want to keep in touch, follow me on GitHub.

More like this

  • Git Commits

    A snippet collection of simplified git documentation and tips covering git commit commands.

    Collection · 22 snippets

  • Rewind back to a specific commit in Git

    Did you make a mistake but haven't pushed your changes yet? Learn how to rewind back to a specific commit in Git.

    Git, Branch · May 26, 2023

  • Restore a deleted file

    Restores a file deleted in a specific commit.

    Git, Branch · Apr 13, 2021

  • How does Git's fast-forward mode work?

    Learn about Git's fast-forward mode works and its benefits when mergin branches, so you can decide if it's a good fit for you and your team.

    Git, Branch · Jul 15, 2021