85

Suppose I'm on Git branch master and I git merge featurebranch. There is a conflict in foo.html.

When I open foo.html, I see, in the area of the conflict, what master has and what featurebranch has. But I can't really tell what change was made on master that conflicted with featurebranch; I only know what master has now.

I'd like to see the diff that each one applied.

Or, to get the same information, I could see:

  • The version master has now
  • The version featurebranch has now
  • The version their common ancestor had

How can I see this?

Nathan Long
  • 122,748
  • 97
  • 336
  • 451

2 Answers2

124

From git-merge(1),

An alternative style can be used by setting the "merge.conflictstyle" configuration variable to "diff3".

In addition to the <<<<<<<, =======, and >>>>>>> markers, it uses another ||||||| marker that is followed by the original text. ... You can sometimes come up with a better resolution by viewing the original.

This can be enabled using

git config --global merge.conflictstyle diff3

or right in ~/.gitconfigfile

[merge]
  conflictstyle = diff3
RazerM
  • 5,128
  • 2
  • 25
  • 34
  • 7
    That is fantastic! Thank you! In the meantime I had discovered `git mergetool`, which (for me) opens `vimdiff` with several windows showing similar info, but I like your method better. – Nathan Long Aug 08 '13 at 16:52
  • 2
    @NathanLong: btw, `mergetool` is really nice when using `kdiff3` – Hasturkun Aug 08 '13 at 16:55
11

A lot of GUI diff/merge tools have a 3 or 4 way merge view. I highly recommend Beyond Compare for resolving merge conflicts. Another (okay) tool is DiffMerge.

You can set up a custom mergetool to use with the git mergetool command. This is my .gitconfig configuration for Beyond Compare (Pro edition) and DiffMerge on my Windows machine using msysgit:

[merge]
    tool = bc3
[diff]
    tool = bc3
[difftool "dm"]
    cmd = C:/Program\\ Files/SourceGear/Common/DiffMerge/sgdm.exe \"$LOCAL\" \"$REMOTE\"
[difftool "bc3"]
    cmd = "\"c:/program files (x86)/beyond compare 3/bcomp.exe\" \"$LOCAL\" \"$REMOTE\""
[mergetool "bc3"]
    cmd = "\"c:/program files (x86)/beyond compare 3/bcomp.exe\" \"$LOCAL\" \"$REMOTE\" \"$BASE\" \"$MERGED\""

You can read more about various diff/merge tool configurations in the official Linux Kernel Git documentation for git config.

  • 1
    +1 for Beyond Compare! I've used the Pro Edition for years and love it. Their support is also excellent. – TrueWill Aug 08 '13 at 17:17
  • The 3-way merge screenshot for Beyond Compare has the same window layout as I got in `vimdiff`. – Nathan Long Aug 08 '13 at 17:35
  • 3
    I would greatly appreciate an explanation for any downvotes to this answer. Considering that I provide working configurations for some [popular external diff and merge tools](http://www.codinghorror.com/blog/2005/11/in-praise-of-beyond-compare.html) for msysgit installations on Windows, I think my answer provides great value. –  Aug 12 '13 at 08:27
  • 4
    For other's that got here and have been wondering why the above config doesn't work for 3-way merges, check that you have a Pro version of Beyond Compare and not a Standard license (which doesn't support 3-way-merge) – nivekastoreth Apr 26 '14 at 23:16
  • 1
    Thanks @nivekastoreth. It was doing a 2-way merge for me, no matter what I did. I was confused by the [Standard vs Pro](http://scootersoftware.com/shop.php?zz=kb_editions) page and mistakenly thought Starndard included 3-way file merges, but not directories. Upgrading to pro and [following directions](http://www.scootersoftware.com/support.php?zz=kb_vcs#gitlinux) instantly fixed it up. – Tyler Collier May 25 '15 at 22:16
  • I ran into this issue again, and thought maybe it was a bug in BC where it forgot my license or something (even though Help>About shows my Pro license). When I did `git mergetool`, it showed only the `LOCAL` and `REMOTE` and not the `BASE` in BC. I don't understand why, but it turns out that BC indeed had a 3rd/middle column, but it wasn't showing it because the filename was blank. I think it was blank because the BASE file that should have been there had a 0 file size. – Tyler Collier Dec 12 '16 at 18:45
  • 3
    I feel like this answer was only a promote to Beyond Compare which btw is not so good – amdev Jan 28 '19 at 13:12