Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference in Git merge order?

Tags:

git

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

?

like image 256
Chin Avatar asked Feb 18 '14 23:02

Chin


People also ask

Does order matter in Git merge?

However, Git always uses a single algorithm for computing both L and R, so the order does not matter here. To put this another way, if you manage to produce a Doppelgänger file—one that has different content from, but the same hash as, some existing file, Git simply refuses to put that file into the repository.

What is the difference between fast forward merge and 3-way merge of branches?

It is called so because, in the 3-way merge, Git uses three commits to generate the merge commit; two branch tips and their common ancestor. Typically, the fast forward merge is used by developers for small features or bug fixes while the 3-way merge is reserved for integrating longer-running features.


2 Answers

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
|/  
like image 195
janos Avatar answered Sep 23 '22 17:09

janos


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.

like image 22
SzG Avatar answered Sep 23 '22 17:09

SzG