Most developers think git merge just "combines two branches." You have your code, they have their code, merge smashes them together.
That model is wrong. And it breaks down the moment two people edit the same file.
The Problem With Two-Way Comparison
You changed line 5. They also changed line 5. If git only sees two versions, it has no idea what the original looked like. Did you add that line? Did they delete something? Without context, git is blind.
The Merge Base — A File You've Never Seen
So git cheats. It finds a third file: the merge base. This is the common ancestor — the last commit both branches shared before they diverged.
Think of it as the "before" photo. Your branch is one "after." Their branch is the other "after." Now git doesn't have to guess who changed what. It knows, because it can compare each side against the original.
Finding this ancestor is simple. Git walks the commit graph backward from both branch tips until the paths converge. That convergence point is your merge base.
Three-Way Diff
Now git has three versions of every file: base, yours, and theirs. It compares them line by line:
- Line same in all three? Keep it. Nobody touched it.
- Only you changed a line? Take yours.
- Only they changed it? Take theirs.
- Both made the exact same change? Keep it — you agree.
- Both changed the same line differently? Conflict.
That last case is the only one git can't solve alone. In a typical merge, 95% of lines resolve automatically. Git only flags the handful where both sides diverged from the base in different ways.
This is why three-way merge is so much better than two-way diff. Two-way diff would flag every difference between your file and theirs. Three-way diff only flags actual conflicts. The base file eliminates all the false alarms.
Conflict Resolution
When git hits a real conflict, it stops and asks you:
<<<<<<< yours
return user.name
=======
return user.displayName
>>>>>>> theirs
You are the merge algorithm now. Pick your version, pick theirs, combine them, or write something new. Git just needs you to remove the markers and save.
Once every conflict is resolved, git creates a merge commit — a commit with two parents. In the commit graph, this creates a diamond shape: two paths diverged and now reconverge into a single point.
Rebase: The Alternative
Merge isn't the only way. Rebase replays your commits on top of theirs, one by one. Same three-way diff under the hood, but the base changes every time.
The result: a clean, linear history. No diamond. No merge commit. Just a straight line.
The trade-off? Merge preserves what actually happened — two people worked in parallel. Rebase rewrites history to look sequential. Neither is better. Merge is honest. Rebase is clean. The best teams use both.
Watch the full animated breakdown: the merge base, three-way diff, conflict markers, and rebase — all visualized step by step.
Neural Download — visual mental models for the systems you use but don't fully understand.
Top comments (0)