902

Is there a way to find out what branch a commit comes from given its SHA-1 hash value?

Bonus points if you can tell me how to accomplish this using Ruby Grit.

2
  • 20
    The different methods below are pragmatic, useful, working ways to infer a probable answer, but let's note that in git the question itself is a misunderstanding, commits don't come from branches. Branches come and go, they move, only commits represent the real repo history. Then again, this is not a way to say below solutions are bad. Just know none of them gives a fully trustable answer, which is unobtainable by design in git. (A simple case is deleted branches : I branch, I commit twice, I merge into another branch, I delete the first branch. Where does the commit "come" from? Commented Sep 3, 2019 at 16:02
  • 2
    I have a case where I explicitly pull a depth 1 shallow clone from a tag. It's cheap and easy and efficient... and until now did everything I wanted beautifully. Now that I'd like to know which branch the tag was on, I'm hosed, at least for that detail. You can't always go home, lol Commented Dec 12, 2019 at 21:12

17 Answers 17

1180
+50

While Dav is correct that the information isn't directly stored, that doesn't mean you can't ever find out. Here are a few things you can do.

Find branches the commit is on

git branch -a --contains <commit>

This will tell you all branches which have the given commit in their history. Obviously this is less useful if the commit's already been merged.

Search the reflogs

If you are working in the repository in which the commit was made, you can search the reflogs for the line for that commit. Reflogs older than 90 days are pruned by git-gc, so if the commit's too old, you won't find it. That said, you can do this:

git reflog show --all | grep a871742

to find commit a871742. Note that you MUST use the abbreviatd 7 first digits of the commit. The output should be something like this:

a871742 refs/heads/completion@{0}: commit (amend): mpc-completion: total rewrite

indicating that the commit was made on the branch "completion". The default output shows abbreviated commit hashes, so be sure not to search for the full hash or you won't find anything.

git reflog show is actually just an alias for git log -g --abbrev-commit --pretty=oneline, so if you want to fiddle with the output format to make different things available to grep for, that's your starting point!

If you're not working in the repository where the commit was made, the best you can do in this case is examine the reflogs and find when the commit was first introduced to your repository; with any luck, you fetched the branch it was committed to. This is a bit more complex, because you can't walk both the commit tree and reflogs simultaneously. You'd want to parse the reflog output, examining each hash to see if it contains the desired commit or not.

Find a subsequent merge commit

This is workflow-dependent, but with good workflows, commits are made on development branches which are then merged in. You could do this:

git log --merges <commit>..

to see merge commits that have the given commit as an ancestor. (If the commit was only merged once, the first one should be the merge you're after; otherwise you'll have to examine a few, I suppose.) The merge commit message should contain the branch name that was merged.

If you want to be able to count on doing this, you may want to use the --no-ff option to git merge to force merge commit creation even in the fast-forward case. (Don't get too eager, though. That could become obfuscating if overused.) VonC's answer to a related question helpfully elaborates on this topic.

18
  • 5
    +1. The sheer absence of information for that particular issue makes you wonder if it is actually a problem? Branches can change, be renamed or be deleted at any time. May be git describe is enough, for (annotated) tags can be viewed as more significant than branches.
    – VonC
    Commented Apr 25, 2010 at 9:48
  • 9
    I definitely agree - branches are meant to be lightweight and flexible. If you do adopt a workflow in which that information becomes important, you can use the --no-ff option to make sure there's always a merge commit, so you can always trace the path of a given commit as it's merged toward master.
    – Cascabel
    Commented May 17, 2010 at 17:35
  • 37
    FYI, if you want to find commits that only exist on the remote, add the -a flag to the first command e.g. git branch -a --contains <commit> Commented Oct 26, 2011 at 3:48
  • 8
    @JonathanDay: No, that will find commits on any branch. If you want only ones on the remote, use -r.
    – Cascabel
    Commented Oct 26, 2011 at 5:34
  • 8
    @SimonTewsi Like I said, if it's really an issue, use merge --no-ff to reliably record the branch name when you merge. But otherwise, think of branch names as temporary short labels, and commit descriptions as permanent ones. "What short name did we refer to this by during development?" should really not be as important a question as "what does this commit do?"
    – Cascabel
    Commented May 15, 2013 at 14:29
260
+50

This simple command works like a charm:

git name-rev <SHA>

For example (where test-branch is the branch name):

git name-rev 651ad3a
251ad3a remotes/origin/test-branch

Even this is working for complex scenarios, like:

origin/branchA/
              /branchB
                      /commit<SHA1>
                                   /commit<SHA2>

Here git name-rev commit<SHA2> returns branchB.

10
  • 14
    I found git name-rev --name-only <SHA> to be more useful for getting only the branch name. My question... Can it return more than one branch in any circumstance?
    – dnk8n
    Commented Jul 31, 2019 at 7:59
  • 7
    If you want to find only branch name and except tags following command can be used: git name-rev --refs="refs/heads/*" --name-only <SHA> Commented Feb 17, 2020 at 5:51
  • 2
    Unfortunately, this only works for a short time after the merge, before the ref gets garbage collected. It is the same limitation as for the method "Search the reflogs" in @Cascabel 's answer. As pointed out by VonC , the only real way to preserve this information is if your workflow includes saving it in a git note or annotated tag for each merge.
    – Yitz
    Commented Apr 19, 2020 at 9:40
  • 6
    This does not work correctly. In some project, I have done all my commits in master, but this git name-rev command on my commits gives branches I have never used, in particular remote branches from another user. This makes no sense at all!
    – vinc17
    Commented Jun 12, 2020 at 10:58
  • 1
    In my tests this command only shows one branch. For a commit with 3 branches attached, the -a --contains option lists all three. Commented Aug 5, 2020 at 13:14
51

For example, to find that c0118fa commit came from redesign_interactions:

* ccfd449 (HEAD -> develop) Require to return undef if no digits found
*   93dd5ff Merge pull request #4 from KES777/clean_api
|\
| * 39d82d1 Fix tc0118faests for debugging debugger internals
| * ed67179 Move &push_frame out of core
| * 2fd84b5 Do not lose info about call point
| * 3ab09a2 Improve debugger output: Show info about emitted events
| *   a435005 Merge branch 'redesign_interactions' into clean_api
| |\
| | * a06cc29 Code comments
| | * d5d6266 Remove copy/paste code
| | * c0118fa Allow command to choose how continue interaction
| | * 19cb534 Emit &interact event

You should run:

git log c0118fa..HEAD --ancestry-path --merges

And scroll down to find last merge commit. Which is:

commit a435005445a6752dfe788b8d994e155b3cd9778f
Merge: 0953cac a06cc29
Author: Eugen Konkov
Date:   Sat Oct 1 00:54:18 2016 +0300

    Merge branch 'redesign_interactions' into clean_api

Update

Or just one command:

git log c0118fa..HEAD --ancestry-path --merges --oneline --color | tail -n 1
8
  • 9
    This was the only solution that gave me the desired result. I tried the rest.
    – crafter
    Commented Dec 15, 2016 at 13:49
  • 1
    Note this only works if the merge commit summaries contain the branch names used in the merges, which by default they usually do. However git merge -m"Any String Here" will obscure the source and target branch information.
    – qneill
    Commented Jan 23, 2018 at 22:58
  • @qneill: No, the merge always has info about merged branches: Merge: f6b70fa d58bdcb. You can name your merge commit as you with. I will not have matter Commented Jan 24, 2018 at 7:27
  • 2
    @EugenKonkov once the branches have traversed the merge, the history about which path in the graph they came from is left in the reflog, but not in the git object database. I posted an answer trying to explain what I'm saying
    – qneill
    Commented Jan 26, 2018 at 21:31
  • UPD version works for me, simple command that finds the original commit
    – vpalmu
    Commented Apr 9, 2018 at 10:42
50

Update December 2013:

sschuberth comments

git-what-branch (Perl script, see below) does not seem to be maintained anymore. git-when-merged is an alternative written in Python that's working very well for me.

It is based on "Find merge commit which include a specific commit".

git when-merged [OPTIONS] COMMIT [BRANCH...]

Find when a commit was merged into one or more branches.
Find the merge commit that brought COMMIT into the specified BRANCH(es).

Specifically, look for the oldest commit on the first-parent history of BRANCH that contains the COMMIT as an ancestor.


Original answer September 2010:

Sebastien Douche just twitted (16 minutes before this SO answer):

git-what-branch: Discover what branch a commit is on, or how it got to a named branch

This is a Perl script from Seth Robertson that seems very interesting:

SYNOPSIS

git-what-branch [--allref] [--all] [--topo-order | --date-order ]
[--quiet] [--reference-branch=branchname] [--reference=reference]
<commit-hash/tag>...

OVERVIEW

Tell us (by default) the earliest causal path of commits and merges to cause the requested commit got onto a named branch. If a commit was made directly on a named branch, that obviously is the earliest path.

By earliest causal path, we mean the path which merged into a named branch the earliest, by commit time (unless --topo-order is specified).

PERFORMANCE

If many branches (e.g. hundreds) contain the commit, the system may take a long time (for a particular commit in the Linux tree, it took 8 second to explore a branch, but there were over 200 candidate branches) to track down the path to each commit.
Selection of a particular --reference-branch --reference tag to examine will be hundreds of times faster (if you have hundreds of candidate branches).

EXAMPLES

 # git-what-branch --all 1f9c381fa3e0b9b9042e310c69df87eaf9b46ea4
 1f9c381fa3e0b9b9042e310c69df87eaf9b46ea4 first merged onto master using the following minimal temporal path:
   v2.6.12-rc3-450-g1f9c381 merged up at v2.6.12-rc3-590-gbfd4bda (Thu May  5 08:59:37 2005)
   v2.6.12-rc3-590-gbfd4bda merged up at v2.6.12-rc3-461-g84e48b6 (Tue May  3 18:27:24 2005)
   v2.6.12-rc3-461-g84e48b6 is on master
   v2.6.12-rc3-461-g84e48b6 is on v2.6.12-n
   [...]

This program does not take into account the effects of cherry-picking the commit of interest, only merge operations.

2
  • 5
    git-what-branch does not seem to be maintained anymore. git-when-merged is an alternative written in Python that's working very well for me.
    – sschuberth
    Commented Dec 6, 2013 at 14:37
  • @sschuberth thank you for this update. I have included your comment in the answer for more visibility.
    – VonC
    Commented Dec 6, 2013 at 14:52
26

khichar.anil covered most of this in his answer.

I am just adding the flag that will remove the tags from the revision names list. This gives us:

git name-rev --name-only --exclude=tags/* $SHA
4
  • Something seems to be missing in the second sentence. Commented Dec 4, 2019 at 13:02
  • 2
    This helped me, otherwise I was getting a tag instead of a branch in the output
    – Cole
    Commented Apr 25, 2021 at 23:17
  • 2
    This was exactly why I came here. Thanks for sharing. Commented Nov 24, 2021 at 14:14
  • This command also works using tags instead of hashes. Commented Mar 18 at 20:26
14

I tried all of the above solutions, and none of them quite worked for me.

Here is the only method that has worked for me so far (assuming HEAD is in a sensible place):

git log --branches --source | grep <sha>

#or if you also care about remotes
git log --branches --remotes --source | grep <sha>

The name of the branch should be at the end of the line.

From the documentation

--source

Print out the ref name given on the command line by which each commit was reached.

So this may change depending on where HEAD is, but for me putting HEAD at the latest commit on my master branch produced the results I expected.

Visual inspection with gitk --all may also be helpful. It has a 'branches' field for each commit, but it shows all branches that "can reach" that commit, not necessarily which branch that commit is "on". See here


Aside:

It's also worth mentioning that this question is a bit of a misnomer. Unlike other SVCs, in git branches are just temporary pointers to locations in your commit history/graph. They don't 'own' commits nor are they 'composed of' commits. They are essentially just tags that update to whatever commit you push while you have the tag checked out. So a branch is not a series of commits, but rather a pointer to the end/tip of a series of commits. As a result, commits don't 'belong to' or 'come from' a branch, they are just nodes in your graph.

So what are we actually doing here? We are using the idea of reachability. If we start at the commit each branch points to (the 'end' of the 'branch') and go backwards through history, what commits can we find/reach? We say commits are 'on' a branch if the only way to find them is by starting our search on that particular branch.

Note that this is not a stable thing. If we merge, rename, or rebase our branch all of a sudden which commits are reachable by (and thus 'on') our branch will change. This is why commits being 'on/owned by' a particular branch is not an official or always meaningful concept in git. (and why this question doesn't have a clean/definitive answer)

1
  • 1
    This was exactly it; eloquent too.
    – Shōgun8
    Commented Apr 22, 2022 at 19:58
12

git branch --contains <ref> is the most obvious "porcelain" command to do this. If you want to do something similar with only "plumbing" commands:

COMMIT=$(git rev-parse <ref>) # expands hash if needed
for BRANCH in $(git for-each-ref --format "%(refname)" refs/heads); do
  if $(git rev-list $BRANCH | fgrep -q $COMMIT); then
    echo $BRANCH
  fi
done

(crosspost from this SO answer)

1
  • Oh HELL yeah! I'm trying to write a script that will create the most succinct diff from a collection of repos in a very liberal range of states (on a local branch or detatched head, with tracking and an upstream set or not, etc.) and this is my answer! ~~This bastard~~ My coworker will be connected via LTE or sometimes Iridium, so I need to reduce bandwidth as much as possible and this mechanism tells me where I can start from (I just change refs/heads to refs/remotes/origin). Commented Sep 15, 2021 at 7:34
8

I deal with the same problem (Jenkins multibranch pipeline) - having only commit information and trying to find a branch name where this commit originally came from. It must work for remote branches, local copies are not available.

This is what I work with:

git rev-parse HEAD | xargs git name-rev

Optionally you can strip the output:

git rev-parse HEAD | xargs git name-rev | cut -d' ' -f2 | sed 's/remotes\/origin\///g'
0
5

The short answer is that Git doesn't store the name of the branch on which a commit was made. Tricks to try to reconstruct this information don't seem to work in all cases.

2
  • 1
    As a postscript, I personally feel that the fact that the branches in the Git history are unnamed is a severe lack of documentation. It makes the commit history very hard to understand.
    – Dimitri C.
    Commented Aug 18, 2022 at 9:45
  • 2
    Agreed, it's basically a designed limitation of git. It purposely doesn't store the branch names. But I feel this is rather critical piece of information to learn about the history of changes
    – O'Rooney
    Commented Jul 10, 2023 at 4:24
3

A poor man's option is to use the tool tig1 on HEAD, search for the commit, and then visually follow the line from that commit back up until a merge commit is seen. The default merge message should specify what branch is getting merged to where :)

1 Tig is an ncurses-based text-mode interface for Git. It functions mainly as a Git repository browser, but it can also assist in staging changes for commit at chunk level and act as a pager for output from various Git commands.

0
3

If the OP is trying to determine the history that was traversed by a branch when a particular commit was created ("find out what branch a commit comes from given its SHA-1 hash value"), then without the reflog there aren't any records in the Git object database that shows what named branch was bound to what commit history.

(I posted this as an answer in reply to a comment.)

Hopefully this script illustrates my point:

rm -rf /tmp/r1 /tmp/r2; mkdir /tmp/r1; cd /tmp/r1
git init; git config user.name n; git config user.email [email protected]
git commit -m"empty" --allow-empty; git branch -m b1; git branch b2
git checkout b1; touch f1; git add f1; git commit -m"Add f1"
git checkout b2; touch f2; git add f2; git commit -m"Add f2"
git merge -m"merge branches" b1; git checkout b1; git merge b2
git clone /tmp/r1 /tmp/r2; cd /tmp/r2; git fetch origin b2:b2
set -x;
cd /tmp/r1; git log --oneline --graph --decorate; git reflog b1; git reflog b2;
cd /tmp/r2; git log --oneline --graph --decorate; git reflog b1; git reflog b2;

The output shows the lack of any way to know whether the commit with 'Add f1' came from branch b1 or b2 from the remote clone /tmp/r2.

(Last lines of the output here)

+ cd /tmp/r1
+ git log --oneline --graph --decorate
*   f0c707d (HEAD, b2, b1) merge branches
|\
| * 086c9ce Add f1
* | 80c10e5 Add f2
|/
* 18feb84 empty
+ git reflog b1
f0c707d b1@{0}: merge b2: Fast-forward
086c9ce b1@{1}: commit: Add f1
18feb84 b1@{2}: Branch: renamed refs/heads/master to refs/heads/b1
18feb84 b1@{3}: commit (initial): empty
+ git reflog b2
f0c707d b2@{0}: merge b1: Merge made by the 'recursive' strategy.
80c10e5 b2@{1}: commit: Add f2
18feb84 b2@{2}: branch: Created from b1
+ cd /tmp/r2
+ git log --oneline --graph --decorate
*   f0c707d (HEAD, origin/b2, origin/b1, origin/HEAD, b2, b1) merge branches
|\
| * 086c9ce Add f1
* | 80c10e5 Add f2
|/
* 18feb84 empty
+ git reflog b1
f0c707d b1@{0}: clone: from /tmp/r1
+ git reflog b2
f0c707d b2@{0}: fetch origin b2:b2: storing head
2
  • And what is the output for git log 80c10e5..HEAD --ancestry-path --merges --oneline --color | tail -n 1 and git log 086c9ce..HEAD --ancestry-path --merges --oneline --color | tail -n 1 commands for both cases? Commented Jan 27, 2018 at 0:08
  • The SHAs of course change any time the commands are run, to respell your commands I used HEAD^1 and HEAD^2 on a fresh run. The commands and output are: $ git log HEAD^1..HEAD --ancestry-path --merges --oneline --color | tail -n 1 which yields 376142d merge branches and $ git log HEAD^2..HEAD --ancestry-path --merges --oneline --color | tail -n 1 which yields 376142d merge branches -- which shows the merge commit summary, which (as I was asserting) can be overwritten when the merge is created, possibly obfuscating the branch history of the merge.
    – qneill
    Commented Feb 2, 2018 at 15:42
2

As an experiment, I made a post-commit hook that stores information about the currently checked out branch in the commit metadata. I also slightly modified gitk to show that information.

You can check it out here: https://github.com/pajp/branch-info-commits

2
1

TL;DR:

Use the below if you care about shell exit statuses:

  • branch-current - the current branch's name
  • branch-names - clean branch names (one per line)
  • branch-name - Ensure that only one branch is returned from branch-names

Both branch-name and branch-names accept a commit as the argument, and default to HEAD if none is given.


Aliases useful in scripting

branch-current = "symbolic-ref --short HEAD"  # https://stackoverflow.com/a/19585361/5353461
branch-names = !"[ -z \"$1\" ] && git branch-current 2>/dev/null || git branch --format='%(refname:short)' --contains \"${1:-HEAD}\" #"  # https://stackoverflow.com/a/19585361/5353461
branch-name = !"br=$(git branch-names \"$1\") && case \"$br\" in *$'\\n'*) printf \"Multiple branches:\\n%s\" \"$br\">&2; exit 1;; esac; echo \"$br\" #"

Commit only reachable from only one branch

% git branch-name eae13ea
master
% echo $?
0
  • Output is to STDOUT
  • Exit value is 0.

Commit reachable from multiple branches

% git branch-name 4bc6188
Multiple branches:
attempt-extract
master%
% echo $?
1
  • Output is to STDERR
  • The exit value is 1.

Because of the exit status, these can be safely built upon. For example, to get the remote used for fetching:

remote-fetch = !"branch=$(git branch-name \"$1\") && git config branch.\"$branch\".remote || echo origin #"
1
  • I had to google what reachable means, and what I found was that the commit is reachable if it is reachable in following the parent links, For me this was lightbulb moment as I initially had thought if its a tree structure, then all nodes are reachable if you can go any direction, this is not the case its a parent only direction - someone please correct me if I'm wrong Commented Apr 8, 2021 at 9:44
1

git checkout <SHA> -> Will make you go into a detached HEAD state.

git rev-parse --abbrev-ref HEAD -> will print HEAD, so this command won't work in this state.

Now that we are in a detached HEAD state, we can use the following command to get the branch name.
PS: This will not work if you are not in a detached HEAD state!

git branch -a --contains HEAD | sed -n 2p | awk '{ printf $1 }'

Voilà! Now you have the branch name.

0

I think someone should face the same problem that can't find out the branch, although it actually exists in one branch.

You'd better pull all first:

git pull --all

Then do the branch search:

git name-rev <SHA>

or:

git branch --contains <SHA>
-1

To find the local branch:

grep -lR YOUR_COMMIT .git/refs/heads | sed 's/.git\/refs\/heads\///g'

To find the remote branch:

grep -lR $commit .git/refs/remotes | sed 's/.git\/refs\/remotes\///g'
1
  • 2
    some explanations of your examples will be great Commented Feb 23, 2018 at 9:16
-7

Aside from searching through all of the tree until you find a matching hash, no.

1
  • 8
    I don't really want to downvote this, because strictly speaking it's true that git doesn't permanently store that information, but it is very often possible to find out nonetheless. See my answer.
    – Cascabel
    Commented Apr 25, 2010 at 4:09

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