Is there any way to find out what branch a commit comes from given its sha1?
Bonus points if you can tell me how to accomplish this using Ruby Grit.
Is there any way to find out what branch a commit comes from given its sha1?
Bonus points if you can tell me how to accomplish this using Ruby Grit.
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.
git branch --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.
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. 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 repo; 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.
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.
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 broughtCOMMIT
into the specified BRANCH(es).Specificially, look for the oldest commit on the first-parent history of
BRANCH
that contains theCOMMIT
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.
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
For example to find thatc0118fa
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
UPD
Or just one command:
git log c0118fa..HEAD --ancestry-path --merges --oneline --color | tail -n 1
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)
Poor man\'s option is to use the tool tig
1 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 can also assist in staging changes for commit at chunk level and act as a pager for output from various Git commands.
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
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 sha1\"), then without the reflog there\'s no 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 e@x.io
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
Use the below if you care about shell exit statuses:
branch-current
- the current branch\'s namebranch-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 argument, and default to HEAD
if none given.
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\\\" #\"
% git branch-name eae13ea
master
% echo $?
0
0
.% git branch-name 4bc6188
Multiple branches:
attempt-extract
master%
% echo $?
1
1
.Because of the exit status, these can be safely built upon. Eg to get the remote used for fetching:
remote-fetch = !\"branch=$(git branch-name \\\"$1\\\") && git config branch.\\\"$branch\\\".remote || echo origin #\"
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, no local copies are 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\'
To find local branch
grep -lR YOUR_COMMIT .git/refs/heads | sed \'s/.git\\/refs\\/heads\\///g\'
To find remote branch
grep -lR $commit .git/refs/remotes | sed \'s/.git\\/refs\\/remotes\\///g\'
Aside from searching through all of the tree until you find a matching hash, no.