Sub topic 8
Resolving Merge Conflicts
This chapter assumes you can create and switch branches, and that you know the difference between a fast-forward merge and a three-way merge. You should also be comfortable with git status, git add, and git commit. A merge conflict is the one case where a three-way merge can't finish on its own.
Why conflicts happen
A three-way merge compares three commits: the merge base (the last commit both branches share), the tip of your current branch, and the tip of the branch you are merging in. For each region of each file, Git applies a simple rule:
- If only one side changed the region, Git takes that change.
- If both sides made the identical change, Git takes it once.
- If both sides changed the same lines in different ways, Git can't choose, so it stops and asks you.
Conflicts also occur when one branch edits a file and the other deletes or renames it. Git detects renames by content similarity, so a rename plus an edit often merges cleanly. A delete plus an edit never does.
Git only sees text overlap. If one branch renames a function and the other adds a call to the old name in a different file, the merge succeeds and the code breaks. A clean merge doesn't prove the code works, so run your tests after every merge.
Creating a conflict to practice on
This script builds a small repository where both branches change the same line.
git init conflict-demo
cd conflict-demo
printf 'title=Demo\ntimeout=30\n' > app.conf
git add app.conf
git commit -m "Add config"
git branch -M main
git switch -c feature/login
printf 'title=Demo\ntimeout=60\n' > app.conf
git commit -am "Raise timeout to 60"
git switch main
printf 'title=Demo\ntimeout=45\n' > app.conf
git commit -am "Raise timeout to 45"
git merge feature/loginThe last command prints CONFLICT (content): Merge conflict in app.conf and Automatic merge failed; fix conflicts and then commit the result. The merge is now paused, not failed. Your repository is in a "merging" state until you finish or abort it.
Reading the repository state
Run git status. The file appears under "Unmerged paths" as both modified. Git has also done three things behind the scenes:
- It wrote conflict markers into the working-tree file.
- It stored three versions of the file in the index, as stage 1 (base), stage 2 (ours), and stage 3 (theirs).
- It recorded the incoming commit in
.git/MERGE_HEAD, which is howgit commitknows to create a merge commit with two parents.
Here is what app.conf looks like now:
title=Demo
<<<<<<< HEAD
timeout=45
=======
timeout=60
>>>>>>> feature/loginThe block between <<<<<<< and ======= is your side (the branch you were on, HEAD). The block between ======= and >>>>>>> is the incoming side. Lines outside the markers merged cleanly.
Resolving the conflict
Resolving means deciding what the file should contain and saying so in your editor. You can keep one side, keep the other, or write something new. Say the team agrees on 50 seconds. Replace the whole marked block, including the three marker lines, with one line:
title=Demo
timeout=50Then tell Git the file is resolved and finish the merge:
git add app.conf
git commitGit opens an editor with a prepared message such as Merge branch 'feature/login'. You can also run git merge --continue, which does the same thing and refuses to run while unresolved files remain.
Before you stage a file, check that no markers are left over:
git diff --check
git grep -n -E '^(<<<<<<<|=======|>>>>>>>)'git diff --check reports leftover conflict markers and whitespace errors. The git grep line searches tracked files for marker lines.
Tools that make resolution easier
During a conflict, a few commands help you see what is going on.
| Command | What it does |
|---|---|
| `git diff --name-only --diff-filter=U` | Lists only the unmerged files |
| `git diff` | Shows a combined diff of the conflicted regions |
| `git log --merge --oneline` | Shows commits from both sides that touch the conflicted files |
| `git checkout --ours -- path` | Replaces the file with your side's version |
| `git checkout --theirs -- path` | Replaces the file with the incoming version |
| `git checkout -m -- path` | Re-creates the conflict markers if you botched the edit |
| `git merge --abort` | Cancels the merge and restores the pre-merge state |
| `git mergetool` | Opens a configured visual merge tool |
--ours and --theirs swap meaning during a rebase, because the branch being replayed is "theirs" while the branch you rebase onto is "ours". Check which one you are getting before you rely on it.
Showing the base version
By default, markers show only two sides, so you can't tell what each side changed from. The diff3 style adds the base version between the sides. The newer zdiff3 style (Git 2.35 or later) does the same and trims common lines to make the blocks shorter.
git config --global merge.conflictStyle zdiff3With this setting, our example looks like this:
title=Demo
<<<<<<< HEAD
timeout=45
||||||| parent of 1a2b3c4 (Raise timeout to 60)
timeout=30
=======
timeout=60
>>>>>>> 1a2b3c4 (Raise timeout to 60)The middle block shows the original value, 30. Now you can see that both sides raised it, and you can choose a value with that context.
Delete and modify conflicts
If one branch deleted app.conf and the other edited it, git status shows deleted by us or deleted by them. There are no markers to edit. You choose between the two outcomes:
git add app.confRun that to keep the edited file. Run git rm app.conf to accept the deletion. Then commit as usual.
Backing out and common mistakes
Run git merge --abort at any point before you commit to return to the state before the merge started. If you had uncommitted changes when you began the merge, Git may not be able to restore them. Commit or stash your work before merging.
- Committing markers. Staging a file doesn't check its contents. Run the check commands above first.
- Taking one side blindly.
--oursand--theirsdiscard the other side's work for the whole file, not only the conflicting lines. Use them only when you mean it. - Resolving the same conflict repeatedly. If you merge or rebase often, enable
git config --global rerere.enabled true. Git then records how you resolved each conflict and reapplies that resolution if the same conflict appears again. - Binary files. Git can't merge them line by line. Choose a version with
--oursor--theirs, thengit addit. - Skipping tests. A conflict-free or hand-resolved merge can still be broken. Build and test before you push.
Exercise
You run git merge feature/login and Git reports a conflict in app.conf. You open the file and see markers. Name the commands, in order, that you run after editing the file so that the merge is completed, and explain what each one tells Git.
Show answer
First git add app.conf, which tells Git the conflict in that file is resolved and stages the new content, replacing the three index stages. Then git commit (or git merge --continue), which creates the merge commit with two parents because .git/MERGE_HEAD exists. Before staging, git diff --check can confirm no markers remain.
Exercise
During a merge, you realise you resolved a file wrongly and the markers are gone from your working copy. Which command restores the original conflict markers for that file, and which command cancels the whole merge?
Show answer
git checkout -m -- path/to/file re-creates the conflicted version with markers. git merge --abort cancels the whole merge and returns the repository to its state before the merge began.