Are merges in Git symmetric?

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.


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.


The content of the files after the merge is 'symmetric' (the same, whichever merge you do), 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 shown in the merge commit are different depending on which merge you did (in addition to the other differences mentioned above, and in my edit below):

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.


EDIT:

Another difference is that if you later do git reset [--hard] HEAD~{n} to roll back by a number of commits, and this goes back past the merge point, then the branch it goes back down is different depending on which merge you did, and is always the branch you merged 'into' (the one checked out, in the examples above).