Sub topic 3
How the upstream argument selects commits
This chapter assumes you can commit, branch, switch, and read git log, and that you know a branch is a movable pointer to a commit. You don't need to have used git rebase before.
Two jobs, one argument
Every rebase answers two questions:
- Which commits should be copied?
- Where should the copies go?
In a plain git rebase main, the single argument answers both. Git treats main as the place to put the copies and as the boundary of what to copy. The --onto form separates these jobs. The upstream argument answers the first question, and --onto <newbase> answers the second.
The range upstream..branch
Git selects the commits to copy with the range upstream..branch. This means "commits reachable from branch that are not reachable from upstream". A commit is reachable from a ref if you can get to it by following parent links back from that ref.
Walking back from branch, Git stops at anything it can also reach from upstream. Everything it passed before stopping is copied. The range is a set difference of two histories. It is not a diff of file contents.
Here topic was started from next, and next was started from main. Which commits move depends entirely on the upstream you name.
Try it
This script builds the history from the diagram. It uses empty commits, so there are no files to conflict. It assumes Git 2.28 or later for -b main.
git init -q -b main range-demo
cd range-demo
git commit -q --allow-empty -m "m1"
git commit -q --allow-empty -m "m2"
git switch -q -c next
git commit -q --allow-empty -m "n1"
git commit -q --allow-empty -m "n2"
git switch -q -c topic
git commit -q --allow-empty -m "t1"
git commit -q --allow-empty -m "t2"
echo "--- next..topic"
git log --oneline next..topic
echo "--- main..topic"
git log --oneline main..topicThe first range lists t2 and t1. The second lists t2, t1, n2, and n1. The upstream you choose decides how many commits move.
Now compare the two rebases. Do not run both on the same repository without undoing the first.
# Replays t1 and t2 only, and places them on main.
git rebase --onto main next topic
# A plain rebase would replay n1, n2, t1, and t2.
# git rebase main topicWith next as the upstream, n1 and n2 stay on next. With main as the upstream, copies of them would also land on topic, and next and topic would no longer share those commits.
What else Git skips or changes
- Commits already applied. Git compares the patch content of each commit in the range with commits on the new base. If an identical change is already there, it skips that commit. This is why a rebase after a regular merge or cherry-pick often replays fewer commits than the range shows.
- Merge commits. By default, merge commits in the range are dropped and their changes are replayed as a linear series. Use
--rebase-mergesif you need to keep the merge structure. - Copies, not moves. Each replayed commit gets a new hash, because its parent changed. The original commits stay in the repository until garbage collection removes them. A later chapter uses this to undo a rebase.
Common errors
A wrong upstream is the usual cause of a surprising rebase. Typical symptoms:
- Too many commits replayed. You named an upstream that is further back than the true fork point. Expect extra conflicts on commits you did not write.
- Too few commits replayed. You named an upstream that already contains some of your work. The missing commits stay behind on the old line.
- Empty range. If
upstream..branchhas no commits, the rebase has nothing to copy and finishes without changing the branch.
Run git log --oneline upstream..branch before every --onto rebase. If the list is not exactly what you intend to move, change the upstream first.
Exercise
A branch topic was started from next, which was started from main. You run git log --oneline main..topic and see six commits, but you wrote only two. Which upstream should you use to move only your two commits, and why?
Show answer
Use next as the upstream, for example git rebase --onto main next topic. The range next..topic excludes every commit reachable from next, so only the two commits unique to topic are replayed. The range main..topic also includes the four commits that belong to next.