TExcelTAD
Home › Git › Git Merge Conflicts

Git Merge Conflicts

The merge lesson showed a basic conflict. This lesson gives you the tools to resolve any conflict calmly: understanding the markers, seeing the original version, picking a side for whole files, and avoiding conflicts in the first place.

All examples use this scenario: two people change the same line of price.txt on different branches.

bash
mkdir shop && cd shop && git init -q
printf 'item: tea\nprice: 3.00\ncurrency: EUR\n' > price.txt && git add . && git commit -q -m "Add tea price"
git switch -q -c sale && sed -i 's/3.00/2.50/' price.txt && git commit -q -am "Tea on sale"
git switch -q main && sed -i 's/3.00/3.20/' price.txt && git commit -q -am "Raise tea price"

See the base version too: diff3 / zdiff3

The default markers show ours and theirs, but not what the line was before either change. With merge.conflictStyle zdiff3 (or diff3) Git also shows the common ancestor, which makes the intent of each side obvious:

bash · try it yourself
mkdir shop && cd shop && git init -q
printf 'item: tea\nprice: 3.00\ncurrency: EUR\n' > price.txt && git add . && git commit -q -m "Add tea price"
git switch -q -c sale && sed -i 's/3.00/2.50/' price.txt && git commit -q -am "Tea on sale"
git switch -q main && sed -i 's/3.00/3.20/' price.txt && git commit -q -am "Raise tea price"
git config merge.conflictStyle zdiff3
git merge -q sale
cat price.txt
Auto-merging price.txt
CONFLICT (content): Merge conflict in price.txt
Automatic merge failed; fix conflicts and then commit the result.
item: tea
<<<<<<< HEAD
price: 3.20
||||||| 008ab9d
price: 3.00
=======
price: 2.50
>>>>>>> sale
currency: EUR

Read it as: the price was 3.00; main raised it to 3.20, sale dropped it to 2.50. Now you can make an informed decision. Many developers set zdiff3 globally.

Inspecting a conflict

During a conflict, the index holds up to three versions of each conflicted file, numbered stages: 1 = common ancestor, 2 = ours (HEAD), 3 = theirs. You can print each one:

bash · try it yourself
mkdir shop && cd shop && git init -q
printf 'item: tea\nprice: 3.00\ncurrency: EUR\n' > price.txt && git add . && git commit -q -m "Add tea price"
git switch -q -c sale && sed -i 's/3.00/2.50/' price.txt && git commit -q -am "Tea on sale"
git switch -q main && sed -i 's/3.00/3.20/' price.txt && git commit -q -am "Raise tea price"
git merge -q sale
git ls-files -u
echo "--- base:   $(git show :1:price.txt | grep price)"
echo "--- ours:   $(git show :2:price.txt | grep price)"
echo "--- theirs: $(git show :3:price.txt | grep price)"
echo "--- conflicted files:"
git diff --name-only --diff-filter=U
Auto-merging price.txt
CONFLICT (content): Merge conflict in price.txt
Automatic merge failed; fix conflicts and then commit the result.
100644 f303df2433c5227feeb028f8f9cf7c7bb511fa84 1	price.txt
100644 f9102298aecd98f4c5e662f1592ac669c8469003 2	price.txt
100644 9ae476d926e5d504bb2b68006e7b0aa4daa75bc4 3	price.txt
--- base:   price: 3.00
--- ours:   price: 3.20
--- theirs: price: 2.50
--- conflicted files:
price.txt

git diff --name-only --diff-filter=U is a handy way to list files that still need resolving.

Taking one side for a whole file

When you know one version is right for a file, you don't need to edit markers by hand:

Command Keeps
git checkout --ours price.txt the version from the current branch
git checkout --theirs price.txt the version from the branch being merged
git merge -X ours sale on conflicting hunks, prefer ours (non-conflicting changes from both still merge)
git merge -X theirs sale on conflicting hunks, prefer theirs
bash · try it yourself
mkdir shop && cd shop && git init -q
printf 'item: tea\nprice: 3.00\ncurrency: EUR\n' > price.txt && git add . && git commit -q -m "Add tea price"
git switch -q -c sale && sed -i 's/3.00/2.50/' price.txt && git commit -q -am "Tea on sale"
git switch -q main && sed -i 's/3.00/3.20/' price.txt && git commit -q -am "Raise tea price"
git merge -q sale
git checkout --theirs price.txt
git add price.txt
git commit -q --no-edit
cat price.txt
echo "--- same result in one step with -X theirs:"
git reset -q --hard HEAD~1
git merge -X theirs sale -m "Merge sale (prefer sale's prices)"
cat price.txt
Auto-merging price.txt
CONFLICT (content): Merge conflict in price.txt
Automatic merge failed; fix conflicts and then commit the result.
Updated 1 path from the index
item: tea
price: 2.50
currency: EUR
--- same result in one step with -X theirs:
Auto-merging price.txt
Merge made by the 'ort' strategy.
 price.txt | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
item: tea
price: 2.50
currency: EUR

Warning: In a rebase, "ours" and "theirs" are swapped: "ours" is the branch you are rebasing onto, and "theirs" is your commit being replayed. Double-check the result with git diff before continuing.

Aborting and starting over

Command Effect
git merge --abort go back to the state before the merge
git checkout -m price.txt re-create the conflict markers in a file you botched
git rebase --abort, git cherry-pick --abort the same for rebase and cherry-pick

Conflicts Git can't mark with lines

Not every conflict is about lines. If one branch deletes a file that the other modifies, Git can't show markers; you decide whether to keep (git add) or delete (git rm) it:

bash · try it yourself
mkdir shop && cd shop && git init -q
echo "old banner" > banner.txt && git add . && git commit -q -m "Add banner"
git switch -q -c cleanup && git rm -q banner.txt && git commit -q -m "Remove banner"
git switch -q main && echo "new banner" > banner.txt && git commit -q -am "Update banner"
git merge cleanup
git status -s
git rm -q banner.txt
git commit -q --no-edit
git log --oneline -1
git ls-files
CONFLICT (modify/delete): banner.txt deleted in cleanup and modified in HEAD.  Version HEAD of banner.txt left in tree.
Automatic merge failed; fix conflicts and then commit the result.
UD banner.txt
84cc2f2 Merge branch 'cleanup'

UD means "updated by us, deleted by them". Here we agreed with the cleanup and removed the file.

Avoiding conflicts

  • Merge (or rebase onto) main often; small differences give small conflicts.
  • Keep branches short-lived and focused.
  • Agree on formatting (a formatter like Prettier or Black) so whitespace changes don't collide.
  • Don't reorder or reformat code in the same commit as a feature change.
  • Turn on git config --global rerere.enabled true: Git reuses recorded resolutions when the same conflict appears again (common with repeated rebases).

Test yourself

What extra section does merge.conflictStyle zdiff3 add to conflict markers?

  1. The commit messages
  2. The common ancestor's version (||||||| base)
  3. Line numbers
  4. Author names

During a merge conflict, what does git show :3:file.txt print?

  1. The common ancestor
  2. Our version
  3. Their version (the branch being merged)
  4. Line 3 of the file

Which command resolves a conflicted file by keeping the incoming branch's version entirely (during a merge)?

  1. git checkout --ours file
  2. git checkout --theirs file
  3. git restore --staged file
  4. git merge --abort