Sub topic 8
Choosing commit sets with A..B and A...B
This chapter assumes you can run git log with --oneline, know that each commit points to its parent, and have two branches that have diverged. A revision range tells git log which commits to list. The two range forms, A..B and A...B, answer different questions.
Start from the graph
A range is a set operation on the commit graph. Git starts from the commits you name and follows parent links backward. The examples below use this history:
Here main points to C and feature points to E. Commit B is the merge base, the most recent commit that both branches share.
Two dots: reachable from B but not from A
A..B means "commits reachable from B that are not reachable from A". It is the usual way to ask "what does this branch have that the other one lacks?"
git log main..feature --onelineThis lists E and D. Commit B is excluded because main already reaches it. Commit C is not listed because feature cannot reach it.
The order matters. git log feature..main --oneline lists only C. If the output is empty or shows the wrong commits, check whether you swapped the endpoints.
Two dots is shorthand for exclusion with a caret. These commands are equivalent:
git log main..feature --oneline
git log feature ^main --oneline
git log ^main feature --onelineThe ^ prefix means "not reachable from this commit". You can use it more than once, for example git log feature ^main ^release. That is useful when no single .. expresses the set you want.
If you leave one side empty, Git uses HEAD for it. git log main.. is the same as git log main..HEAD.
Three dots: reachable from either side, but not both
A...B means "commits reachable from A or B, but not from both". This is the symmetric difference. Each side shows its own commits since the branches split.
git log main...feature --oneline --left-rightThis lists C, D and E, but not A or B. With --left-right, each line starts with < if the commit is on the left side (main) or > if it is on the right (feature). C gets <, while D and E get >.
The same syntax means something else in git diff
git diff compares two snapshots, not sets of commits, so the dots mean something different there.
git diff main..feature # same as: git diff main feature
git diff main...feature # merge base of main and feature, versus featuregit diff main..feature compares the tips. It shows C's changes in reverse, because feature lacks them, together with D's and E's changes. git diff main...feature compares B with E. It shows only what feature added, which matches what a merge would bring in. For reviewing a branch, three dots is normally what you want.
| Form | In `git log` | In `git diff` |
|---|---|---|
| `A..B` | Commits in B but not in A | Snapshot A versus snapshot B |
| `A...B` | Commits in either side but not both | Merge base of A and B versus B |
Common mistakes
- Reading
main..featureas "difference between the branches". It excludes anything only onmain. - Using
git diff main..featureto review a branch. Work thatmaingained after the split shows up as removals. - Forgetting that a range limits only which commits are listed. Options such as
--authoror a path after--still apply on top of it.
Exercise
Using the history in the diagram, which commits does git log feature..main --oneline list, and what does git diff main...feature compare?
Show answer
It lists only C, the one commit on main that feature lacks. The three-dot diff compares the merge base B with the tip of feature, E, so it shows the changes made in D and E.