Resolving Merge Conflicts

Updated · 15 min read
mediumGit 2.53gitmerge-conflictconflict-markerszdiff3interview

Two teachers update the same class schedule at the same time. One moves the math class to Monday, the other moves it to Tuesday. When their copies are combined, nobody can just pick one automatically: someone has to ask "which is right?" and decide.

A merge conflict is Git asking that question. When two branches change the same lines of a file in different ways, Git can't know which change you want, so it stops the merge, marks both versions in the file, and waits for you. It is not an error and nothing is broken. It is a normal part of working in a team, and once you can read the markers, resolving most conflicts takes a minute.

In this lesson, you will create a conflict, read Git's markers, resolve it by hand, take one side when that is all you need, and abort a merge that you want to try again later.

How a Conflict Happens

Navika and Advitha both changed the late fee, on different branches, to different values:

Terminal
mkdir fee-tracker
cd fee-tracker
git init --quiet
printf 'Term 1 fee: $12,000\nLate fee: $250\nHostel fee: $9,000\n' > fees.txt
git add fees.txt
git commit --quiet -m "Add the fee list"
git switch --quiet -c feature/late-fee
sed -i.bak 's/Late fee: $250/Late fee: $300/' fees.txt
rm fees.txt.bak
git commit --quiet -a --author="Advitha Anamaneni <advitha@example.com>" -m 'Raise the late fee to $300'
git switch --quiet main
sed -i.bak 's/Late fee: $250/Late fee: $275/' fees.txt
rm fees.txt.bak
git commit --quiet -am 'Raise the late fee to $275'
git log --oneline --graph --all
Output
* 7ad8422 (HEAD -> main) Raise the late fee to $275
| * ce95e72 (feature/late-fee) Raise the late fee to $300
|/
* d5d4cd2 Add the fee list

Navika merges Advitha's branch into main:

Terminal
git merge feature/late-fee
Output
Auto-merging fees.txt
CONFLICT (content): Merge conflict in fees.txt
Automatic merge failed; fix conflicts and then commit the result.

Git merged what it could (Auto-merging fees.txt), found one place where both sides changed the same line (CONFLICT (content)), and stopped: Automatic merge failed; fix conflicts and then commit the result. No merge commit was made yet. You are now in the middle of a merge.

See What Is in Conflict

git status lists the files that need you:

Terminal
git status
Output
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:   fees.txt
 
no changes added to commit (use "git add" and/or "git commit -a")

both modified: fees.txt is under Unmerged paths. In the short format, the same file shows as UU. Git also reminds you of the two ways out: fix the conflicts and commit, or git merge --abort.

Read the Conflict Markers

Git wrote both versions into the file, between markers:

Terminal
cat fees.txt
Output
Term 1 fee: $12,000
<<<<<<< HEAD
Late fee: $275
=======
Late fee: $300
>>>>>>> feature/late-fee
Hostel fee: $9,000
MarkerMeaning
<<<<<<< HEADStart of the conflict. The lines below are ours: the branch you are on, main.
=======The divider between the two versions
>>>>>>> feature/late-feeEnd of the conflict. The lines above it are theirs: the branch being merged.

Everything outside the markers merged cleanly. Here, the term fee and hostel fee lines are fine, and only the late fee is in question: $275 on main against $300 on the feature.

Ours, theirs and the markers between them

Resolve It

Resolving means editing the file into the version you want and removing all three marker lines. The result can be ours, theirs, a mix of both or something new. After talking to Advitha, Navika agrees on $300. In your editor you would delete the markers and the line you don't want. Here, the file is written in one step:

Terminal
printf 'Term 1 fee: $12,000\nLate fee: $300\nHostel fee: $9,000\n' > fees.txt
git add fees.txt
git status -s
Output
M  fees.txt

git add tells Git "this file is resolved". The status changed from UU to M : the resolved file is staged. When every conflicted file is added, finish the merge with a commit. --no-edit keeps Git's message:

Terminal
git commit --no-edit
git log --oneline --graph
Output
[main d85f1d7] Merge branch 'feature/late-fee'
*   d85f1d7 (HEAD -> main) Merge branch 'feature/late-fee'
|\
| * ce95e72 (feature/late-fee) Raise the late fee to $300
* | 7ad8422 Raise the late fee to $275
|/
* d5d4cd2 Add the fee list

The merge commit records the decision. Instead of git commit, you can also run git merge --continue, which does the same.

Syntax

CommandIt is used to
git statusList the files with conflicts (both modified, UU)
git add <file>Mark a file as resolved
git commit or git merge --continueFinish the merge once every file is resolved
git merge --abortStop the merge and go back to how things were before it
git restore --ours <file>Take our whole version of a conflicted file
git restore --theirs <file>Take their whole version of a conflicted file
git config merge.conflictStyle zdiff3Also show the original lines in conflict markers

See the Original Too: zdiff3

With the default markers, you see ours and theirs, but not what the line was before either side changed it. That third version often makes the decision obvious. Setting merge.conflictStyle to zdiff3 (added in Git 2.35) adds it:

Terminal
git config merge.conflictStyle zdiff3
git switch --quiet -c feature/term-fee
sed -i.bak 's/Term 1 fee: $12,000/Term 1 fee: $12,500/' fees.txt
rm fees.txt.bak
git commit --quiet -a --author="Srihitha Anamaneni <srihitha@example.com>" -m "Correct the term 1 fee"
git switch --quiet main
sed -i.bak 's/Term 1 fee: $12,000/Term 1 fee: $13,000/' fees.txt
rm fees.txt.bak
git commit --quiet -am "Raise the term 1 fee"
git merge feature/term-fee
Output
Auto-merging fees.txt
CONFLICT (content): Merge conflict in fees.txt
Automatic merge failed; fix conflicts and then commit the result.
Terminal
cat fees.txt
Output
<<<<<<< HEAD
Term 1 fee: $13,000
||||||| d85f1d7
Term 1 fee: $12,000
=======
Term 1 fee: $12,500
>>>>>>> feature/term-fee
Late fee: $300
Hostel fee: $9,000

The new section between ||||||| and ======= is the base: the line as it was in the merge base, before either branch changed it. The marker line also names the merge base commit, here d85f1d7, the previous merge. Now it is clear that both sides started from $12,000: Navika raised it to 13000 and Srihitha corrected it to 12500.

git config merge.conflictStyle zdiff3 sets it for this repository. Add --global to use it everywhere, which most experienced developers do. (diff3 is the older version of the same style, and zdiff3 trims lines that are the same on both sides.)

Take One Side: --ours and --theirs

Sometimes you don't need to mix anything: one side is simply right for the whole file. Srihitha's correction is the right one here, so Navika takes their version of fees.txt:

Terminal
git restore --theirs fees.txt
cat fees.txt
git add fees.txt
git commit --quiet --no-edit
git log --oneline -1
Output
Term 1 fee: $12,500
Late fee: $300
Hostel fee: $9,000
3c52f61 (HEAD -> main) Merge branch 'feature/term-fee'

git restore --theirs replaced the whole file with the merged branch's version, and --ours would take the current branch's version. The older command git checkout --theirs fees.txt does the same. Either way, you still git add and commit.

Abort a Merge

If a conflict is bigger than expected, or you need to ask someone first, you can stop. git merge --abort puts everything back as it was before git merge.

Here is a conflict every Java team meets sooner or later: two branches change the version in Maven's pom.xml:

Terminal
cat > pom.xml <<'EOF'
<project>
  <groupId>com.feetracker</groupId>
  <artifactId>fee-tracker</artifactId>
  <version>1.0.0</version>
</project>
EOF
git add pom.xml
git commit --quiet -m "Add the Maven build file"
git switch --quiet -c release/2.0
sed -i.bak "s/<version>1.0.0</<version>2.0.0</" pom.xml
rm pom.xml.bak
git commit --quiet -am "Start version 2.0.0"
git switch --quiet main
sed -i.bak "s/<version>1.0.0</<version>1.1.0</" pom.xml
rm pom.xml.bak
git commit --quiet -am "Release version 1.1.0"
git merge release/2.0
Output
Auto-merging pom.xml
CONFLICT (content): Merge conflict in pom.xml
Automatic merge failed; fix conflicts and then commit the result.
Terminal
cat pom.xml
Output
<project>
  <groupId>com.feetracker</groupId>
  <artifactId>fee-tracker</artifactId>
<<<<<<< HEAD
  <version>1.1.0</version>
||||||| 08f0b44
  <version>1.0.0</version>
=======
  <version>2.0.0</version>
>>>>>>> release/2.0
</project>

The markers look the same in any kind of text file. The team hasn't decided the version yet, so Navika aborts:

Terminal
git merge --abort
git status -s
grep version pom.xml
Output
  <version>1.1.0</version>

The status is clean, and pom.xml is back to main's 1.1.0, as if the merge had never started.

In Practice

For anything bigger than a few lines, most developers resolve conflicts in a tool rather than by editing markers:

  • IntelliJ IDEA and other JetBrains IDEs open a three-panel window (yours, the result, theirs) with buttons to take each change.
  • VS Code shows "Accept Current Change", "Accept Incoming Change" and "Accept Both Changes" above each conflict, and has a three-way merge editor.
  • git mergetool opens the merge tool you configured (such as meld, kdiff3 or vimdiff) for each conflicted file in turn.
  • GitHub and GitLab can resolve simple conflicts in the browser on the pull request (merge request) page. Bitbucket Cloud has no built-in conflict editor, so conflicts there are resolved locally and pushed.

They all edit the same file and run the same git add for you. Knowing the markers means you can always check what a tool did.

Common Mistakes

Committing with markers still in the file. Search for <<<<<<< before you git add. Many teams add a check (a hook or CI step) that rejects such commits.

Panicking and deleting the repository. Nothing is lost during a conflict. git merge --abort returns you to where you started.

Taking --ours or --theirs without looking. They replace the whole file. Read both sides first.

Resolving without understanding the other change. Ask the teammate who wrote it, or read their commit with git log -p --merge, which shows only the commits that touched the conflicting files.

Mixing up ours and theirs. In a merge, ours is the branch you are on, and theirs is the branch you named in git merge. (In a rebase they are swapped, which the rebase lessons explain.)

Leaving merges for weeks. The longer branches live apart, the bigger the conflicts. Merge main into your branch often.

Under the Hood

During a conflict, the staging area holds three versions of the conflicted file instead of one, numbered as stages: 1 is the merge base, 2 is ours and 3 is theirs. git ls-files -u lists them, and git show :<stage>:<file> prints each one. Here, a quick conflict is created and inspected:

Terminal
git switch --quiet -c feature/hostel
sed -i.bak 's/Hostel fee: $9,000/Hostel fee: $9,500/' fees.txt
rm fees.txt.bak
git commit --quiet -am "Raise the hostel fee"
git switch --quiet main
sed -i.bak 's/Hostel fee: $9,000/Hostel fee: $8,500/' fees.txt
rm fees.txt.bak
git commit --quiet -am "Lower the hostel fee"
git merge --quiet feature/hostel
git ls-files -u
git show :1:fees.txt | grep Hostel
git show :2:fees.txt | grep Hostel
git show :3:fees.txt | grep Hostel
git merge --abort
Output
Auto-merging fees.txt
CONFLICT (content): Merge conflict in fees.txt
Automatic merge failed; fix conflicts and then commit the result.
100644 a6e3af3725bf87cd91819994d2834564a2477aaa 1	fees.txt
100644 5e28b287818b0a8498a17746b0e816f2daa753bb 2	fees.txt
100644 eb92696acb180cae7019b1e878974f71893ca1ba 3	fees.txt
Hostel fee: $9,000
Hostel fee: $8,500
Hostel fee: $9,500

Three entries, one per stage, each with the ID of its version of fees.txt. Stage 1 has the original 9000, stage 2 (ours) 8500 and stage 3 (theirs) 9500. The file in your folder, with the markers, is built from these three. git add replaces all three stages with one ordinary entry, which is exactly what "marking as resolved" means. --ours and --theirs copy stage 2 or 3 into the working tree.

Git also writes .git/MERGE_HEAD with the ID of the commit being merged. That is how git commit knows to make a merge commit with two parents, and how git merge --abort knows there is a merge to abort.

Interview Corner

What is a merge conflict and when does it happen?

A situation where Git can't combine two branches automatically, because both changed the same lines of a file differently (or one changed a file the other deleted). Git stops the merge, marks the conflicting parts in the file, and waits for you to resolve them.

How do you resolve a merge conflict?

Run git status to find the conflicted files. In each, edit the section between <<<<<<< and >>>>>>> into the correct result and delete the markers. Then git add each file and run git commit (or git merge --continue). To give up, run git merge --abort.

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

<<<<<<< HEAD starts the conflict, followed by the current branch's lines (ours). ======= separates them from the merged branch's lines (theirs), which end at >>>>>>> <branch>. With zdiff3, a ||||||| section in the middle shows the original lines from the merge base.

What are "ours" and "theirs" in a merge?

Ours is the branch you are on when you run git merge. Theirs is the branch being merged in. git restore --ours <file> or --theirs takes one whole version of a conflicted file.

How do you cancel a merge that has conflicts?

git merge --abort. It restores the branch, staging area and working tree to the state before the merge started. It works best when you had no uncommitted changes before merging.

Practice

Challenge: conflict or not? In the same repository, you are on main. Create a branch feature/fees, change the term 1 fee line to Term 1 fee: $14,000 there and commit. On main, change the hostel fee line to Hostel fee: $10,000 and commit. Then merge feature/fees into main with --no-edit.

What does Git print? Both branches changed fees.txt. Before you open the solution, decide whether this merge stops with a conflict.

Show solution
Terminal
git switch --quiet -c feature/fees
sed -i.bak 's/Term 1 fee: $12,500/Term 1 fee: $14,000/' fees.txt
rm fees.txt.bak
git commit --quiet -am 'Raise the term 1 fee to $14,000'
git switch --quiet main
sed -i.bak 's/Hostel fee: $8,500/Hostel fee: $10,000/' fees.txt
rm fees.txt.bak
git commit --quiet -am 'Raise the hostel fee to $10,000'
git merge --no-edit feature/fees
cat fees.txt
Output
Auto-merging fees.txt
Merge made by the 'ort' strategy.
 fees.txt | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
Term 1 fee: $14,000
Late fee: $300
Hostel fee: $10,000

No conflict. Both branches changed the same file, but different lines, with an unchanged line (the late fee) between them. Git compared each side with the merge base, took each change, and made the merge commit. Conflicts happen when changes touch the same or neighboring lines, not just the same file.

Checkpoint. In a practice repository, create three conflicts and resolve each one: the same line edited on two branches (resolve by hand), a file deleted on one branch and edited on the other (decide whether to keep it with git add or delete it with git rm), and a version clash in a pom.xml (resolve with --ours or --theirs).

Quick check: Merge conflicts

4 questions
1When does Git report a merge conflict?
2In a merge, what is between <<<<<<< HEAD and =======?
3You edited the conflicted file and removed the markers. What next?
4What does merge.conflictStyle zdiff3 add to the markers?

Key Takeaways

Quick reference

CommandIt is used to
git statusSee which files have conflicts
git add <file>Mark a file as resolved
git commit / git merge --continueFinish the merge
git merge --abortCancel the merge
git restore --ours <file>Take the current branch's whole file
git restore --theirs <file>Take the merged branch's whole file
git config --global merge.conflictStyle zdiff3Show the base version in markers
git log -p --mergeSee the commits behind the conflicting changes
git mergetoolResolve in a visual tool

Was this tutorial helpful?

Lesson 32 of 106