Resolving Merge Conflicts
7 pages · ~5 min✓ Reviewed
Builds on Rebase vs Merge. Next up: Stash & Worktrees.
Why Conflicts Happen
1. What a conflict is
- Conflict = Git refuses to guess
- Two commits changed the same lines since their merge base
- Or one edited a file the other deleted
- Merge base = last shared commit; ours = your branch; theirs = incoming
| Case | Base | Ours | Theirs | Result |
|---|---|---|---|---|
| Only theirs changed | A | A | B | B (auto) |
| Only ours changed | A | B | A | B (auto) |
| Both, differently | A | B | C | CONFLICT |
2. Common triggers
- Same-line edits
- Edit vs delete
- rename/rename (named differently) or rename/delete
- add/add: same path, different content
- Rename on one side + edit on the other merges cleanly
3. Where it shows up
- Can conflict:
merge,rebase,cherry-pick,stash pop,revert
git statuslists Unmerged paths, both modifiedMERGE_HEADor arebase-mergedir exists until you finish
Paused, not failed
The operation stays open until every path is resolved.
Reading Conflict Markers
1. The three markers
text
<<<<<<< HEAD timeout = 30 ======= timeout = 60 >>>>>>> feature
Markers start at column 0
<<<<<<< HEADto=======: your current side=======to>>>>>>> feature: the incoming side- In a rebase the HEAD side is the upstream branch; the flip is covered in the ours/theirs section
2. Show the base
diff3orzdiff3adds a|||||||section with the original text- Seeing the base makes each side's intent easy to judge
- Make the base section the default:
bash
git config --global merge.conflictStyle zdiff3
3. Resolve and check
- Pick ours, theirs, or a hand-written mix
- Edit to the final text; delete all three marker lines
Resolve and Continue a Merge or Rebase
1. Merge Workflow
- git merge featurestops on conflict
- git statusfind both modified
- Edit filesremove markers
- git add <file>marks resolved
- git commitmessage pre-filled
2. Rebase Workflow
- 1git rebase mainstops at 1st bad commit
- 2Fix files, git add <file>
- 3git rebase --continuerepeat per conflicting commit
git addalone marks a file resolved- Rebase needs no commit in between, only
--continue git rebase --skipdrops the current commit
3. Track and Reuse
Ours, Theirs and Mergetool
1. Ours vs theirs
| ours | theirs | |
|---|---|---|
| merge | branch you are on | branch merged in |
| rebase | upstream you rebase onto | your commit replayed |
2. Picking a side
bash
git checkout --ours app.py git checkout --theirs app.py git add app.py
whole file one way, then stage
| Option | Effect |
|---|---|
-X ours / -X theirs | decides conflicting hunks only; rest still merges |
-s ours | discards the other branch's changes entirely |
3. Mergetool
Aborting and Avoiding Conflicts
1. Bail Out Cleanly
- Abort restores the state from before the operation
- Use the command that matches what you started
bash
git merge --abort git rebase --abort git cherry-pick --abort
each one resets branch, index and files
2. Bad Resolutions
- Committing a file that still has markers is the classic mistake
- Run
git diff --checkbefore every commit
| Already committed | Undo with |
|---|---|
| A bad merge | git reset --hard ORIG_HEAD |
| Anything else | git reflog, then reset to the earlier entry |
3. Avoid Conflicts
Merge Conflict Cheat Sheet
1. Inspect
bash
git status git diff git diff --name-only --diff-filter=U git log --merge
U = still unmerged
2. Resolve and pick a side
| Merge | Rebase | |
|---|---|---|
| Finish | git commit | git rebase --continue |
--ours | your branch | upstream |
--theirs | merged-in branch | your commit |
bash
git checkout --theirs <file> git add <file>
edit, then git add marks it resolved
3. Escape and setup
| Escape | Command |
|---|---|
| Merge |
Check yourself
During git rebase main you want your own commit's version of app.py. Which flag?
git checkout --theirs app.py- Rebase flips sides: theirs is your replayed commit
- Then
git add,git rebase --continue
After the merge commit, tests fail with a syntax error at a ======= line. What went wrong?
- Conflict markers were committed unresolved
git addmarks a file resolved without checking its content- Run
git diff --checkbefore staging
Mid-rebase you fixed a file and ran git add. What next?
git rebase --continue- No
git commitneeded here - Repeat for each conflicting commit
git merge -X theirs feature vs git merge -s ours feature: what differs?
-X theirs: conflicting hunks take feature, the rest still merges-s ours: records the merge but keeps your tree unchanged