Sub topic 5
Checking the commit range before and after
This chapter assumes you can commit, branch, and read git log. It also assumes you know that git rebase --onto <newbase> <upstream> <branch> replays the commits in upstream..branch onto newbase. Most --onto mistakes come from a wrong upstream, so you will learn to check it before you run the command and to check the result afterwards.
Why the range needs a check
Git does not ask whether your upstream argument makes sense. It computes upstream..branch, which means "commits reachable from branch but not from upstream", and replays whatever it finds. Two failure modes follow:
- An
upstreamthat is too far back replays commits you meant to leave behind. These often duplicate work already on the new base and cause conflicts. - An
upstreamthat is too far forward silently leaves commits out of the rebased branch.
Both are cheap to catch before the rebase and awkward to untangle afterwards.
Preview the range with git log
The range that git rebase --onto replays is the same range git log prints. Run the log first, using the upstream and branch you plan to pass to the rebase.
# List the commits that --onto will replay.
git log --oneline next..topic
# Run the rebase.
git rebase --onto main next topic
# Inspect the new shape of the history.
git log --oneline --graph --decorate main next topicRead the preview output as a checklist:
- Every commit listed should be one you wrote for
topic. - No commit that belongs to
nextshould appear. - The count should match what you expect.
git rev-list --count next..topicgives the number directly.
If the list shows too many commits, move upstream forward. If it shows too few, move it back. Repeat the preview until the list is exactly the commits you want to carry.
What the preview looks like
Suppose topic was started from next, and next has three commits that are not in main. The preview for the correct upstream shows only the two commits on topic:
$ git log --oneline next..topic
c4f1a92 Add retry to the upload client
9b03e7d Add upload clientIf you had used main..topic by mistake, the output would also include the three commits from next. That is the sign that you would be copying someone else's work onto main.
Verify the result afterwards
After the rebase finishes, check three things.
First, check the shape. In the graph output from the example above, topic should sit directly on top of main, and next should be unchanged.
Second, check the count. The commits in main..topic should be the same number you saw in the preview.
git rev-list --count main..topicThird, compare the content. git range-diff pairs each old commit with its rewritten copy and shows what changed between them. The old tip is available as ORIG_HEAD right after the rebase.
git range-diff next ORIG_HEAD topicA commit shown with = is identical in content to its original. A commit marked ! differs, which is expected where you resolved a conflict. A < or > line means a commit exists only on one side, so one was dropped or added.
Diagram of the check
Common errors
| Symptom | Likely cause | Fix |
|---|---|---|
| Preview lists commits from the parent branch | `upstream` is the new base instead of the old base | Use the parent branch as `upstream` |
| Preview is empty | `upstream` already contains `branch`, or the names are swapped | Check argument order and run `git log --oneline --graph --all` |
| Many conflicts on commits you did not write | The range includes commits already on the new base | Abort and fix `upstream` |
| Commit count after is lower than the preview | An empty or duplicate commit was dropped | Compare with `git range-diff` |
Exercise
You plan to run git rebase --onto main next topic. Which command previews the commits that will be replayed, and what does it mean if the output includes commits that belong to next?
Show answer
Run git log --oneline next..topic. If commits from next appear, the range is wrong, because next..topic should list only commits unique to topic. Check that next is the branch topic was actually started from, and change the upstream argument before rebasing.
Exercise
A rebase has just finished. Which command shows whether each rewritten commit has the same content as the original, and which reference gives you the old branch tip?
Show answer
git range-diff next ORIG_HEAD topic compares the old and new commits. ORIG_HEAD holds the old tip until a later command overwrites it. The reflog, for example topic@{1}, also holds it.