How to find the branch from commit id
Solution 1
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.
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.
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.
Solution 2
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}
You can see the current branch name on that page.
PriyankaK
Updated on July 14, 2022Comments
-
PriyankaK over 1 year
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 over 10 yearsThanks for the update VonC.The new feature is pretty cool. Finally the feature I always wanted on Git.
-
ginna over 7 yearsSuper useful. Thank you!!
-
udondan almost 5 yearsIf you want to use it programatically, these options might be helpful:
git branch --no-color --no-column --format "%(refname:lstrip=2)" --contains <commit>
-
Lester Peabody over 4 yearsDo 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. -
jangorecki almost 4 yearswhat if there is no branch name there, probably got delete, how can I checkout to it locally?
-
VonC almost 4 years@jangorecki you can create a new branch, starting from the commit id.
git switch -c newBranch <commitID>
, using the newgit switch
command: stackoverflow.com/a/57066202/6309