Recently I enabled diff3 and it\'s much easier to resolve conflict now.
Previously in some cases I had to check the log to see why people did this and that to do the
Why it is not the default behavior of git?
I think it's not default because git mergetool
shows 3 panels on top anyway: local, base (common ancestor) and remote + the 4th panel at the bottom, which has the content as you write in your question.
So if you turn on diff3
and use mergetool
, you then have information duplicated in the middle top panel and in the section between ||||||| and ======== in the bottom panel.
For other readers (and from this article):
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
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.
This was introduced fairly early (2008), and I supose it isn't the default, because a default unix diff isn't display as a 3-way diff.
As mentioned in this thread, if you want to run this command without setting the config, so that you can switch between normal diffs and diff3s easily, this is possible in one specific case:
If a conflict is marked in the index (i.e., the state you are in after a conflicted merge, but before you mark a path as resolved), you can do:
git checkout --conflict=diff3 <path...>
Note that that is actually checking out the index contents into the working tree, so any edits you may have made to the conflicted working tree copy will be overwritten.
Note that the |||||| merged common ancestors
will evolve with git 2.24 (Q4 2019)
See commit b657047 (07 Oct 2019), commit 8e4ec33 (01 Oct 2019), and commit 4615a8c, commit 45ef16f, commit f3081da, commit 5bf7e57, commit e95e481, commit a779fb8, commit 8599ab4, commit 7c0a6c8, commit c749ab1, commit bab5687, commit ff1bfa2, commit 4d7101e, commit 724dd76, commit 345480d, commit b4db8a2, commit 98a1d3d, commit 9822175, commit 10f751c (17 Aug 2019) by Elijah Newren (newren).
(Merged by Junio C Hamano -- gitster -- in commit 280bd44, 15 Oct 2019)
merge-recursive: provide a better label for diff3 common ancestor
Signed-off-by: Elijah Newren
In commit 7ca56aa07619 ("
merge-recursive
: add a label for ancestor", 2010-03-20, Git v1.7.1-rc0 -- merge), a label was added for the '||||||
' line to make it have the more informative heading '|||||| merged common ancestors
', with the statement:It would be nicer to use a more informative label.
Perhaps someone will provide one some day.This chosen label was perfectly reasonable when recursiveness kicks in, i.e. when there are multiple merge bases.
(I can't think of a better label in such cases.)
But it is actually somewhat misleading when there is a unique merge base or no merge base.
Change this based on the number of merge bases:
>=2: "merged common ancestors" 1: <abbreviated commit hash> 0: "<empty tree>"
Tests have also been added to check that we get the right ancestor name for each of the three cases.
With Git 2.25 (Q1 2020), "git apply --3way
" learned to honor merge.conflictStyle
configuration variable, like merges would.
See commit 091489d, commit aa76ae4, commit 9580620, commit b006968, commit fa87b81 (23 Oct 2019) by Denton Liu (Denton-L).
(Merged by Junio C Hamano -- gitster -- in commit eff313f, 10 Nov 2019)
apply: respect merge.conflictStyle in --3way
Signed-off-by: Denton Liu
Before, when doing a 3-way merge, the
merge.conflictStyle
option was not respected and the "merge
" style was always used, even if "diff3
" was specified.Call
git_xmerge_config()
at the end ofgit_apply_config()
so that the merge.conflictStyle config is read.
diff3
should be the default. It is not only useful for resolving conflicts, it makes resolving conflicts possible. It is literally impossible to correctly resolve conflicts using (only) the default merge
conflict style. I suggest to everyone to set diff3
in their global options.
git config --global merge.conflictStyle diff3
Why is it literally impossible? Consider a branch which adds a
function foo1
at a particular source file location
def foo1():
print("foo1")
and another branch which adds a function foo2
at the same location
def foo2():
print("foo2")
If I rebase one on the other I get a conflict. The default merge conflict style will show
++<<<<<<< HEAD
+def foo1():
+ print("foo1")
++=======
+ def foo2():
+ print("foo2")
++>>>>>>> Add foo2
What are the conflict markers telling me? They're telling me that I
need to add both foo1
and foo2
to the file, right?
Unfortunately not! Consider a file in which foo1
and foo2
already
exist, and two branches, one of which removes foo1
and one of which
removes foo2
. If I rebase one on the other what is the result? The
default merge conflict style will show
++<<<<<<< HEAD
+def foo1():
+ print("foo1")
++=======
+ def foo2():
+ print("foo2")
++>>>>>>> Remove foo1
Under the default conflict style the case of removing two functions is
completely indistinguishable from the case of adding two functions
(besides the text of the commit message which can only ever be a
hint)! Therefore it is insufficient for purpose of
resolving conflicts. This probably explains why resolving conflicts
is seen as a dark art. diff3
not only makes it possible, it often makes it easy.