Git markers started showing up when resolving conflicts with VS2019 + KDiff3

467 views Asked by At

Today I tried to merge two branches and I ran into a conflict. I use the Visual Studio 2019 Team Explorer, and .gitconfig is set-up like this to launch KDiff3:

[merge]
    tool = kdiff3
[mergetool "kdiff3"]
    path = C:/Program Files/KDiff3/bin/kdiff3.exe
    trustExitCode = false

This has worked great until now, but today there were Git's conflict markers in the base version of the file. Something like this:

Common code
<<<<<<<<< Temporary merge branch 1
One version of code
=========
Another version of code
>>>>>>>>> Temporary merge branch 2
More common code

This never happened before. Aren't those markers only meant for merging manually using text files? Any idea on how to fix this?

EDIT:

Also, using git directly from command line has the same markers, but I have no idea if those were there before because I almost never use the command line. This is the command I used:

git mergetool -g FileName.cs
2

There are 2 answers

5
prosoitos On

Those markers are added by Git whenever there is a merge conflict. It is Git's way to make the location(s) of the conflict(s) easy to spot (by a human or a merge tool) and to show the alternative versions.

Merge tools detect these markers and make it easier for the user to compare versions and choose the one they prefer. GUI tools such as KDiff3 usually hide the markers and present the alternative versions in different panes. Once the user has resolved the conflict, the merge tool deletes the lines with the markers.

Other tools allow to easily jump from conflict to conflict, highlight the alternative versions in different colors, and have keybindings to easily resolve the conflict, but they do not hide those Git conflict markers from the user (e.g. ediff).

In any event, whether you see them or not (depending on the merge tool you use), they are there until the conflict gets resolved.

I don't know what went wrong this time with KDiff3, but it is very easy to fix manually:

You can simply open the file with any text editor, delete the lines

<<<<<<<<< Temporary merge branch 1
=========
>>>>>>>>> Temporary merge branch 2

and choose either One version of code, Another version of code, or a combination of the two (or something else entirely).

Once you are done, save the file, run:

git add <file>

(this tells Git that you have resolved the conflict), then:

git commit

You don't have to enter a commit message because Git will automatically offer you a merge commit message (that you can accept or modify).

Et voilĂ .

6
relatively_random On

Turns out this doesn't happen on normal conflicts, this was just a case of a convoluted tree of merges. For normal merge conflicts with an obvious single common ancestor, git generates three simple files for merge tools to look at. When you have multiple common ancestors for a conflict, it leaves in some markers in the "base" file.