Skip to content

Lesson 11 of 14 · 19m

Conflicts Are Three Files: markers, diff3, ours/theirs

Read stage 1/2/3 in the index, turn on diff3, survive the rebase ours/theirs flip, and let rerere remember.

When a merge or rebase stops on a conflict, git hasn't failed — it's asked a question it refuses to guess at. It loaded three versions of the file: the merge base (what the file looked like when your branches diverged), ours (your HEAD), and theirs (the incoming side). Hunks that changed on only one side apply cleanly. Hunks both sides rewrote become markers, and the index holds the file three times until you answer.

See the three versions

git ls-files -u during a conflict shows the index holding stage 1 (base), stage 2 (ours), stage 3 (theirs) entries for the path — resolution is literally "produce one stage-0 entry and git add it."

The default marker view shows only ours and theirs:

<<<<<<< HEAD
return session.ttl * 2;
=======
return session.ttl + grace;
>>>>>>> feature/session-extend

Set git config --global merge.conflictStyle diff3 and a third section appears — ||||||| shows the base version both sides started from. Base is the whole game: seeing that both sides changed ttl * 2 tells you the correct answer might be ttl + grace multiplied, or something neither side wrote. Without it you're choosing between two texts; with it you're reconciling two intents. Git 2.35 added zdiff3, which trims the common lines out of the markers for an even tighter view.

The tools

  • git checkout --ours <path> / --theirs <path> — take one side wholesale when the resolution is "theirs wins" or "mine wins."
  • git checkout -m <path> — recreate the conflict markers after a botched resolve.
  • git mergetool — opens your configured 3-way tool (vimdiff, Beyond Compare, etc.) on base/ours/theirs.
  • git diff mid-conflict shows the combined diff — both sides' changes against what you'd get.
  • git merge --abort / git rebase --abort / git cherry-pick --abort — every sequencer state has a fire escape that returns you to the start.

The ours/theirs flip that wastes afternoons

During a merge, ours is your branch, theirs is incoming — intuitive. During a rebase, you're replaying your commits onto someone else's base: "ours" is the branch you're rebasing onto and "theirs" is your own commit being replayed. git checkout --ours mid-rebase grabs main's version, not yours. The number of people who've resolved a rebase conflict backwards and shipped it is why this paragraph exists — the rule is "ours = where HEAD is standing," and during rebase HEAD is standing on the reconstructed base.

rerere: resolve it once

git config --global rerere.enabled true turns on reuse recorded resolution: git snapshots each conflict and your resolution in .git/rr-cache/, and replays it automatically the next time the same conflict appears. It's the difference between fighting a long-lived branch's merge once and fighting it every week — kernel maintainers lean on it heavily for repeated merges, and it turns rebase's per-commit conflict repetition into a one-time cost. git rerere status and git rerere diff show what it recorded.

Practice

  • Resolve while thinking, not while merging as fast as possible — a conflict settled by picking one side is a silent revert of the other. Read the base section, decide what the combined intent is, write it, then run the tests on the merged result.
  • Keep conflicts rare: rebase personal branches before merge day. A conflict between branches a day apart is a question; between branches a quarter apart is archaeology.
  • git diff --check before committing the resolution — it flags leftover markers and whitespace sins; CI catching <<<<<<< in main means someone skipped it.

Pitfalls

  • git checkout --theirs during a rebase grabs the upstream version — the flip above, in flag form.
  • "Both changes" resolutions need a compile + test; hunks that don't textually overlap can still break each other semantically (a renamed function, a changed signature).
  • git mergetool leaves .orig backups behind by default — git config mergetool.keepBackup false or remember to clean them.

Try this

Scratch repo: make both branches change the same function, git merge, set merge.conflictStyle diff3, rerun after git merge --abort. Resolve with the base section visible, git ls-files -u empty, then git add and commit.

Was this lesson helpful?

Sign in to rate

Keep reading

Related articles from the SudoSecurity blog.