Git Internals

Three-way merge mechanics

Understand how Git computes merges from base, ours, and theirs so conflicts and merge outcomes are easier to reason about.

Who This Is For
  • Readers building a durable Git mental model
  • Developers who keep running into history, ref, or recovery confusion
Prerequisites
  • Comfort reading basic Git output
  • A rough idea of commits, branches, and HEAD
Common Risks
  • Learning low-level terms without connecting them to commands
  • Collapsing objects, refs, and working state into one concept

Data & Performance

Key Quotes

Git performs a three-way merge by finding the common ancestor (merge base) of the two commits and using it as the baseline to combine the two sets of changes.

Citations & Further Reading

  1. Git merge [Official]
  2. Git merge base [Official]
  3. Git Branching Basic Branching and Merging [Book]

What you will learn

  • Understand the core purpose of Three-way merge mechanics
  • Master the basic usage and common options of Three-way merge mechanics
  • Understand how Git computes merges from base, ours, and theirs so conflicts and merge outcomes are easier to reason about.
  • Understand key concepts: Inputs
  • Know when to use this feature and when to avoid it

Git merge is a three-input computation: base, ours, and theirs.

Start with a problem

You use Git commands daily, but occasionally encounter 'strange' behavior — like being told a file changed when you didn't touch it, or unexpected conflicts during a rebase. You want to understand how Git works under the hood.

Inputs

  • base: common ancestor from merge-base
  • ours: current branch tip
  • theirs: merged branch tip

Git compares base→ours and base→theirs, then synthesizes result.

Three-way Merge MechanicsThree-way merge uses the tips of two branches and their common ancestor, attempting to automatically merge non-conflicting parts.
Before merge
main
ABC
feature
BDE
After merge
main
ABCM
feature
BDEM

Why conflicts appear

Conflicts occur when both sides changed overlapping regions and Git cannot choose semantics safely.

Practical takeaway

To resolve conflicts well, inspect base context instead of only choosing ours/theirs blindly.

Conflict markers are a decision point, not a parser failure

A conflict means Git needs human intent where automatic reconciliation is ambiguous.

Good follow-up reads

  1. merge-base and ancestry
  2. git-merge
  3. rerere for recurring conflicts

Try it yourself

  1. Practice the three-way-merge-mechanics command in a test repository and observe state changes before and after
  2. Experiment with different options and compare the output differences
  3. Simulate a real scenario where you would need to use this, and walk through the full process

Further reading

Keep going on the same topic: