414

What I'm trying to do is a version check. I want to ensure the code stays on top of a minimum version. So I need a way to know if the current branch contains a specified commit.

6
  • 7
    Possible duplicate of How to list branches that contain a given commit?
    – kowsky
    Commented Apr 21, 2017 at 6:11
  • 16
    See the proposed duplicate for how to find all branches that contain a specified commit. To find out if the current branch contains commit C, use the "plumbing" command git merge-base --is-ancestor. The current branch contains C if C is an ancestor of HEAD, so: if git merge-base --is-ancestor $hash HEAD; then echo I contain commit $hash; else echo I do not contain commit $hash; fi
    – torek
    Commented Apr 21, 2017 at 8:09
  • 1
    Hi, please submit this as an answer so that it can be selected as the correct answer =)
    – Ben
    Commented Feb 15, 2018 at 21:18
  • 1
    @Ben - I added it as a community wiki answer.
    – Zitrax
    Commented Sep 3, 2018 at 13:39
  • 2
    @Remover: in shell scripts, zero is true, nonzero is false: the reverse of the C convention. /bin/true was originally implemented as exit 0 and /bin/false as exit 1. (Modern shells have then built in.)
    – torek
    Commented Aug 21, 2019 at 16:12

12 Answers 12

486

Naïve method

There are multiple ways to achieve this result. First naive option is to use git log and search for a specific commit using grep, but that is not always precise

git log | grep <commit_id>

This could result in false positives if git log mentions the commit SHA for other reasons, e.g. it's mentioned in a commit message as the result of a port from another branch.

Interactive Solution

You are better off to use git branch directly to find all branches containing given COMMIT_ID using

git branch --contains $COMMIT_ID

Scriptable Solution

The next step is finding out current branch which can be done since git 1.8.1 using

git symbolic-ref --short HEAD

And combined together as

git branch $(git symbolic-ref --short HEAD) --contains $COMMIT_ID

Even Better Scriptable Solution

But the command above doesn't return true or false and there is a shorter version that returns exit code 0 if commit is in current branch OR exit code 1 if not

git merge-base --is-ancestor $COMMIT_ID HEAD

Exit code is nice, but as you want string true or false as answer you need to add a bit more and then combined with if from bash you get

if [ 0 -eq $(git merge-base --is-ancestor $COMMIT_ID HEAD) ]; then echo "true"; else echo "false"; fi
7
  • 22
    That's not correct; the grep could result in false positives if git log mentions the commit SHA for other reasons, e.g. it's mentioned in a commit message as the result of a port from another branch. Commented Jul 27, 2017 at 15:21
  • 3
    Please see the comment on the question, which uses the built-in git toolset. stackoverflow.com/questions/43535132/…
    – Ben
    Commented Feb 15, 2018 at 21:18
  • 3
    I like the first option, but to rule out Adam's objection, I would add the option --format=format:%H to git log. Commented Feb 20, 2018 at 23:27
  • @AdamSpiers If you're doing this programmatically, sure, but to the human eye it should be clear. git log messages are formatted so that the entries start with commit 012345beef, and then the content is prefixed by spaces, ____Adds lime to coconut. So only if a line started and only contained the word commit and hash would it look suspect. Normally you'd see something like ____(cherry picked from commit 12341234). (Note, I had to put underscores because it renders the Markdown wrong if I use spaces.) Commented Sep 2, 2020 at 19:24
  • When used in detached head mode repo, merge-base --is-ancestor worked while other options didn't. Thanks.
    – Chen A.
    Commented Sep 16, 2020 at 17:13
122

Get a list of branch(es) that contains the specific commit.

# get all the branches where the commit exists
$ git branch --contains <commit-id>

Check if a branch has the specific commit.

# output the branch-name if the commit exists in that branch
$ git branch --contains <commit-id> | grep <branch-name>

Search the branch (say, feature) with exact matching.

$ git branch --contains <commit-id> | grep -E '(^|\s)feature$'

e.g. If you have 3 local branches called feature, feature1, feature2 then

$ git branch --contains <commit-id> | grep 'feature'

# output
feature
feature1
feature2

$ git branch --contains <commit-id> | grep -E '(^|\s)feature$'

# output
feature     

You can also search in both local and remote branches (use -a) or only in remote branches (use -r).

# search in both 'local' & 'remote' branches  
$ git branch -a --contains <commit-id> | grep -E '(^|\s)feature$'

# search in 'remote' branches  
$ git branch -r --contains <commit-id> | grep -E '(^|\s)feature$'
5
  • 1
    The grep here could also result in false positives if there are other branches containing the commit whose names also contain <branch-name> as a substring. Commented Jul 27, 2017 at 15:23
  • 1
    Yes @AdamSpiers, Updated the answer with exact matching the branch name by grep -E '(^|\s)branchname$'
    – Sajib Khan
    Commented Jul 27, 2017 at 22:46
  • 2
    Per this comment on the related question it can be useful to add the -r ("remote") or -a ("all") option to git branch to look for branches that may not be replicated in the local repo clone.
    – sxc731
    Commented May 23, 2018 at 8:37
  • This didn't work for me, I needed git branch --all --contains <commit-id> Commented Mar 13, 2020 at 10:53
  • Doesn't actually show all the branches for me, if I'm on master - it shows it & its remotes, if I'm in my feature branch - shows the branch and it's remotes, but not master Commented Apr 11, 2023 at 7:01
21

Extracted comment by @torek as answer:

See the proposed duplicate for how to find all branches that contain a specified commit.

To find out if the current branch contains commit C, use the "plumbing" command git merge-base --is-ancestor. The current branch contains C if C is an ancestor of HEAD, so:

if git merge-base --is-ancestor $hash HEAD; then
    echo I contain commit $hash
else
    echo I do not contain commit $hash
fi

(Side note: in shell scripts, a command that exits zero is "true" while one that exits nonzero is "false".)

1
  • Note that this may also return true if you are on no branch, aka detached HEAD
    – phil294
    Commented Apr 14 at 19:02
16

Checks only in the local branches, that's been checked-out.

git branch --contains $COMMIT_ID

Checks in all the branch (local and remotes that are fetched)

git branch -a --contains $COMMIT_ID

Make sure to fetch the remote

git fetch origin
14

To list local branches containing commit:

git branch --contains <commit-id>

and to list all branches, including remote only, containing commit:

git branch -a --contains <commit-id>

Similarly to check if commit is in particular branch:

git log <branch> | grep <commit_id>

and if branch does not exist locally prefix branch name with origin/

1
  • Voted up for the remark on -a. Thanks for that advice! Commented May 4, 2020 at 18:10
10

Yeah another alternative:

git rev-list <branch name> | grep `git rev-parse <commit>`

This works best for me since it also works on locally cached remote branches such as remotes/origin/master, on which git branch --contains won't work.

This covers more than OP's question about just "current branch" but I find it dumb to ask a "any branch" version of this question so I decide to post here anyway.

4
git branch --contains <commit-id> --points-at <target branch name>

It will return the target branch name if the commit id exists in that branch. Otherwise the command will fail.

3
  • nope... error: malformed object name 'branch-name' Commented Jul 20, 2018 at 6:02
  • 1
    You might get that error in 2 cases 1. When the given <branch name> doesn't contain the given <commit id> 2. The checkout <branch name> is not the given <branch name>
    – DreamUth
    Commented Aug 2, 2018 at 15:30
  • 1
    I think it changed, now if the target branch doesn't have the commit you get nothing, if the target branch doesn't exist then you have an error
    – Philippe
    Commented Oct 15, 2020 at 8:52
1

If you're using libgit2, you may want to use the revwalk functions to go through all commits one by one, or the descendants functions to find out if a commit is a descendant of a branch's last commit.

The git branch --contains <commit> command uses descendants: https://github.com/git/git/blob/8f7582d995682f785e80e344197cc715e6bc7d8e/commit-reach.c#L775

0

Since this question has some nice scripty answers, I'm adding my favorites.

This one will show you, for last $n commits in $br which branches contains each:

br=mybranch
n=10
git log --oneline -n$n $br | awk '{print; system("git branch --contains "$1)}'

This one is similar but does the same for a list of branches:

br="mybranch yourbranch herbranch"
n=4
for br in $brs; do git log --oneline -n$n $br | awk '{print; system("git branch --contains "$1)}'; done
0

On a Windows machine on Windows/CMD terminal. You can do:

> git log | findstr "commit-id"

like:

> git log | findstr "c601cd6366d"
0

Use git cherry:

COMMIT_HASH="abc"

if [ -z "$(git cherry -v main $COMMIT_HASH)" ]; then
    echo "Commit exists on the main branch."
else
    echo "Commit does not exist on the main branch."
fi
-1

Git checkout will return you to the branch of your current HEAD (current file) but it will not check out all branches. For example if you do a git checkout master the code will be checked out as if you were on master branch but it will not check out any files for changes outside of that branch.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.