Sub topic 2
What a Plain Rebase Replays, and Why It Is a Special Case of --onto
This chapter assumes you can commit, create and switch branches, read git log, and merge. It explains what git rebase does internally, so the three-argument --onto form in the next chapters reads as a small extension and not a new command.
What rebase does
A commit in Git is immutable. It records a snapshot, a parent, an author, and a message. Changing the parent produces a different commit with a different ID. So Git cannot "move" a commit. Rebase copies commits instead.
Take a topic branch started from an older point on main, after which main moved on:
Running the rebase looks like this:
git switch topic
git rebase mainGit does four things:
- It works out which commits belong to
topicalone: here, E and F. - It saves them as a list of patches, oldest first.
- It resets
topicto the tip ofmain, commit D. - It re-applies each patch in order, making E' and then F'.
E' and F' have the same changes and messages as E and F. They have different parents, so they have different IDs. The old E and F still exist in the object database until garbage collection, which matters for recovery later.
Step 1: which commits are "yours"
The list in step 1 comes from a range expression. For git rebase main while on topic, Git selects the commits reachable from topic but not reachable from main. In log syntax, that is main..topic.
git log --oneline main..topicIn the example, this prints F and E. Commits A, B, and C are reachable from main, so they are excluded. Commit D is also excluded.
The argument you pass, main, is called the upstream. It has a role here: it marks where the replay range stops.
Step 3: where the commits land
In a plain rebase, Git has to decide where to put the copies. With no other instruction, it uses the upstream. So main plays two roles at once:
- It is the boundary of the commits to replay (
main..topic). - It is the new base the copies are placed on.
The same command with --onto
--onto lets you name the new base separately from the upstream. Its form is git rebase --onto <newbase> <upstream> <branch>. A plain rebase is the case where <newbase> and <upstream> are the same commit, and <branch> is the current branch:
# Plain rebase: the upstream is also the new base.
git switch topic
git rebase main
# The same operation written out with --onto.
git rebase --onto main main topicBoth commands produce the same history. The second one just spells out what the first one assumes. The topic at the end also does the work of git switch topic: Git checks it out before replaying.
| Argument | Plain `git rebase main` | Spelled out |
|---|---|---|
| New base | `main` (implied) | `--onto main` |
| Upstream (range boundary) | `main` | `main` |
| Branch to move | current branch | `topic` |
Why the distinction matters
Because the two roles share one argument, a plain rebase can only move a branch onto a commit that is also an ancestor boundary. That is fine when topic branched from main. It fails when topic branched from something else, such as another feature branch. If you ran git rebase main there, main..topic would include the other feature's commits too, and they would be copied along with yours.
Separating the two roles fixes this. You can set the boundary at the other feature branch, so only your own commits are replayed, and set the new base to main. The next chapters build on that.
Common mistakes
- Expecting the same commit IDs. After a rebase, every replayed commit has a new ID. Anyone who based work on the old IDs will see diverged history.
- Forgetting that the range is
upstream..branch. If the output ofgit log upstream..branchshows commits you did not write, the rebase will copy them too. - Treating
mainas the only valid upstream. It is only the default choice when the branch started frommain.
Exercise
You are on topic, which branched from main. Write the --onto form that is equivalent to git rebase main, and name the commits it replays.
Show answer
git rebase --onto main main topic. It replays the commits reachable from topic but not from main, which is the range main..topic, and places the copies on top of main.
Exercise
After a plain rebase of topic onto main, why do the replayed commits have different IDs from the originals, even though their changes are the same?
Show answer
A commit's ID is computed from its contents, including its parent. The copies have a new parent (the tip of main), so their IDs differ. The originals are not modified and stay in the object database.