19

Is there any difference between

git merge c1 c2

and

git merge c2 c1

? Also, is there any difference between

git checkout c1
git merge c2

and

git checkout c2
git merge c1

?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Chin
  • 19,717
  • 37
  • 107
  • 164
  • 1
    Not sure about the first one. For the second one, it makes a difference as to which branch you merge on, if c1 and c2 are on different branches. – CompuChip Feb 18 '14 at 23:10
  • Possible duplicate of [Are merges in Git symmetric?](http://stackoverflow.com/questions/12192526/are-merges-in-git-symmetric) – Tobias Kienzler Feb 27 '17 at 09:52

2 Answers2

19

The end result in terms of the file content should be the same in all cases you described.

But there will be a difference in the DAG, in the ordering of commits in the graph of all commits, for example:

Case 1: git merge c1 c2

*   dd24250 (HEAD, master) Merge branches 'c1' and 'c2'
|\  
| * 9d09bec (c2) change in c2
* | 1f5c0ca (c1) change in c1
|/  

Case 2: git merge c2 c1

*   3256c8d (HEAD, master) Merge branches 'c2' and 'c1'
|\  
| * 1f5c0ca (c1) change in c1
* | 9d09bec (c2) change in c2
|/  

Case 3: git checkout c1; git merge c2

*   111e7da (HEAD, c1) Merge branch 'c2' into c1
|\  
| * 9d09bec (c2) change in c2
* | 1f5c0ca change in c1
|/  

Case 4: git checkout c2; git merge c1

*   8ccf531 (HEAD, c2) Merge branch 'c1' into c2
|\  
| * 1f5c0ca (c1) change in c1
* | 9d09bec change in c2
|/  
janos
  • 120,954
  • 29
  • 226
  • 236
1

I'd start with the second question.

The resulting tree objects in the merge commits will be identical. However the two commit objects won't be:

The order of the two parent commit objects will be different. Which may lead to subtle differences when referring to commits with the HEAD^1~5 notation.

The same applies to the first question:

1st case, 1st parent = current branch last commit, 2nd parent = c1 last commit, 3rd parent = c2 last commit.

2nd case, 1st parent = current branch last commit, 2nd parent = c2 last commit, 3rd parent = c1 last commit.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
SzG
  • 12,333
  • 4
  • 28
  • 41