2

Have a question on git merging, lets say I have 2 branches - B1 and B2, wherein B2 is created from B1. Development is going on parallel in both branches, as described below - commits "6, 7, 8" belongs to branch "B2" and "1, 2, 3, 4, 5, 9, 10" belongs to branch "B1" where commit "10" is a merged commit.

            / - 6 - 7 - 8 - \ 
----1 - 2 - 3 - 4 - 5 - 9 - 10 (Merged commit)

My question is - can commit "10", somehow, tell me that source branch (the one which is merged) is B2 and destination (the one B2 merged into) is B1?

I looked into "git show" options but got nothing.

I understand that using "git log --graph" and gitk I can manually check but it becomes extremely difficult when there are large number of commits and even more difficult if there are fast forward merges and a lot of merging activities in the repository. Commit comments is another option but cannot really rely on it. So I am wondering if "git show" or any other git command could provide this information.

Schleis
  • 41,516
  • 7
  • 68
  • 87
ajay.gour
  • 106
  • 1
  • 7

1 Answers1

2

As mentioned in this answer:

git show --format="%P" <SHA>

In the case of most merges, the SHA of the branch that was checked out will be the first parent, and the SHA of the branch that was merged in will be the second.

Basically, the checked out branch is always the first parent; the other parents are the branches that were specified to the merge command in the order of their specification.

So for each SHA1, you can check which branch they belong to: "How to list branches that contain a given commit?", and get back B1 (destination), then B2 (merged) that way.

That being said, remember that branches are transient in git: they can be renamed or moved at any time.

Community
  • 1
  • 1
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • Thanks VonC for the super quick response. "git branch -a --contains SHA" lists all the branches which are reachable. Wonder if it is possible to display specific branch that gotten merged into checked out branch. – ajay.gour May 13 '14 at 14:58
  • @ajay.gour only by doing `git branch -a --contains SHA` for the second SHA1 returned by `git show --format="%P" `: that second SHA1 is the branch which has been merged into the target branch. – VonC May 13 '14 at 15:31
  • Thanks again. Appreciate your response. – ajay.gour May 15 '14 at 13:33