23

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?

Jez
  • 27,951
  • 32
  • 136
  • 233
  • Perhaps you can tell from subsequent merge commits? Otherwise I'm not aware of any way of doing this; a branch is just a reference to a commit and is supposed to be transient. – Will Vousden Feb 20 '12 at 12:15
  • 2
    I find the github commit view of very little use due to the problem, you mentioned. Another missing feature are links to all child commits (maybe even cross-clone). For your problem, github should link all branches, that are listed by `git branch --contains `. – Tilman Vogel Feb 20 '12 at 12:40
  • As for which branch it was originally committed to, I tend to the position that it is quite useless to know because it may have been some multiply rebased local feature branch nobody ever was meant to see. And it is not recorded anyway. – Tilman Vogel Feb 20 '12 at 12:44
  • Does this answer your question? [How to find the branch from commit id](https://stackoverflow.com/questions/15647221/how-to-find-the-branch-from-commit-id) – Michael Freidgeim May 16 '20 at 02:34

6 Answers6

11

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.

Jez
  • 27,951
  • 32
  • 136
  • 233
11

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.

enter image description here

Michael Freidgeim
  • 26,542
  • 16
  • 152
  • 170
PriyankaK
  • 925
  • 2
  • 10
  • 18
  • 1
    Or if you want to look at the remote branches too: git branch -a --contains – Tsvetomir Dimitrov May 11 '16 at 08:34
  • useful information, but slightly different answer than what the question was asking. this shows all current branches for which the commit is contained within the history. this is likely multiple branches. the OP wants to know which one particular branch name was checked out at the time of the commit. this information is lost forever, because one could easily rename branches and use the same branch name on an unrelated history. – Jeff Puckett Jul 18 '18 at 03:36
5

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).

Community
  • 1
  • 1
RobinGower
  • 928
  • 6
  • 14
3

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.)

Ben Hocking
  • 7,790
  • 5
  • 37
  • 52
  • That works pretty well. I still don't know why the commits aren't automatically tagged with the branch name they are being checked into, though; that could be really helpful especially for an old commit. – Jez Feb 20 '12 at 12:25
  • I frequently put a series of commits on a local branch while I'm working on a feature, and merge that branch to trunk and push when done. So, those commits would be tagged with a branch name that didn't mean anything to anyone else and could conflict with other local branches, or even with a remote branch name. – Useless Feb 20 '12 at 12:33
1

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

todd.pierzina
  • 740
  • 4
  • 10
0

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
Jeff Puckett
  • 37,464
  • 17
  • 118
  • 167
Starkii
  • 1,149
  • 2
  • 9
  • 17