Question

Lets say we have two branches (B and C) that have diverged from a common ancestor A. Will merging from B to C produce the same result as merging from C to B?

  A
  |
 / \
B   C

To clarify- I'm assuming that any manual merge conflict resolutions will occur in both directions. But will any automatic merges that occur result in the same code being chosen? This is what I'm assuming, since the commit dates are identical in both directions.

To further clarify - I know that the actual merging results in "mirror images" of each other based on the direction. I'm just asking about the automatically resolved conflicts.

Was it helpful?

Solution

The answer is yes for default merges. A three-way merge finds a common ancestor and then applies the differences from both sides, an operation that isn't order dependent. The topic of merge-ordering and commutativity generated a fascinating discussion on the git list (if you're into that kind of thing, that is). Note B into C and C into B should be symmetric, but the same cannot necessarily be said for (B into C) into A versus B into (C into A).

[Editing note, April 2020: If you add options like -X ours or -X theirs, the answer becomes "no", and see twalberg's answer and the others for additional caveats.]


To elaborate a bit more, based on Vince's comment below and seh's comment on the question, there will be two noticeable differences between B into C and C into B, neither of which affect the automatic merge resolution referenced in the question.

First, history will be different. The merge commit's parents will change depending on the merge order. For these examples, I'm going to use "first_branch" and "second_branch", so I can reserve letters to represent commits.

git checkout first_branch && git merge second_branch

E <- merge commit
|\
| D <- second_branch's tip
| |
| C <- another commit on second_branch 
| |
| B <- and another
|/
A <- first_branch's tip before the merge

In this case, the "first parent" of E, E^1, is first_branch's tip before the merge. second_branch is the "second parent" of the merge commit, aka E^2. Now consider the reverse:

git checkout second_branch && git merge first_branch

E <- merge commit
|\
| D <- first_branch's tip
| |
| C <- another commit on first_branch 
| |
| B <- and another
|/
A <- second_branch's tip before the merge

The parents are reversed. E^1 is the tip of second_branch before the merge. E^2 is the tip of first_branch.

Second, the display order of conflicts will reverse. In the first case, a conflict might look like this:

<<<<<<< HEAD
This line was added from the first_branch branch.
=======
This line was added from the second_branch branch.
>>>>>>> second_branch

In the second case, the same conflict would look like this:

<<<<<<< HEAD
This line was added from the second_branch branch.
=======
This line was added from the first_branch branch.
>>>>>>> first_branch

Neither of these differences affect automatic merge resolution, but they do appear when you reverse three-way merge order.

OTHER TIPS

It depends on what you mean by the "same result". From a content perspective, assuming that there are either no conflicts, or all existing conflicts are meticulously resolved in exactly the same way, the contents of the resulting new merge commit should be the same.

However, from a history topology standpoint, the two are very different. If you are on branch B and merge in C, the HEAD of B moves to point to the merge commit, but C stays where it is. Conversely, if you are on C and merge in B, C's HEAD moves, while B's stays where it is. So the final topology is very different, which has implications for future development on either branch, even though the content of the new commit is identical in either case.

no, I think it won't be symmetric. First you can set the merge strategy option, particularly in your case the theirs or ours that determines if B or C will be chosen.

In automatic merges, I think for example the remote branch has priority over your changes if you are merging into yours so it means calling git merge C from B will result in C changes being chosen. I am not 100% sure though, so you can try it your self and let us know.

The content is symmetric, but if you use GitHub or Bitbucket or your favourite local Git viewer to view the changes made at the merge, you will see that the file changes listed in the merge commit are different. (As well as the other things mentioned above.)

git checkout b
git merge c

The file changes shown in the merge will be the changes which were made on c since b and c diverged.

git checkout c
git merge b

The file changes shown in the merge will be the changes which were made on b since b and c diverged.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top