December 13, 2014 · git

Git Merge Conflict Style Diff3

Lets take this simple file:

some  
random  
words  

Now lets say you have 2 branches. Lets say a branch called merge1 changes the first line to changed and you have a branch called merge2 that changes the second line to specific. Now you merge in merge1 however when you try to merge in merge2, you are going to get a conflict. This was a little weird to me at first since those branches changed different lines (and SVN merges this type of change fine) however in the process of investigating it, I learned about merge conflict styles with Git.

By default, you are going to see this in the file when you get the merge conflict:

<<<<<<< HEAD  
changed  
random  
=======
some  
specific  
>>>>>>> merge2
words  

Now if theses are pull requests that you didn't work on, it would look like merge2 changed the first line to some which technically didn't happen. A comment on a stackoverflow post of mine made mention of configuring the merge.conflictstyle with:

git config --global merge.conflictstyle diff3  

When I made this change, the file now looks like this when a merge conflict happens:

<<<<<<< HEAD  
changed  
random  
||||||| merged common ancestors
some  
random  
=======
some  
specific  
>>>>>>> merge2
words  

With this you can much more clearly see that there has been a change to the branch I am merging into where some is now changed and my merge is changing random to specific. I now know that I should keep line 1 as changed and line 2 as specific. The other merge conflict style is not so clear that merge 2 didn't change changed to some.


I don't know why diff3 is not the default merge.conflictstyle but I would recommend anyone using git to make this change to make dealing with conflicts easier.

comments powered by Disqus