ajay.gour
ajay.gour

Reputation: 106

Git merge - Source and destination branch name identification

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.

Upvotes: 2

Views: 3022

Answers (1)

VonC
VonC

Reputation: 1326852

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.

Upvotes: 2

Related Questions