Sub topic 5
Checking the Commit Range Before and After
This chapter assumes you know how git rebase --onto <newbase> <upstream> <branch> is read. The upstream argument bounds the range upstream..branch, and those commits are copied onto the new base. You also need git log, branch creation and a clean working tree. The chapter covers how to check which commits will move before you rebase, and how to confirm afterwards that exactly those commits moved.
Why check at all
A wrong --onto command does not fail. It succeeds and gives you a history that looks plausible and is wrong. The usual causes are:
- An upstream that is too old replays commits you meant to leave behind.
- An upstream that is too new silently drops commits you meant to keep.
- A branch name in the wrong position moves a different branch.
The rebase rewrites history, so a mistake costs time even when you can undo it. Previewing is cheap, and it catches most of these mistakes.
The workflow has four steps.
Before: preview the range
Git replays the commits reachable from branch but not reachable from upstream. That is the two-dot range upstream..branch. git log accepts the same notation, so you can list the set directly:
git log --oneline next..topicRead the output as the exact list of commits that will be copied. Check three things:
- The first line is the tip of your branch.
- The last line is the first commit that belongs to your work.
- No commit in the list comes from the branch you are leaving.
Use the commit count as a quick check, and compare it with what you expect:
git rev-list --count next..topicTwo more previews are worth running when the range is long.
# Merge commits in the range. A default rebase does not keep them.
git log --oneline --merges next..topic
# Total change that the range introduces, as a file summary.
git diff --stat next...topicThe three-dot form in the second command is deliberate. In git diff, next...topic compares topic with the merge base of next and topic. That is the net change your range carries, which is what you want to see.
By default, rebase flattens merge commits and drops them from the replay. If the merge preview prints anything, decide whether that matters before you continue. Git can also skip a commit in the range when the new base already contains an identical change. So the real replay can be shorter than the preview, never longer.
Look at the shape of the graph
The list shows which commits move. The graph shows where they sit relative to the other branches:
git log --oneline --graph --decorate main next topicListing the branches by name limits the output to the history you care about. Look for the point where topic leaves next, and for where main sits compared with that point.
Record the old tip
A rebase abandons the old commits. They stay in the repository and can be recovered, but only if you can name them. Give them a name first:
git branch backup/topic topicThe branch is cheap, and it gives you a stable name to compare against afterwards. You can delete it when you are satisfied.
A scratch repository to practice on
This script builds a small history that matches the diagrams in this tutorial. topic was started from next, and main has moved on since then. Run it in an empty directory.
git init -q -b main onto-demo
cd onto-demo
git config user.name "Demo"
git config user.email "[email protected]"
c() { echo "$1" > "$1.txt"; git add "$1.txt"; git commit -q -m "$1"; }
c m1
git switch -q -c next
c n1
c n2
git switch -q -c topic
c t1
c t2
git switch -q main
c m2
c m3The history is now m1, m2, m3 on main, m1, n1, n2 on next, and t1, t2 stacked on n2 on topic. Preview the range:
git log --oneline next..topic
git rev-list --count next..topicThe log prints two lines, for t2 and t1, and the count is 2. The commits n1 and n2 are not in the list, because next already contains them. This is the result you want, since the goal is to move t1 and t2 and leave next behind.
Save a backup and run the rebase:
git branch backup/topic topic
git rebase --onto main next topicAfter: verify the result
Run these checks in order. Each one catches a different kind of mistake.
Check the count
git rev-list --count main..topicThe new range from the new base should hold as many commits as the old range did. Here the result is 2. A different number means something was dropped, skipped or added.
Check ancestry
git merge-base --is-ancestor main topic && echo "main is below topic"
git merge-base --is-ancestor next topic || echo "next is not below topic"git merge-base --is-ancestor A B exits with status 0 when A is an ancestor of B, and with status 1 when it is not. It prints nothing itself, so the && and || supply the messages. You expect the first test to succeed and the second to fail. If next is still an ancestor of topic, the branch was not moved off it.
Check the files
git ls-filesThe list contains m1.txt, m2.txt, m3.txt, t1.txt and t2.txt. The files n1.txt and n2.txt must be absent. This is the check that matters most, because it shows that next's work did not come along with the branch.
Compare the patches with git range-diff
The previous checks look at the shape of the result. git range-diff compares the content of each commit before and after:
git range-diff next..backup/topic main..topicIt takes two ranges, the old one and the new one, and pairs up their commits. The output has one line per pair. An = between the two numbers means the commit's patch is identical before and after. A ! means the patch changed. A < means a commit exists only in the old range and a > means a commit exists only in the new range.
For a clean move like this one, every line shows =. For a rebase that hit conflicts, a ! marks the commits where your resolution changed the patch. Read those with extra care, because they are the places where you made decisions.
The same command is the quickest check for a dropped commit. A < line in the output names it.
Look at the graph again
git log --oneline --graph --decorate main next topictopic now sits directly on main's tip, and next still ends at n2 with its own history untouched. Compare the picture with the one you took before the rebase.
Common errors and what they look like
| Symptom in the preview | Likely cause | Fix |
|---|---|---|
| The list includes commits from the old parent branch | The upstream is too old, or is a different branch than the one you started from | Name the parent branch or the last parent commit as the upstream |
| The list is empty | The upstream already contains the branch tip, or the branch name is wrong | Check `git log --oneline --graph --decorate` and swap the arguments |
| The list is shorter than expected | The upstream is too new and sits inside your own work | Move the upstream back to the commit just before your first commit, for example `topic~3` |
| The count after rebase differs from before | A commit was skipped as already applied, or a merge commit was flattened away | Run `git range-diff` and look for `<` lines |
| `next` is still an ancestor of `topic` afterwards | The new base was given the wrong branch | Undo and rerun with the correct first argument |
The third row is the failure to watch for. A too-new upstream is not an error, because Git does what you asked. The commits between the real start and your upstream are never replayed, so they disappear from the branch. Removing commits that way is useful on purpose, and a later chapter uses it deliberately. As an accident it only shows up in the preview or in git range-diff.
Clean up
When the checks pass, remove the backup:
git branch -D backup/topic-D is needed because Git sees the old commits as unmerged. Keep the backup until you have pushed and the branch has been reviewed, if you want a longer safety margin.
Summary of the habit
- Preview with
git log --oneline upstream..branchandgit rev-list --count upstream..branch. - Name the old tip with a backup branch.
- After the rebase, check the count, the ancestry, the file list and
git range-diff.
Exercise
You run git rebase --onto main feature-a feature-b. Before running it, git rev-list --count feature-a..feature-b printed 4. After it finishes, git rev-list --count main..feature-b prints 3, and git range-diff feature-a..backup/feature-b main..feature-b shows one line starting with <. What does that line mean, and what are two possible reasons for it?
Show answer
The < line is a commit that exists in the old range but has no matching commit in the new range, so it was not replayed. One reason is that main already contained an identical change, so Git skipped it as already applied. The other is that the commit was a merge commit, which a default rebase flattens away. You can tell the two apart with git log --oneline --merges feature-a..backup/feature-b.
Exercise
In the scratch repository, topic has been rebased onto main. Which two commands show, without opening any file, that n1 and n2 did not come along?
Show answer
git merge-base --is-ancestor next topic || echo "next is not below topic" prints the message, because next is no longer an ancestor of topic. git ls-files lists the files in the working tree and does not include n1.txt or n2.txt. git log --oneline main..topic also works, and shows only t1 and t2.