Sub topic 11
Comparing branches: two-dot versus three-dot diff
This chapter assumes you can create branches and commit, and that you have read the chapters on revision ranges and on what git diff compares. You will learn how A..B and A...B behave in git log and in git diff. They behave differently in each, and mixing them up is a common source of confusion.
The merge base
Two branches that have diverged share a common ancestor. Git calls the best such ancestor the merge base. Suppose main and feature both started from commit C. After C, each branch got its own commits.
Two dots in git log
In git log, main..feature means "reachable from feature but not from main". In the diagram, that is F1 and F2.
git log main..feature --onelineThis answers the question "what does feature have that main lacks?" The three-dot form main...feature in git log shows the symmetric difference: commits reachable from either branch but not from both. Here that is M1, M2, F1, and F2.
Two dots in git diff
git diff compares two snapshots. It does not walk a range. So git diff main..feature is the same as git diff main feature. It compares the tip of main with the tip of feature directly.
git diff main..feature --statThe result includes your feature work (F1, F2). It also shows the reverse of anything that landed on main after the branches diverged (M1, M2). Those changes appear as if feature had deleted them.
Three dots in git diff
In git diff, main...feature compares feature with the merge base of the two branches. Git finds C and then diffs C against feature.
git diff main...feature --statThe output shows only what feature changed since it diverged. Work that arrived on main is left out. This is usually what a reviewer wants to see before a merge.
Summary of the meanings
The meaning of the dots flips between the two commands. In git diff, the three-dot form uses the merge base. In git log, the three-dot form shows the symmetric difference.
| Command | Meaning |
|---|---|
| `git log main..feature` | Commits on `feature` not on `main` |
| `git log main...feature` | Commits on either branch but not both |
| `git diff main..feature` | Tip of `main` against tip of `feature` |
| `git diff main...feature` | Merge base against tip of `feature` |
Checking the merge base yourself
You can print the merge base and diff against it. This is the same as the three-dot form.
git merge-base main feature
git diff $(git merge-base main feature) feature --statExercise
A teammate's branch feature was created from main last week. Since then, main gained a new file deploy.sh. You run git diff main..feature and see deploy.sh listed as deleted. Why does this happen, and which command shows only the teammate's changes?
Show answer
Two-dot diff compares the tip of main directly with the tip of feature. feature has no deploy.sh, so the comparison reports it as removed, even though the teammate never touched it. Run git diff main...feature. It compares feature with the merge base, which does not include deploy.sh, so only the teammate's changes appear.
Exercise
What does git log main...feature --oneline list, and how does that differ from git log main..feature --oneline?
Show answer
The three-dot form lists commits reachable from either branch but not both: the commits unique to main plus the commits unique to feature. The two-dot form lists only commits reachable from feature that are not reachable from main.