Reducing merge headaches: git meets diff3

git has an option to display merge conflicts in diff3 format (by default it only displays the two files to be merged). You can enable it like so:

$ git config --global merge.conflictstyle diff3

Now, when you have to resolve merge conflicts, git shows your side, the side being merged, and (here's what's new) the common ancestor in between them. Here's an example of the diff3-formatted output:

cauliflower
<<<<<<< HEAD
peas
potatoes
||||||| merged common ancestors
peas
=======
>>>>>>> topic
tomatoes

Having the merge ancestor readily available helps you to quickly determine what the correct merge is, since you can infer from it the changes that were made on both sides. Here you can see that the original state was peas. On your branch potatoes was added (compare the middle section to the top) and on the other branch peas was removed (compare the middle section to the bottom). Therefore the correct change is to both add potatoes and remove peas, leaving you with just potatoes in the conflicted section.

There's really no reason you shouldn't enable the diff3 style, because you frequently need the ancestor to determine what the correct merge is.

To see that this is true, even in the simple example above, look at what the conflict looks like under the standard style:

cauliflower
<<<<<<< HEAD
peas
potatoes
=======
>>>>>>> topic
tomatoes

There's an asymmetry between peas and potatoes: one was added and one was deleted, but this merge conflict doesn't tell you anything at all about which was which! You can't determine the correct merge unless you remember the sequence of changes that led up to this point. And why should you have to rack your brain to do that? That's exactly the sort of thing that your computer can, and should, help you with.

Bonus tip: rerere (reuse recorded resolution)

If your workflow finds you redoing the same merges over and over again you might also find git's rerere (reuse recorded resolution) feature to be useful.

One of the things that is wonderful about rerere is that it provides hardly any UI surface at all. Just set it...

$ git config --global rerere.enabled 1

...and forget it. Although there is a git rerere command, you can get a lot done without using it at all.

After enabling rerere, whenever you resolve a merge conflict, git automatically squirrels away the resolution in its database. You'll see a message like this one:

$ git commit
Recorded resolution for 'soup'
[...]

And the next time you encounter the same conflict, where you would have expected git to spit out a file with conflict markers, you will instead find that it has automatically resolved the merge for you, and printed the following message:

$ git merge topic
Auto-merging soup
CONFLICT (content): Merge conflict in soup
Resolved 'soup' using previous resolution.

Just double-check to make sure nothing has gone awry, add, and commit. Save your blood, sweat, and tears for other, more interesting problems than redoing merges.

Further reading: Pro Git on rerere

52 comments:

  1. Love the diff3 format. I can't see why anyone would not want to use it. Thanks for posting!

    ReplyDelete
  2. I also love diff3. Why the heck is that not the default?

    Thanks for the tip on rerere. It sounds too good to be true.

    ReplyDelete
  3. Awesome! I did not know about the diff3 option, yet always wanted it! Can't believe I didn't look for it. Thanks for writing this up!

    ReplyDelete
  4. My two cents on why it's not the default is because it's not clear at first how it works. I mean, what "common ancestor" represents. I needed this blog to understand the concept. Thanks for that.

    ReplyDelete
  5. Look at style #3 that BitKeeper could use:
    https://www.bitkeeper.org/man/smerge.html

    That requires a bit more editing, but the information is _much_ clearer.

    ReplyDelete
  6. I really fell happy to read this post thanks for sharing this very good
    THC Diamonds

    ReplyDelete

  7. Im grateful for the article post.Really thank you! Will read on

    ReplyDelete
  8. Goodness, cool post. I’d prefer to compose like this as well – requiring significant investment and genuine difficult work to make an incredible article…

    ReplyDelete
  9. Silicon carbide and aluminium oxide are the major impregnating abrasive particles of sandpaper. sandpaper grit

    ReplyDelete
  10. thanks for your marvelous posting! I really enjoyed reading it, you happen to be a great author.

    ReplyDelete

  11. I am glad to be a visitor of this complete site! , thanks for this rare info!

    ReplyDelete
  12. Cool you write, the information is very good and interesting,

    ReplyDelete
  13. I like the helpful information that provide in articles. Keep doing it.

    ReplyDelete
  14. This blog is very informative the stuff you provide I really enjoyed reading

    ReplyDelete
  15. thank you for sharing this amazing piece of information

    ReplyDelete
  16. I do agree with all of the ideas you’ve presented in your post.

    ReplyDelete
  17. The post was really very good. Thanks for sharing.

    ReplyDelete
  18. I love seeing blog that understand the value of providing a quality resource for free.

    ReplyDelete
  19. Just the website I was looking for. It’s really awesome.

    ReplyDelete
  20. Excellent website you have here, so much cool information!

    ReplyDelete


  21. your views are in accordance with my own for the most part

    ReplyDelete

  22. I am really grateful for your blog post for giving a lot of information

    ReplyDelete
  23. Hard to ignore such an amazing article like this.

    ReplyDelete
  24. I had a lot of fun at this Olympics, but something was missing.

    ReplyDelete
  25. Very nice blog and articles. Thank you

    ReplyDelete
  26. Now I am found which I actually want. Thank you.

    ReplyDelete
  27. I appreciate you spending some time and energy to put this content together.

    ReplyDelete
  28. I read your article very impressively. I want to write something like this.

    ReplyDelete
  29. I'm happy to see some great articles on your site. Good topic.

    ReplyDelete
  30. It’s really pleasure to read your post. Thank you so much for writing such a nice post

    ReplyDelete
  31. Great web site! It looks really expert! Maintain the helpful job!

    ReplyDelete

  32. I was really amazed reading this

    ReplyDelete


  33. Thank you for the good story. Considered a great and very useful content

    ReplyDelete

  34. I’m glad that you simply shared this useful info with us

    ReplyDelete

  35. I did a Google search about the topic and found most people will believe your blog

    ReplyDelete

  36. Hello there, just became alert to your blog through Google, and found that it’s truly informative

    ReplyDelete


  37. Wonderful article. I love your style of writing. It’s inspiring , and I am happy with your writing style

    ReplyDelete

  38. Wonderful web site. Plenty of useful info here.

    ReplyDelete

  39. Informative article, exactly what I wanted to find

    ReplyDelete

  40. Really a great addition. I have read this marvelous post

    ReplyDelete

  41. Nice Blog. Thanks for sharing with us. Such amazing information.

    ReplyDelete

  42. Very nice write-up. I certainly love this website. Keep writing!

    ReplyDelete

  43. Thanks for the info I will try to figure it out for more

    ReplyDelete

  44. I'm extremely impressed with your writing skills as well as with the layout on your blog.

    ReplyDelete

  45. Excellent article. I definitely love this website. Keep writing!

    ReplyDelete

  46. I like the valuable information you supply for your articles

    ReplyDelete

  47. Perfect just what I was looking for!

    ReplyDelete


  48. thanks for sharing great blog article thanks again

    ReplyDelete