Resolving Merge Conflicts: Conflict Markers, ours and theirs, Merge Tools and rerere
What a merge conflict is and how to resolve one calmly: reading conflict markers and the zdiff3 style, the three stages, git status and diff during a conflict, taking one side safely, modify/delete and binary conflicts, aborting and redoing, merge tools, rerere, and checking a resolution.
kvi5hnuCode Sprout team member11 min readOn this page
When two branches change the same lines of a file in different ways, Git can't know which version is right, so it stops the merge and asks you. That's a merge conflict. It isn't an error or a sign that something broke: it's Git refusing to guess. Resolving one means deciding what the lines should say, telling Git, and finishing the merge.
Everything here also applies to conflicts during git rebase, git cherry-pick, git revert and git stash pop, which use the same machinery, with one twist about "ours" and "theirs" covered below.
Anatomy of a conflict
Git compares three versions of each file: the merge base (where the branches split), ours (the current branch, HEAD) and theirs (the branch being merged). A change made on only one side is taken automatically. A conflict happens only where both sides changed the same area:

Here's that conflict happening:
git init -q trail-guide && cd trail-guide
printf 'Ridge loop\nDistance: 8 km\nClimb: 240 m\nParking: free\n' > ridge.txt
git add . && git commit -q -m "Add ridge notes"
git switch -q -c ana
sed -i 's/8 km/8.5 km/' ridge.txt; git commit -q -am "Correct distance"
git switch -q main
sed -i 's/8 km/9 km/; s/free/paid/' ridge.txt; git commit -q -am "Measure distance again, parking now paid"
git merge ana
echo "exit status: $?"
echo "=== git status"
git status
echo "=== the file now"
cat ridge.txt
Auto-merging ridge.txt
CONFLICT (content): Merge conflict in ridge.txt
Automatic merge failed; fix conflicts and then commit the result.
exit status: 1
=== 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: ridge.txt
no changes added to commit (use "git add" and/or "git commit -a")
=== the file now
Ridge loop
<<<<<<< HEAD
Distance: 9 km
=======
Distance: 8.5 km
>>>>>>> ana
Climb: 240 m
Parking: paid
Git merged what it could, Parking: paid, and wrote both versions of the Distance line between conflict markers:
<<<<<<< HEADstarts our version.=======separates the two.>>>>>>> anaends their version.
git status lists the file under Unmerged paths as both modified, and the merge is paused: nothing is committed until you finish.
Resolving: edit, add, continue
- Edit each conflicted file so it says what it should. That may be one side, the other, or a combination. Delete all the marker lines.
git addeach resolved file. That marks it as resolved.git merge --continue, orgit commit, to create the merge commit.
git init -q trail-guide && cd trail-guide
printf 'Ridge loop\nDistance: 8 km\nClimb: 240 m\nParking: free\n' > ridge.txt
git add . && git commit -q -m "Add ridge notes"
git switch -q -c ana
sed -i 's/8 km/8.5 km/' ridge.txt; git commit -q -am "Correct distance"
git switch -q main
sed -i 's/8 km/9 km/; s/free/paid/' ridge.txt; git commit -q -am "Measure distance again, parking now paid"
git merge -q ana
# Ana's measurement is the right one: write the resolved file
printf 'Ridge loop\nDistance: 8.5 km\nClimb: 240 m\nParking: paid\n' > ridge.txt
git add ridge.txt
git status -s
git merge --continue
git log --oneline --graph
Auto-merging ridge.txt
CONFLICT (content): Merge conflict in ridge.txt
Automatic merge failed; fix conflicts and then commit the result.
M ridge.txt
[main 1014874] Merge branch 'ana'
* 1014874 Merge branch 'ana'
|\
| * 6d444be Correct distance
* | 94a0b4c Measure distance again, parking now paid
|/
* 993bf1c Add ridge notes
git merge --continue opened the editor for the merge message, which is accepted as-is in the sandbox. In real life, the default Merge branch 'ana' message is fine. Adding a line about how a tricky conflict was resolved helps future readers.
Before adding, search for leftover markers. git diff --check reports lines that still look like conflict markers, and so does a quick grep -n '^<<<<<<<\|^=======\|^>>>>>>>' file. Committing a file with markers in it is a classic mistake, and it often breaks the build.
See the base: the zdiff3 style
The default markers show only ours and theirs, so you can't tell what each side changed. Was the original 8 km? Did one side just reformat? Setting merge.conflictStyle to zdiff3 adds the base version between ||||||| and =======:
git config --global merge.conflictStyle zdiff3
git init -q trail-guide && cd trail-guide
git config merge.conflictStyle zdiff3
printf 'Ridge loop\nDistance: 8 km\nClimb: 240 m\nParking: free\n' > ridge.txt
git add . && git commit -q -m "Add ridge notes"
git switch -q -c ana
sed -i 's/8 km/8.5 km/' ridge.txt; git commit -q -am "Correct distance"
git switch -q main
sed -i 's/8 km/9 km/; s/free/paid/' ridge.txt; git commit -q -am "Measure distance again, parking now paid"
git merge -q ana
cat ridge.txt
Auto-merging ridge.txt
CONFLICT (content): Merge conflict in ridge.txt
Automatic merge failed; fix conflicts and then commit the result.
Ridge loop
<<<<<<< HEAD
Distance: 9 km
||||||| 993bf1c
Distance: 8 km
=======
Distance: 8.5 km
>>>>>>> ana
Climb: 240 m
Parking: paid
The ||||||| line is labelled with the merge base's commit ID, and below it you can see that the original said 8 km and both sides changed the number. diff3 is the older style with the same idea. zdiff3, available since Git 2.35, also moves lines that are identical on both sides out of the conflict block, making it smaller. Many experienced developers consider zdiff3 the single most useful conflict setting.
To redraw a file's conflict in another style, or to restore the markers after a botched edit, use git checkout --conflict=zdiff3 <file> (or --conflict=merge for the default style).
Inspecting a conflict
While a merge is paused, several commands help you understand it:
git init -q trail-guide && cd trail-guide
printf 'Ridge loop\nDistance: 8 km\nClimb: 240 m\nParking: free\n' > ridge.txt
git add . && git commit -q -m "Add ridge notes"
git switch -q -c ana
sed -i 's/8 km/8.5 km/' ridge.txt; git commit -q -am "Correct distance"
git switch -q main
sed -i 's/8 km/9 km/; s/free/paid/' ridge.txt; git commit -q -am "Measure distance again, parking now paid"
git merge -q ana
echo "=== conflicted files only"
git diff --name-only --diff-filter=U
echo "=== the three stages in the staging area"
git ls-files -u
echo "=== line 2 of each stage: base, ours, theirs"
for n in 1 2 3; do git show :$n:ridge.txt | sed -n 2p; done
echo "=== combined diff of the working file against both sides"
git diff
echo "=== commits on either side that touched the conflicted files"
git log --merge --oneline
Auto-merging ridge.txt
CONFLICT (content): Merge conflict in ridge.txt
Automatic merge failed; fix conflicts and then commit the result.
=== conflicted files only
ridge.txt
=== the three stages in the staging area
100644 e28e5b9d16b7b269b52e636a66a92341950d5c95 1 ridge.txt
100644 8007c347e3604a8a67b9f718ea35ac54729bd4dc 2 ridge.txt
100644 04e2249b8b9472150c29e73f5a9b2148dcee337e 3 ridge.txt
=== line 2 of each stage: base, ours, theirs
Distance: 8 km
Distance: 9 km
Distance: 8.5 km
=== combined diff of the working file against both sides
diff --cc ridge.txt
index 8007c34,04e2249..0000000
--- a/ridge.txt
+++ b/ridge.txt
@@@ -1,4 -1,4 +1,8 @@@
Ridge loop
++<<<<<<< HEAD
+Distance: 9 km
++=======
+ Distance: 8.5 km
++>>>>>>> ana
Climb: 240 m
-Parking: free
+Parking: paid
=== commits on either side that touched the conflicted files
94a0b4c Measure distance again, parking now paid
6d444be Correct distance
- During a conflict, the staging area holds three versions of the file: stage 1 is the base, stage 2 is ours, stage 3 is theirs.
git show :2:ridge.txtprints our version, and so on. Once yougit addthe file, the three stages are replaced by one normal entry. git diffshows a combined diff with two columns of+/-: the first column compares with ours, the second with theirs.git diff --oursandgit diff --theirscompare with just one side, andgit diff --basewith the base.git log --mergelists the commits on both sides that changed the conflicted files: a quick way to read why each side changed the line. Add-pto see the changes.
Taking one side, carefully
Sometimes one side is simply right for a whole file. Git can write either side's version:
git checkout --ours ridge.txt # our version of the file
git checkout --theirs ridge.txt # their version of the file
git restore --ours ridge.txt # the same with restore (Git 2.23)
Careful: these take that side's entire file, not just its side of the conflicting lines. Every change the other side made to the file is lost, even changes that merged cleanly:
git init -q trail-guide && cd trail-guide
printf 'Ridge loop\nDistance: 8 km\nClimb: 240 m\nParking: free\n' > ridge.txt
git add . && git commit -q -m "Add ridge notes"
git switch -q -c ana
sed -i 's/8 km/8.5 km/' ridge.txt; git commit -q -am "Correct distance"
git switch -q main
sed -i 's/8 km/9 km/; s/free/paid/' ridge.txt; git commit -q -am "Measure distance again, parking now paid"
git merge -q ana
git checkout --theirs ridge.txt
echo "=== Parking: paid, our non-conflicting change, is gone"
cat ridge.txt
echo "=== get the markers back with checkout -m"
git checkout -m ridge.txt
cat ridge.txt
Auto-merging ridge.txt
CONFLICT (content): Merge conflict in ridge.txt
Automatic merge failed; fix conflicts and then commit the result.
Updated 1 path from the index
=== Parking: paid, our non-conflicting change, is gone
Ridge loop
Distance: 8.5 km
Climb: 240 m
Parking: free
=== get the markers back with checkout -m
Recreated 1 merge conflict
Ridge loop
<<<<<<< ours
Distance: 9 km
=======
Distance: 8.5 km
>>>>>>> theirs
Climb: 240 m
Parking: paid
git checkout -m <file> (or --conflict=...) recreates the conflicted version with markers, so a wrong choice can be redone before you git add.
To keep all clean changes and take one side only where lines conflict, redo the merge with a strategy option: git merge --abort, then git merge -X theirs ana (or -X ours). Article 12 explains these. Use them only when you're sure one side should win every conflict.
"Ours" and "theirs" are swapped in a rebase
In a merge, "ours" is the branch you're on and "theirs" is the branch being merged. During a rebase, Git replays your commits onto the other branch, so HEAD is the other branch and your commits are the ones being applied: --ours means the branch you're rebasing onto, and --theirs means your own commits. It's a common source of confusion, so check with git status and the marker labels. Article 14 covers rebase conflicts.
Other kinds of conflict
Not every conflict has markers:
git init -q trail-guide && cd trail-guide
echo "Old trail notes" > old.txt
printf '\x89PNG-v1' > logo.png
git add . && git commit -q -m "Start project"
git switch -q -c cleanup
git rm -q old.txt
printf '\x89PNG-v2' > logo.png
git commit -q -am "Remove old notes, new logo"
git switch -q main
echo "One more note" >> old.txt
printf '\x89PNG-v3' > logo.png
git commit -q -am "Extend old notes, another logo"
git merge cleanup
echo "=== short status: UD = we updated, they deleted; UU = both changed"
git status -s
git rm -q old.txt # agree with the deletion
git checkout --theirs logo.png # take their logo
git add logo.png
git commit -q --no-edit
git status -s
git log --oneline -1
Auto-merging logo.png
CONFLICT (content): Merge conflict in logo.png
CONFLICT (modify/delete): old.txt deleted in cleanup and modified in HEAD. Version HEAD of old.txt left in tree.
Automatic merge failed; fix conflicts and then commit the result.
=== short status: UD = we updated, they deleted; UU = both changed
UU logo.png
UD old.txt
Updated 1 path from the index
4a61bc9 Merge branch 'cleanup'
| Conflict | What happened | How to resolve |
|---|---|---|
modify/delete (UD, DU) | one side edited a file, the other deleted it | git add it to keep it, or git rm it to accept the deletion |
| binary | both sides changed a binary file, so no line merge is possible | pick a version with --ours or --theirs, or put the right file in place, then git add |
| rename/rename, rename/delete | one side renamed a file that the other renamed differently or deleted | git status explains. Keep the right name with git mv/git rm, then git add |
add/add (AA) | both sides created a file with the same name | resolve like an ordinary content conflict |
The two-letter codes in git status -s describe each side: U updated, D deleted, A added. UU is the usual both-modified conflict.
Stopping, or starting again
git merge --abortcancels the merge and returns to the state before it. Your branch is untouched. Uncommitted changes from before the merge may not be recoverable, which is why you should start merges with a clean working tree.- To redo just one file:
git checkout -m <file>restores its conflicted state. - Abort is also how you change strategy: abort, then merge again with
-X ...or after rebasing.
Merge tools
A merge tool shows the three versions side by side, and often the result below, so you can pick changes with a click. git mergetool opens each conflicted file in the configured tool in turn:
git config --global merge.tool vscode
git config --global mergetool.vscode.cmd 'code --wait --merge $REMOTE $LOCAL $BASE $MERGED'
git mergetool
git mergetool --tool-help lists the tools Git knows, such as vimdiff, meld, kdiff3, bc (Beyond Compare), p4merge and vscode, and which are installed. For tools it doesn't know, set mergetool.<name>.cmd with the variables $BASE, $LOCAL (ours), $REMOTE (theirs) and $MERGED (the file to write).
Useful settings:
mergetool.keepBackup=falsestops Git leaving*.origbackup files after each resolution.mergetool.<tool>.trustExitCode=truetreats the tool's exit status as "resolved" or "not resolved".
Most editors, including VS Code, JetBrains IDEs and Vim plugins, also detect conflict markers and offer "Accept current", "Accept incoming" and "Accept both" buttons directly, without git mergetool. "Current" is ours and "incoming" is theirs.
rerere: reuse recorded resolution
If you resolve the same conflict repeatedly, for example when you rebase a long-lived branch several times or redo a merge to test it, rerere ("reuse recorded resolution") can remember your resolution and apply it automatically next time:
git init -q trail-guide && cd trail-guide
git config rerere.enabled true
echo "title=Guide" > site.ini; git add .; git commit -q -m "Add site settings"
git switch -q -c rename
echo "title=Trail Guide" > site.ini; git commit -q -am "Rename site"
git switch -q main
echo "title=The Guide" > site.ini; git commit -q -am "Add article to title"
echo "=== first time: resolve by hand"
git merge -q rename
echo "title=The Trail Guide" > site.ini
git add site.ini && git commit -q --no-edit
git reset -q --hard HEAD~1 # throw the merge away
echo "=== second time: rerere resolves it"
git merge rename
cat site.ini
git status -s
=== first time: resolve by hand
Auto-merging site.ini
CONFLICT (content): Merge conflict in site.ini
Recorded preimage for 'site.ini'
Automatic merge failed; fix conflicts and then commit the result.
Recorded resolution for 'site.ini'.
=== second time: rerere resolves it
Auto-merging site.ini
CONFLICT (content): Merge conflict in site.ini
Resolved 'site.ini' using previous resolution.
Automatic merge failed; fix conflicts and then commit the result.
title=The Trail Guide
UU site.ini
rerere recorded the conflict and its resolution the first time (Recorded preimage, Recorded resolution), and reused it on the second merge. It still leaves the file marked as conflicted (UU), so you review it and git add it. Set rerere.autoUpdate=true to have it staged automatically. git rerere forget <file> deletes a bad recorded resolution. Resolutions are stored in .git/rr-cache and are local to your repository.
Checking a resolution
After resolving, and before pushing:
- Build and test. A merge can be textually clean yet wrong, such as when one side renamed a function and the other added a new call to the old name. No conflict appears, but the code doesn't compile.
git show --remerge-diff <merge>(Git 2.36) shows how the committed merge differs from what Git produced automatically, conflict markers included: exactly how each conflict was resolved. It's the most useful view when reviewing someone else's merge.
git init -q trail-guide && cd trail-guide
printf 'Ridge loop\nDistance: 8 km\nClimb: 240 m\nParking: free\n' > ridge.txt
git add . && git commit -q -m "Add ridge notes"
git switch -q -c ana
sed -i 's/8 km/8.5 km/' ridge.txt; git commit -q -am "Correct distance"
git switch -q main
sed -i 's/8 km/9 km/; s/free/paid/' ridge.txt; git commit -q -am "Measure distance again, parking now paid"
git merge -q ana
printf 'Ridge loop\nDistance: 8.5 km\nClimb: 240 m\nParking: paid\n' > ridge.txt
git add ridge.txt && git commit -q --no-edit
git show --remerge-diff --format='%h %s' HEAD
Auto-merging ridge.txt
CONFLICT (content): Merge conflict in ridge.txt
Automatic merge failed; fix conflicts and then commit the result.
601536e Merge branch 'ana'
diff --git a/ridge.txt b/ridge.txt
remerge CONFLICT (content): Merge conflict in ridge.txt
index 81324c7..25f5948 100644
--- a/ridge.txt
+++ b/ridge.txt
@@ -1,8 +1,4 @@
Ridge loop
-<<<<<<< 94a0b4c (Measure distance again, parking now paid)
-Distance: 9 km
-=======
Distance: 8.5 km
->>>>>>> 6d444be (Correct distance)
Climb: 240 m
Parking: paid
The removed lines are Git's automatic attempt, markers and all, and what remains is the resolution: Ana's distance was kept and the other side's version and markers removed.
Avoiding painful conflicts
- Merge or rebase often. Small, frequent integrations produce small conflicts. A branch that lives for months produces huge ones.
- Keep commits focused. A commit that both reformats a file and changes logic conflicts with everything. Do reformatting in its own commit, ideally agreed with the team.
- Use the same formatter settings and line endings across the team (article 35), so whitespace never causes conflicts.
- Talk. If two people need to restructure the same file, agree an order.
Common questions
Git says "Automatic merge failed". Did I break something?
No. Nothing has been committed and no work is lost. Resolve the files, git add them and git merge --continue, or git merge --abort to go back.
What do HEAD and the branch name in the markers mean?
<<<<<<< HEAD marks your current branch's version, and >>>>>>> ana marks the version from the branch being merged. During a rebase or cherry-pick, the labels name commits instead.
I resolved the file but Git still says it's unmerged. Why?
Resolving means git add on the file. Until then, the staging area still holds the three stages. Run git status to see what's left.
Can I commit with conflict markers still in a file?
Git lets you, once you git add it. It doesn't check content. That's why git diff --check before adding (or git diff --cached --check after), a test run, and pre-commit hooks that reject markers (article 36) are worth having.
Should I use -X theirs to avoid conflicts?
Only when you know one side should win every conflict, such as generated files. It decides without looking, and the result can be inconsistent code. Resolving with understanding is usually better.
Summary
- A conflict happens where both sides changed the same lines since the merge base. Git merges everything else and marks the conflicting parts.
- Resolve by editing the file, deleting the markers,
git add, thengit merge --continue.git merge --abortcancels. - Set
merge.conflictStyle=zdiff3to see the base version in the markers. - Stages 1, 2 and 3 hold base, ours and theirs.
git ls-files -u,git show :2:file,git diffandgit log --mergehelp you understand a conflict. git checkout --ours/--theirs <file>takes one side's whole file, andgit checkout -m <file>brings the markers back. In a rebase, ours and theirs are swapped.- Modify/delete conflicts are resolved with
git addorgit rm, and binary conflicts by choosing a version. git mergetoolopens a three-way tool, and rerere replays resolutions you've made before.- Check the result with a build, tests and
git show --remerge-diff.
Practice questions
During a merge, git status -s shows UD config.yml. What happened, and what are the two ways to resolve it?
Answer
Our side updated config.yml and their side deleted it (a modify/delete conflict). To keep the file with our changes, git add config.yml. To accept the deletion, git rm config.yml. Then finish the merge with git merge --continue.
A conflicted file has one conflicting line, but the other branch also changed ten other lines in it without conflict. You run git checkout --ours file. What do you lose?
Answer
All ten of the other branch's clean changes to that file, as well as their side of the conflict. --ours replaces the working file with our entire version. To get the conflicted file back, run git checkout -m file. To keep all clean changes and prefer our side only in conflicts, abort and merge with -X ours.
You're rebasing your feature branch onto main and hit a conflict. You want to keep your own version of a file. Is that --ours or --theirs?
Answer
--theirs. During a rebase, HEAD is the commit being built on main, so "ours" is the main side, and "theirs" is your commit being replayed. Check with git status and the labels in the markers if in doubt.
Previous: Merging Branches: Fast-Forward, Three-Way Merges, --no-ff and Squash Merges

More in Git Course
Quiz: Git Delicate Details
Twenty-five questions on Git's subtle corners: what commit -a and stash leave out, empty folders, ignore patterns and negation, HEAD~ versus HEAD^, ours and theirs in a rebase, tags that don't move or don't push, describe, merge reverts, diff exit codes, stash conflicts and more.
Quiz: Git Interview Questions
Thirty Git questions of the kind asked in developer interviews: Git versus GitHub, the staging area, merge versus rebase, fetch versus pull, reset, revert and restore, detached HEAD, conflicts, force pushing, stash, cherry-pick, reflog, bisect, submodules, LFS and how commits are identified.
Quiz: Git Course Final Exam (All Parts)
The final exam for the Git course: thirty questions across all twelve parts, from the three areas and staging through branching, remotes, undoing, history investigation, configuration, tags, larger projects, patches, rewriting, internals and troubleshooting.
Quiz: Professional Git Practice (Git Part 12)
Sixteen questions on migrating to Git from Subversion, TFVC and Mercurial, team best practices for commits, branches and reviews, repository hygiene, and diagnosing and fixing the most common Git errors, with explanations.
Discussion
Sign in to ask a question or share what you learned.