Jez
Jez

Reputation: 30071

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?

Upvotes: 24

Views: 7841

Answers (6)

PriyankaK
PriyankaK

Reputation: 975

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

Upvotes: 11

Starkii
Starkii

Reputation: 1229

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

Upvotes: 1

todd.pierzina
todd.pierzina

Reputation: 730

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

Upvotes: 1

Jez
Jez

Reputation: 30071

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.

Upvotes: 12

RobinGower
RobinGower

Reputation: 958

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

Upvotes: 5

Ben Hocking
Ben Hocking

Reputation: 8092

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

Upvotes: 3

Related Questions