Show the original branch for a commit

Solution 1:

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.

Solution 2:

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

Solution 3:

A Git branch is nothing else than a "named pointer to a commit" (that's a fundamental different concept than in other well-known VCS).

This situation is clear, commit A is on branch-1, commit B on branch-2:

  o A [branch-1]
  |
o | B [branch-2]
| |

After merging is becomes unclear whether A (or B) originally was on branch-1 or branch-2:

o [branch-1] [branch-2]
|
o merged 
|\
| o A
| |
o | B
| |

Maybe you can guess on what Git branch the commit A was if you have tagged parent commits of A, e.g. release-1 and you know that this tag only was given for commits in branch-1.

o [branch-1] [branch-2]
|
o merged 
|\
| o A
| |
o | B
| |
| o <release-1]
| |