How can I query git to find out which branches contain a given commit? gitk
will usually list the branches, unless there are too many, in which case it just says "many (38)" or something like that. I need to know the full list, or at least whether certain branches contain the commit.

- 5,932
- 2
- 32
- 57

- 80,040
- 26
- 132
- 171
-
4See also: [How to list all tags that contain a commit?](http://stackoverflow.com/questions/7923091/searching-for-all-tags-that-contain-a-commit). – Andrew Marshall Jan 18 '13 at 17:51
-
Related question for an equivalent commit per comments: http://stackoverflow.com/questions/16304574/how-to-list-branches-that-contain-an-equivalent-commit – UpAndAdam Feb 25 '15 at 16:38
3 Answers
From the git-branch manual page:
git branch --contains <commit>
Only list branches which contain the specified commit (HEAD if not specified). Implies
--list
.
git branch -r --contains <commit>
Lists remote tracking branches as well (as mentioned in user3941992's answer below) that is "local branches that have a direct relationship to a remote branch".
As noted by Carl Walsh, this applies only to the default refspec
fetch = +refs/heads/*:refs/remotes/origin/*
If you need to include other ref namespace (pull request, Gerrit, ...), you need to add that new refspec, and fetch again:
git config --add remote.origin.fetch "+refs/pull/*/head:refs/remotes/origin/pr/*"
git fetch
git branch -r --contains <commit>
See also this git ready article.
The
--contains
tag will figure out if a certain commit has been brought in yet into your branch. Perhaps you’ve got a commit SHA from a patch you thought you had applied, or you just want to check if commit for your favorite open source project that reduces memory usage by 75% is in yet.
$ git log -1 tests
commit d590f2ac0635ec0053c4a7377bd929943d475297
Author: Nick Quaranto <nick@quaran.to>
Date: Wed Apr 1 20:38:59 2009 -0400
Green all around, finally.
$ git branch --contains d590f2
tests
* master
Note: if the commit is on a remote tracking branch, add the -a
option.
(as MichielB comments below)
git branch -a --contains <commit>
MatrixFrog comments that it only shows which branches contain that exact commit.
If you want to know which branches contain an "equivalent" commit (i.e. which branches have cherry-picked that commit) that's git cherry
:
Because
git cherry
compares the changeset rather than the commit id (sha1), you can usegit cherry
to find out if a commit you made locally has been applied<upstream>
under a different commit id.
For example, this will happen if you’re feeding patches<upstream>
via email rather than pushing or pulling commits directly.
__*__*__*__*__> <upstream>
/
fork-point
\__+__+__-__+__+__-__+__> <head>
(Here, the commits marked '-
' wouldn't show up with git cherry
, meaning they are already present in <upstream>
.)

- 1,262,500
- 529
- 4,410
- 5,250
-
From the output of this command, does that mean the commit is on tests _and_ master, or just master? – Cory Klein Jan 28 '11 at 16:59
-
3`tests` and `master` - `master` is the current branch, therefore the asterisk. – blueyed Mar 25 '11 at 13:31
-
61This only shows which branches contain *that exact commit*. If you want to know which branches contain an "equivalent" commit (i.e. which branches have cherry-picked that commit) that's `git cherry`: "Because git cherry compares the changeset rather than the commit id (sha1), you can use git cherry to find out if a commit you made locally has been applied
under a different commit id. For example, this will happen if you’re feeding patches – Tyler Apr 14 '11 at 01:04via email rather than pushing or pulling commits directly." http://www.kernel.org/pub/software/scm/git/docs/git-cherry.html -
How can you do something similar but to see if a branch instead of a commit has been brought in? something like git branch --contains branchname – jhanifen Oct 18 '11 at 03:26
-
@jhanifen: considering a branch is just a pointer to a certain commit in the DAG of commits, you might have to list all its commits and apply this answer for each one. – VonC Oct 18 '11 at 04:08
-
69
-
Is there an easy way to get this information in a shell-parseable format? – nornagon Mar 01 '12 at 00:05
-
31You can also do `git tag --contains
`. See [Searching for all tags that contain a commit?](http://stackoverflow.com/questions/7923091/searching-for-all-tags-that-contain-a-commit). – Andrew Marshall Jan 18 '13 at 17:42 -
Can someone provide a working example of how to use git cherry to achieve this? Git cherry SEEMS to be geared for the reverse, finding branches not pushed upstream but that only works if you know where to start, and know the name of every branch you want to test as upstream. – UpAndAdam Apr 30 '13 at 14:04
-
1@UpAndAdam I would recommend making that a separate question (with a link referring back to this one). – VonC Apr 30 '13 at 15:27
-
2This answer did not work for me, but it did when I found out I needed to do `git branch -a --contains
`. It was in a branch on a remote -- can you maybe update your answer? – MichielB Aug 28 '13 at 12:08 -
5For the `git cherry` part @UpAndAdam asked the question here: http://stackoverflow.com/questions/16304574/how-to-list-branches-that-contain-an-equivalent-commit, alas, the question has not (yet) been answered. – adeelx Aug 06 '14 at 19:24
-
@VonC: Is it possible to see exact date/time at which that hash was committed or merged or rebased in specific branch? Because I see multiple branches for single hash. So in one of the branch that code was committed and in remaining branches it was merged/rebased. How do I see exact event date/time? – user613114 Mar 30 '17 at 06:48
-
@user613114 I suppose you simply can get the log for that hash: `git log
` – VonC Mar 30 '17 at 07:44 -
Unfortunately, for a commit that is the branching point of a branch, `git branch --contains` will list not only the branch created from that commit, but also `master` and all subsequent branches. I wonder if there is a method to only find the created branch descending from the given commit. – Alexander Amelkin Aug 30 '18 at 16:18
-
@AlexanderAmelkin I'm a couple of months late, but... not exactly. Branches are just names for the commits at their heads, after all. If you make a note of the commit *after* the one you want on `master`, you could find all branches *that didn't subsequently merge from master* using `git branch --contains
--no-contains – Darael Nov 23 '18 at 11:19` (but ones that later merged from master won't show up), or you could use `git rev-list --children ` and then try `git branch --contains` on each child to get all branches that started there. -
@andreee That sounds [Leet](https://en.wikipedia.org/wiki/Leet). Not sure why [`git branch --show-current`](https://stackoverflow.com/a/55088865/6309) was downvoted though. – VonC Jul 04 '19 at 12:32
-
is there an argument I can add to `git branch --contains {hash}` that will also output the dateTime that the commit was merged into respective branches? – Brad Kent Jun 25 '21 at 21:42
-
@BradKent I don't think so, not without a script. The [`--format=` on `git branch`](https://git-scm.com/docs/git-branch#Documentation/git-branch.txt---formatltformatgt) is for the branch itself, not a merge commit. – VonC Jun 25 '21 at 21:45
You may run:
git log <SHA1>..HEAD --ancestry-path --merges
From comment of last commit in the output you may find original branch name
Example:
c---e---g--- feature
/ \
-a---b---d---f---h---j--- master
git log e..master --ancestry-path --merges
commit h
Merge: g f
Author: Eugen Konkov <>
Date: Sat Oct 1 00:54:18 2016 +0300
Merge branch 'feature' into master

- 22,193
- 17
- 108
- 158
-
6Nice! I used `git log
..master --ancestry-path --merges --oneline | tail -n1` to get this in one line – James EJ Aug 11 '17 at 19:59 -
1If you would like to use pure git command, you could use: `git log
..master --ancestry-path --merges --oneline -1` – Bartosz Mar 23 '18 at 12:03 -
Note: When your commit sha is the most recent commit on the master/foo branch (HEAD)... you can't do an `A..B` commit range, just dont use a range like so:: `git log HEAD --oneline -1` > `82c12a9 (HEAD, origin/remote-branch-name, origin/master, origin/dev, origin/HEAD, master, dev) commit message`. – Devin Rhode Dec 01 '19 at 03:38
-
If this git repo is a submodule and you are trying to solve the detached HEAD problem... then you have a hard question of a preferred branch... In my previous example you could easily say that `master` is always preferred if it's in this list. From there it's less clear. You could try and read git branch from .gitmodules file: `git config -f .gitmodules submodule.src/foo/submodule.branch`. This could be a long standing fork/pr. You can cd to repo root and run `git config submodule.src/foo/submodule.branch`. You can also use the superprojects current git branch. – Devin Rhode Dec 01 '19 at 03:46
-
As a small aside: `git config submodule.src/foo/submodule.branch` Can be influenced by any variety of git configs, including a repo-local .gitconfig file. (requires running `git config --local include.path ./path/to/your/.gitconfig`) – Devin Rhode Dec 01 '19 at 03:51
The answer for git branch -r --contains <commit>
works well for normal remote branches, but if the commit is only in the hidden head
namespace that GitHub creates for PRs, you'll need a few more steps.
Say, if PR #42 was from deleted branch and that PR thread has the only reference to the commit on the repo, git branch -r
doesn't know about PR #42 because refs like refs/pull/42/head
aren't listed as a remote branch by default.
In .git/config
for the [remote "origin"]
section add a new line:
fetch = +refs/pull/*/head:refs/remotes/origin/pr/*
(This gist has more context.)
Then when you git fetch
you'll get all the PR branches, and when you run git branch -r --contains <commit>
you'll see origin/pr/42
contains the commit.

- 6,100
- 2
- 46
- 50
-
@VonC Thanks for improving my answer! I didn't realized I was doing `git commit --add`. – Carl Walsh Nov 09 '20 at 19:10