41

So, I am trying to find branch name in which a given commit was made. (c853d8cf3ae34dae9866b874b96c6529515d7e90)

I have the parent id and commit id referenced on the git issue.
How can I find that given commit id was pushed in 'x' branch?

PriyankaK
  • 925
  • 2
  • 10
  • 18
  • 3
    possible duplicate of [Git: Finding what branch a commit came from](http://stackoverflow.com/questions/2706797/git-finding-what-branch-a-commit-came-from) – Tuxdude Mar 26 '13 at 21:12
  • 1
    GitHub has now as specific way of showing the branch from a commit. See my edit. – VonC Apr 02 '13 at 06:33

2 Answers2

39

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.

Link to pull request

Once the commit makes it to the default branch, any tags that contain the commit will be shown, and the default branch will be the only branch listed.

branch part of commit


Original answer

You can list those branches:

git branch --contains <commit>

# in your case
git branch --contains <commit> | grep x

More details in "Git: Finding what branch a commit came from".

Don't forget that a commit can be part of several branches.

Community
  • 1
  • 1
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • 1
    Thanks for the update VonC.The new feature is pretty cool. Finally the feature I always wanted on Git. – PriyankaK Apr 02 '13 at 17:41
  • 2
    If you want to use it programatically, these options might be helpful: `git branch --no-color --no-column --format "%(refname:lstrip=2)" --contains ` – udondan Dec 13 '18 at 13:00
  • 1
    Do note that the use of `--format` in the above mentioned tip (as sweet as it is) is only available in git v2.13.2 and later. – Lester Peabody Apr 10 '19 at 16:07
  • what if there is no branch name there, probably got delete, how can I checkout to it locally? – jangorecki Dec 23 '19 at 12:16
  • 1
    @jangorecki you can create a new branch, starting from the commit id. `git switch -c newBranch `, using the new `git switch` command: https://stackoverflow.com/a/57066202/6309 – VonC Dec 23 '19 at 12:22
6

Just a cheat.

You can see the branch on the commit detail page of Github like the following:

https://github.com/{github_username}/{repo}/commit/{commit_id}

Ex: https://github.com/chungth/Laravel-5.2-Bootstrap3-starter-site/commit/f81c13234a9da988613e14e52f752abec5f51997

You can see the current branch name on that page.

Alexis Wilke
  • 19,179
  • 10
  • 84
  • 156
Chung
  • 947
  • 1
  • 12
  • 22