I\'ve used git-blame to find a particular commit. Now I want to find the branch that it originally came from. (From there, I\'ll use the branch name to find the particular t
That isn't really applicable in git. Branches are local concepts to each repository: one person's "local-stuff" branch can be quite separate from another person's "local-stuff" branch. If you do something like looking at your main integration branch, and your query commit, and removing all the merge bases between the two, you should be able to get a subtree of the commit history that may shed some illumination... or may not. e.g. if you trace up the link from the query commit towards "master" you should hopefully find merge commits with useful comments saying where the merge came from... but this information is just informational, not recorded in some way intended to be automatically retrieved.
e.g. gitk some-commit...master
(which is almost short for gitk some-commit master --not $(git merge-base some-commit master)
)
Like the others said, if the branch you are looking for isn't local to the repository on which you are blaming this commit (e.g. a branch only in the personal repo of a distant developer), you are screwed.
But assuming that sought-after branch is something you can see, and that of course you have the commit's hash, say d590f2...
, a partial answer is that you can do :
$ git branch --contains d590f2
tests
* master
Then, just to confirm you have the culprit:
$ git rev-list tests | grep d590f2
Of course, if d590f2
has been merged in more than one branch, you will have to be more subtle than this.
I found a simpler way to do it: it's in the message of the last commit of git log <sha>..HEAD --merges
!
This command shows the merges that have happened between master and the commit; the last commit output by this command is the first merge commit that included it. It usually contains the branch name, so even if the branch was deleted you can find its name.
To get only the name of the branch just type git log <sha>..HEAD --merges --oneline |tail -1