How to tell which branch a github commit was for?

On github, when I view a commit, it shows me the commit message and the changes, along with any comments at the bottom. However, it doesn't tell me what branch the commit was checked in to. Even if I 'Browse Code', it is browsing the code for a particular 'tree' (presumably the state of the code when the commit was made), rather than for a particular branch.

I know that commits in git aren't intrinsically linked to a branch, but surely they are always going to be first committed into a particular branch? Isn't the commit tagged with that branch, and can I view which branch it was somehow?

Answers


OK, the answer to this question, fundamentally, is: there is no definitive way to tell. Mercurial actually tags commits with the name of the branch they were checked in to, but git simply doesn't; apparently, it isn't considered important what the name of the branch was. This was a design decision and it doesn't look like it's going to change.


If you know the commit number, you can simply do this

git branch --contains <commit>

This should give you the branch name in which the commit was made.

UPDATE: On GitHub specifically, you now can see the branch a given commit is part of. The blog post "Branch and Tag Labels For Commit Pages" details:

If the commit is not on the default branch, the indicator will show the branches which contain the commit. If the commit is part of an unmerged pull request, a link will be shown.


From git help branch:

With --contains, shows only the branches that contain the named commit (in other words, the branches whose tip commits are descendants of the named commit).

With --merged, only branches merged into the named commit (i.e. the branches whose tip commits are reachable from the named commit) will be listed.

With --no-merged only branches not merged into the named commit will be listed. If the argument is missing it defaults to HEAD (i.e. the tip of the current branch).


If it's a fairly recent commit, you can go to your network graph (e.g., https://github.com/BenHocking/ShortCircuitGA/network) and hover over each node on a branch until you find the commit you're looking for. It's not efficient, but it's the only way I know how to do it directly from GitHub. (If you've got SourceTree, GitX, or other visual Git clients, there might be other alternatives, as well as command line alternatives.)


Start to submit a pull request for the commit. You'll see the default source branch on the "from" side.


This answer won't help for previous check-ins, but I use the following commit-msg hook script to append the current branch to every commit message.

#!/bin/sh
#
export BRANCH=`git status | head -1 | cut -c13-`
echo -n "($BRANCH) - " > .git/tmp-msg
cat $1 >> .git/tmp-msg
mv .git/tmp-msg $1

exit 0

Need Your Help