Listing Branches that Contain a Given Commit

One of the most common questions while working with git branches is “ How to list branches that contain a given commit?”.

This tutorial will give you a solid plan on how to do it.

First, you need to invoke the git branch command, like this

git branch --contains <commit>

You can also replace <commit> with SHA you want to find by running the following command:

git branch --contains f0a1344352
* master
  remotes/origin/master

 git branch --contains abdsd9e4df
error: no such commit  abdsd9e4df

In case you use the command mentioned above very frequently, you can create a git alias, like this:

[alias]
    contains = !sh -c 'git branch -a --contains $1' -

Afterward, you can already use the git contains command, as follows;

git contains f0a1344352
* master
  remotes/origin/master

The Usage of Git Branch

Generally, branching is available in almost all version control systems. You can use the git branch command for creating, deleting, or listing branches in Git. But it would be best if you consider that it won’t allow switching between branches or putting a forked history back together. Each new branch created by you summarizes the changes in case you wish to fix bugs or attach new features.

What is Git Alias

The git alias is used for creating short commands and mapping the longer ones. A git alias command doesn’t exist. It is created with the help of the git config command as well as the git configuration files. Git aliases are being generated both in local and global scopes.


Do you find this helpful?

Related articles