Is merging branch A into B same as merging B into A?

There is a concept called first parent when the merge generate a new merge commit.
You can check the parents of a merge commit by command git show merge-commit-hash, replace the merge-commit-hash with the real commit hash.
And you will get something like following:

commit [commit]
Merge: [parent1] [parent2]

Assume you have following commits history, C1 is the parent of C2 and C3. And branch A points to C2, branch B points to C3.

C1<--C2
\<--C3

When you merge branch A(C2) into branch B(C3), the first parent commit would be C3.
When you merge branch B(C3) into branch A(C2), the first parent commit would be C2.

You can check it with my demo project. And you can check the log graph with TortoiseGit.
Walk Behaviour → First Parent just follow up first parent commit. This will help understand overwhole history.


No, if you merge A into B, then in the end branch A will only have A's changes, and B will have both A+B changes.

If you merge B into A, then A with have both A+B changes, and B will only have B's changes.

Start:

 /A1-A2-A3
X 
 \B1-B2-B3

A to B with a merge commit:

 /A1-A2-A3
X 
 \B1-B2-B3-A*(1,2,3)

A to B with fast forward:

 /A1-A2-A3
X 
 \A1-A2-A3-B1'-B2'-B3'

B to A with merge commit:

 /A1-A2-A3-B*(B1,B2,B3)
X 
 \B1-B2-B3

B to A with fast forward:

 /B1-B2-B3-A1'-A2'-A3'
X 
 \B1-B2-B3

Tags:

Git