Skip to main content

07 - Git Merge Conflicts: Reading the Markers and Closing Them Properly

Two edits to the same line stop the merge: how to read the markers, see the base with diff3, cancel with --abort and reuse your fix with rerere.

What do you think about this article?

07 - Git Merge Conflicts: Reading the Markers and Closing Them Properly
Gitfedericocalo.dev

At a Glance

Two edits to the same line stop the merge: how to read the markers, see the base with diff3, cancel with --abort and reuse your fix with rerere.

When Git stops and asks you to decide

A merge conflict happens when two branches have changed the same lines starting from the same point, and Git has no rule for choosing which version to keep. It is not a repository error: it is information. Git hands you the file with both versions inside, wrapped in three markers (<<<<<<<, =======, >>>>>>>), flags the file as unmerged, and waits for you to write the right line, run git add on it and finish with git commit. If you change your mind halfway, git merge --abort puts everything back as it was. Two tools make the work less blind: git checkout --conflict=diff3 adds to the file the original version both sides started from, and git rerere remembers your resolution so it can reapply it if the same conflict comes back.

Level: intermediate, the step from using Git alone to using it in a team. The trade-off to keep in mind: resolving by hand costs attention but shows you what each branch wanted; shortcuts such as --ours and --theirs are quick but throw one of the two versions away entirely; rerere saves time on repeated conflicts, as long as the first resolution was right.

You need the concepts of commit and branch, and if it is not clear what a merge leaves in the repository, read Git Rebase vs Merge first, the article of the series that compares the two ways of integrating work. I ran every example with Git 2.53.0 in a test repository created in a temporary folder; the commit identifiers in your repository will differ. The outputs are the real ones, with the messages Git prints in English (LC_ALL=C). In the listings, a line that starts with $ is a command and the lines below it are its output.

Provoking a conflict in a test repository

To learn to read a conflict it helps to create a small, controlled one. The listing starts from a four-line file config.txt with one initial commit. On a feature branch I change timeout from 30 to 60; on main, starting from the same commit, I change it from 30 to 45.

Neither branch knows about the other, and when I try to merge them Git stops: the command exits with code 1 and has created no merge commit.

Two changes to the same line, then the merge
$ git init -q -b main . && git config user.email demo@example.invalid && git config user.name Demo
$ printf 'host=localhost\ntimeout=30\nretry=3\nlog=info\n' > config.txt
$ git add config.txt && git commit -qm "A: initial configuration"
$ git switch -q -c feature
$ sed -i 's/^timeout=30/timeout=60/' config.txt && git commit -qam "feature: timeout to 60"
$ git switch -q main
$ sed -i 's/^timeout=30/timeout=45/' config.txt && git commit -qam "main: timeout to 45"
$ git merge feature
Auto-merging config.txt
CONFLICT (content): Merge conflict in config.txt
Automatic merge failed; fix conflicts and then commit the result.

The reason is mechanical. Git compares each branch with the common ancestor, the initial commit, and sees that the line timeout=30 changed on both, in two different ways. For lines touched by only one branch the choice is obvious and Git applies it by itself: host, retry and log are already fine.

For that single line, instead, the merge stops and writes both versions into the working file, one after the other.

Reading the markers

Open the file and look for the three special lines. Everything outside the markers has already been merged and must not be touched. Between <<<<<<< HEAD and ======= is the version from the branch you are on (here main, so timeout=45); between ======= and >>>>>>> feature is the version from the branch you are bringing in (timeout=60).

The label after the markers is the name of the commit or branch, and it helps you remember who is on which side.

The conflicted file, with markers
$ cat config.txt
host=localhost
<<<<<<< HEAD
timeout=45
=======
timeout=60
>>>>>>> feature
retry=3
log=info

One last thing to know: the markers are ordinary text inside the file, so until you remove them they are part of its content. If you leave them and commit, they end up in the repository, and a configuration file, a source file or a JSON with a <<<<<<< line stops working. To keep the situation under control, Git offers four views of the same conflict:

  • git status lists the files not yet resolved under "Unmerged paths"
  • git diff shows the conflict as a combined diff, with two columns of signs, one per parent
  • git ls-files -u prints the three versions the index keeps for every conflicted file
  • git diff --check reports forgotten markers and exits with a non-zero code

There is no fixed order among these views: you use them depending on what you need to know, and the first three tell you where the problem is while the last one checks that you have closed it. The next listing shows the status and the three versions in the index, numbered 1, 2 and 3.

The merge status and the three versions in the index
$ git status
On branch main
You have unmerged paths.
  (fix conflicts and run "git commit")
  (use "git merge --abort" to abort the merge)

Unmerged paths:
  (use "git add <file>..." to mark resolution)
	both modified:   config.txt

no changes added to commit (use "git add" and/or "git commit -a")

$ git ls-files -u
100644 df1b5502d9461ebdbd1576b8ffded35fd39c3c47 1	config.txt
100644 854850c1bb2f05cec87bc03478a559bf83f5d287 2	config.txt
100644 4176a2fb2244a0862bb350bb370da29e44f7ce0e 3	config.txt

$ git show :1:config.txt | sed -n 2p
timeout=30
$ git show :2:config.txt | sed -n 2p
timeout=45
$ git show :3:config.txt | sed -n 2p
timeout=60

Stage 1 is the common ancestor (timeout=30), 2 is "ours" (timeout=45, the branch you are on) and 3 is "theirs" (timeout=60, the branch you bring in). This is how --ours and --theirs know where to fetch the content: they read stage 2 and stage 3 of the index.

Remember, though, that during a merge "ours" is the branch you are on, while during a rebase the two roles are swapped, because your commits are replayed on top of the other branch (the git rebase manual notes this reversal of ours and theirs). For the combined diff, see the next listing: the columns with + tell you which parent each line comes from.

The combined diff of a conflicted file
$ git diff
diff --cc config.txt
index 854850c,4176a2f..0000000
--- a/config.txt
+++ b/config.txt
@@@ -1,4 -1,4 +1,8 @@@
  host=localhost
++<<<<<<< HEAD
 +timeout=45
++=======
+ timeout=60
++>>>>>>> feature
  retry=3
  log=info

Seeing the common base with diff3

The default format shows only the two sides and does not tell you what the file said before the conflict. It is a practical limit: with timeout=45 against timeout=60 you cannot tell whether someone raised the value and someone else lowered it, or whether both raised it by different amounts. The command git checkout --conflict=diff3 <file> recreates the conflict in the file, this time with an extra section, introduced by |||||||, that carries the version of the common ancestor. The command rewrites the named file from the index; I did not try it on a file already edited by hand, so if you have started resolving, make a copy first, or use it before touching the file.

The same conflict with the common base
$ git checkout --conflict=diff3 config.txt
Recreated 1 merge conflict
$ cat config.txt
host=localhost
<<<<<<< ours
timeout=45
||||||| base
timeout=30
=======
timeout=60
>>>>>>> theirs
retry=3
log=info

Now the story is readable: the base was 30, main took it to 45, feature to 60. They are not two opinions on the same value but two independent increases, and the right decision depends on which service really needs a longer timeout, not on which line looks newer.

If you prefer to always see the base, set merge.conflictStyle to diff3 or zdiff3 with git config: the default is merge, the two alternatives are the ones listed in the documentation of git merge, and zdiff3 trims from the block the lines both sides share, which would otherwise be repeated.

The price is that every conflict takes more lines and the file is longer to scroll; in my test with zdiff3 the content was the same as diff3, with different labels (HEAD, the abbreviated base, feature), because with a single conflicted line there is nothing to trim.

Resolving without leaving a wrong file

The most natural gesture, deleting the marker lines, is also the one that causes the quietest damage. The next listing starts from the file with the common base and removes the four marker lines with sed: git diff --check first exits with 2 and flags four lines, afterwards it exits with 0, and Git has nothing more to say.

But the file contains three different timeout lines, and what happens depends on the program that reads the file: one keeps the first key, one the last, another complains about the duplicate key. The check verified the absence of markers, not the correctness of the result.

Removing only the markers is not resolving
$ git diff --check
config.txt:2: leftover conflict marker
config.txt:4: leftover conflict marker
config.txt:6: leftover conflict marker
config.txt:8: leftover conflict marker
$ echo $?
2

$ sed -i '/^<<<<<<<\|^|||||||\|^=======\|^>>>>>>>/d' config.txt
$ cat config.txt
host=localhost
timeout=45
timeout=30
timeout=60
retry=3
log=info
$ git diff --check
$ echo $?
0

Resolving means writing the line you want, and it is a human decision: keep 45, keep 60, or a new value that takes both needs into account. In the next listing I write the correct file with timeout=60, mark it as resolved with git add and finish with git commit --no-edit, which accepts the merge message Git proposes.

The graph shows the diamond of an ordinary merge: from that moment the conflict is part of the history as a commit with two parents.

Writing the right line, git add and commit
$ printf 'host=localhost\ntimeout=60\nretry=3\nlog=info\n' > config.txt
$ git add config.txt && git status -s
M  config.txt
$ git commit -q --no-edit
$ git log --oneline --graph
*   11b612d Merge branch 'feature'
|\  
| * 0f55af7 feature: timeout to 60
* | 5b6e72c main: timeout to 45
|/  
* f7ffbae A: initial configuration

For the cases where the decision is already made, Git has two shortcuts, git checkout --ours <file> and git checkout --theirs <file>, which replace the whole file with the version from one of the two sides. In the listing below, --theirs restores the file with timeout=60.

It works well for files you do not really want to merge, such as a generated file or a lockfile to regenerate after the merge. But there is a risk to know: the shortcut works on the whole file, not on the single conflicted block, and if the file also held useful changes from the other branch, you discard them all.

The --theirs shortcut on the whole file
$ git checkout --theirs config.txt
Updated 1 path from the index
$ cat config.txt
host=localhost
timeout=60
retry=3
log=info
$ git add config.txt && git status -s
M  config.txt

Before closing, go through a short check: after resolving, the markers must no longer appear and the file must still work, so run git diff --check, then the program, the build or the tests the project has. The conflict was a textual merge: Git does not know whether the result makes sense, you do. These are the checks I make before git commit:

  • git diff --check to find forgotten markers, without verifying the meaning of the result
  • git diff --cached to reread what I am about to put in the merge commit, line by line, and not only the conflicted file
  • the test, the build or the start of the program, because a line can be syntactically valid and wrong in meaning
  • a search for duplicated keys or names in the file, like the triple timeout of the previous listing

None of these checks replaces the others: the first is automatic but blind to meaning, the third is the only one that verifies behaviour, and it should be done for every conflict that touches code or configuration. For documentation-only files, rereading the diff is enough.

Going back with git merge --abort

Sometimes, opening the file, you realise it is not the moment: the conflict is much larger than expected, you lack context on what the other branch changed, or you merged on the wrong branch.

git merge --abort stops the merge in progress and tries to rebuild the previous state, the one you had before running git merge. In the next listing, starting from a repository identical to the first, I start the merge, see UU config.txt in the short status, and then cancel: the status is clean again and the file has timeout=45 once more.

Cancelling a conflicted merge
$ git merge feature > /dev/null
$ git status -s
UU config.txt
$ git merge --abort
$ echo $?
0
$ git status -s
$ cat config.txt
host=localhost
timeout=45
retry=3
log=info

There is a limit stated by the documentation of git merge: if at merge time you had uncommitted changes in the working directory, --abort in some cases cannot rebuild them, and the documentation recommends committing your changes before starting.

So, before a merge, the prudent habit is to look at git status and start from a clean directory. With a clean directory the abort is safe, and you can retry whenever you like.

Not resolving twice with git rerere

If you work with long-lived branches, you may redo the same merge several times, for example because you noticed a mistake and cancelled the merge, or because you repeatedly rebase a long-lived branch. Every time the same conflict comes back identical. git rerere, whose name stands for "reuse recorded resolution", records how you resolved a conflict and reapplies it by itself next time, if the conflict is identical. The property that turns it on is rerere.enabled: you set it with git config, and by default Git treats it as active only if an rr-cache directory already exists in the repository, that is, if you used it before.

In the next listing I turn it on and redo the merge. Note the line "Recorded preimage": Git stored the conflict as it was. Then I resolve by hand and commit, and the message "Recorded resolution" confirms that your choice was saved too.

In the .git/rr-cache directory two files appear, preimage (the conflict) and postimage (your solution).

Turning on rerere and recording a resolution
$ git config rerere.enabled true
$ git merge feature
Auto-merging config.txt
CONFLICT (content): Merge conflict in config.txt
Recorded preimage for 'config.txt'
Automatic merge failed; fix conflicts and then commit the result.
$ git rerere status
config.txt
$ printf 'host=localhost\ntimeout=60\nretry=3\nlog=info\n' > config.txt
$ git add config.txt && git commit -q --no-edit
Recorded resolution for 'config.txt'.
$ ls .git/rr-cache/*
postimage
preimage

To test the reuse, I cancel the merge commit with git reset --hard HEAD~1 and redo the same merge. This time Git writes "Resolved 'config.txt' using previous resolution" and the file already has timeout=60, with no markers.

Pay attention to two details of the listing: the git merge command still exits with code 1, and the status still shows UU. Rerere rewrites the file but does not run git add for you, because it wants to leave you the chance to review the result before accepting it.

The second merge reuses the resolution
$ git reset -q --hard HEAD~1
$ git merge feature
Auto-merging config.txt
CONFLICT (content): Merge conflict in config.txt
Resolved 'config.txt' using previous resolution.
Automatic merge failed; fix conflicts and then commit the result.
$ echo $?
1
$ cat config.txt
host=localhost
timeout=60
retry=3
log=info
$ git status -s
UU config.txt

The convenience has a cost you must keep in mind. If the first resolution was wrong, rerere reapplies it with the same confidence: the mistake repeats silently, and even less visibly, because there is no conflict to look at.

That is why it is worth reviewing with git diff the file rerere wrote, and if you find the recording was wrong, git rerere forget <file> clears the stored resolution for that conflict. Rerere pays off only where the same conflict really comes back; in a flow with short branches and a single merge per branch, leave it off and you will have nothing to remember. To sum up, here is how the paths you have seen compare, each with its price:

  • resolving by hand in the editor costs attention but tells you what the two branches wanted, and it is the starting choice
  • --ours or --theirs is fast when you already know which file wins, and discards the other side entirely
  • git merge --abort costs nothing from a clean directory and postpones the decision until you have the context
  • git rerere saves work on repeated conflicts and silently propagates a wrong resolution

None of these paths is the best in absolute terms: what changes is the price you accept to pay. If you do not know where to start, start with the first, look at the base with diff3, and move to the others only when the case justifies it.

What you can do now

You can recognise why Git stopped and read the three markers: your side above, the other branch's side below, everything else already merged. You can look at the common base with --conflict=diff3 to tell whether two changes are conflicting opinions or independent increases, and choose the value with that information in hand.

You know that removing the markers is not enough, that git diff --check looks for markers but not for sense, and that --ours and --theirs discard a whole side. You can go back with git merge --abort from a clean directory, and you know when git rerere saves you time and when it hides a mistake. To move a single commit without merging a whole branch there is cherry-pick, which can itself produce conflicts you read the same way.

Frequently asked questions

What do the markers <<<<<<<, ======= and >>>>>>> mean?

They delimit the two conflicting versions: above the ======= yours, the branch you are on, below the one from the branch you are merging. Until you remove them they are text in the file.

How do I cancel a merge that hit a conflict?

With git merge --abort, which rebuilds the state before the merge. Start from a directory with no uncommitted changes, because otherwise the documentation warns that the restore may fail.

How do I see what the file said before the conflict?

With git checkout --conflict=diff3 <file>, which rewrites the file with an extra section, introduced by |||||||, holding the common ancestor's version. To always have it, set merge.conflictStyle.

When is git rerere worth it?

When you redo the same merge or rebase several times on long-lived branches. If the recorded resolution is wrong it reapplies it unchanged: fix it with git rerere forget.

Sources

  • git-merge - official Git documentation - --abort, its limit with uncommitted changes, and the merge.conflictStyle styles (merge, diff3, zdiff3); content read from the local manual with git help -m git-merge; page opened with curl on 2026-11-20 (HTTP 200), consulted on 2026-11-20
  • git-rerere - official Git documentation - recording and reuse of resolutions, status, diff, forget; content read from the local manual with git help -m git-rerere; page opened with curl on 2026-11-20 (HTTP 200), consulted on 2026-11-20
  • git-checkout - official Git documentation - --ours, --theirs and --conflict=<style>; content read from the local manual with git help -m git-checkout; page opened with curl on 2026-11-20 (HTTP 200), consulted on 2026-11-20
  • git-config - official Git documentation - the default value of rerere.enabled; content read from the local manual with git help -m git-config; page opened with curl on 2026-11-20 (HTTP 200), consulted on 2026-11-20
  • Examples run with Git 2.53.0 (git --version) in test repositories in a temporary folder, on 2026-11-19.

Try These Tools

Free developer tools related to this topic.

Commenti

Caricamento commenti...

Sign in to comment

New articles and tools on Telegram too

The Telegram channel flags the site's new articles and tools.

Did you like this article?

Explore other content on the blog or discover my projects