Sub topic 2
What a Plain Rebase Replays, and Why It Is a Special Case of --onto
This chapter assumes you can commit, branch, switch, read git log, and merge from the command line. You should also know that a branch is a movable pointer to a commit. You do not need to have used git rebase before. By the end you will know exactly which commits a plain git rebase copies and where it puts them. You will also see why that command is shorthand for the three-argument --onto form.
The setup: two branches that diverged
A plain rebase answers one question: "My branch started from an older point on another branch. How do I make it look as if I had started from the current tip?"
Here is the usual starting point. You created topic from main. Both branches have since gained commits.
Before:
T1---T2 topic
/
A---B---M1---M2 mainB is the commit where the two histories split. It is the merge base, the most recent commit reachable from both branches. After a plain rebase, the history looks like this:
After:
A---B---M1---M2 main
\
T1'---T2' topicT1' and T2' carry the same changes as T1 and T2, but they are new commits with new IDs. The old T1 and T2 are not edited. They are left behind, and nothing but the reflog points at them.
What Git does, step by step
When you run git rebase main while topic is checked out, Git performs these steps.
- It finds the commits to replay. These are the commits reachable from
topicbut not frommain. Git writes this asmain..topic. In the example that isT1andT2. - It sets those commits aside as a to-do list, oldest first.
- It moves
HEADto the new base,main, in detached mode. Your working tree now matchesM2. - It replays each commit from the list in order. For each one, it computes the change that commit introduced relative to its parent and applies that change on top of the current
HEAD. This is the same operation asgit cherry-pick. Each successful replay creates a new commit. - It moves the
topicbranch pointer to the last new commit and reattachesHEADtotopic.
The diagram shows the sequence.
Three consequences follow from this model.
- Commit IDs change. A commit's ID is a hash of its contents, its parent, its author and committer, and its message. A new parent means a new ID, so every replayed commit gets a new one. The author name and author date are kept. The committer date is set to the time of the replay.
- The files can end up different. Each commit is replayed as a change applied to a new base. If
mainhas edited the same lines, you get a conflict. If it has edited different lines, the result still combines both sets of changes. The result is not guaranteed to be what you tested before the rebase. - The old commits still exist. Git does not delete them. Until they expire from the reflog and are garbage collected, you can get them back. The recovery chapter covers that.
Try it yourself
This script builds the example history in a scratch directory and runs a plain rebase. It needs Git 2.28 or later because of git init -b.
set -e
git init -q -b main demo
cd demo
git config user.name "Demo"
git config user.email "[email protected]"
echo base > base.txt && git add base.txt && git commit -q -m "B"
git switch -q -c topic
echo t1 > t1.txt && git add t1.txt && git commit -q -m "T1"
echo t2 > t2.txt && git add t2.txt && git commit -q -m "T2"
git switch -q main
echo m1 > m1.txt && git add m1.txt && git commit -q -m "M1"
echo m2 > m2.txt && git add m2.txt && git commit -q -m "M2"
git switch -q topic
echo "Commits that will be replayed:"
git log --oneline main..topic
git rebase main
echo "History after the rebase:"
git log --oneline --graph --decorate
echo "Old tip of topic (saved in ORIG_HEAD):"
git rev-parse --short ORIG_HEADThe first git log lists T2 and T1, the two commits in main..topic. After the rebase, the graph is a straight line: T2, T1, M2, M1, B. The IDs of T1 and T2 differ from the ones listed before. The last command prints the ID of the old topic tip, which is what ORIG_HEAD recorded when the rebase started.
The argument is doing two jobs
In git rebase main, the word main is called the upstream. It does two jobs at once.
| Job | What it decides | In the example |
|---|---|---|
| Boundary | Which commits to replay: those reachable from the branch but not from the upstream | `T1` and `T2` |
| Destination | Where the replayed commits are placed | On top of `M2` |
A plain rebase uses one value for both jobs. That is convenient when the branch you want to follow is also the branch you started from. It is a limitation when the two differ. The next chapter looks at what the upstream does as a boundary in more detail. For now, notice that nothing forces the two jobs to share a value.
--onto separates them. The full form is:
git rebase --onto <newbase> <upstream> <branch><newbase>is the destination. The replayed commits are placed on top of it.<upstream>is the boundary. Git replays<upstream>..<branch>.<branch>is the branch to move. Git switches to it first, so you do not need to check it out beforehand.
If you leave out --onto <newbase>, Git uses <upstream> as the new base. That default is the plain rebase.
The same operation written both ways
# 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 forms select main..topic and place the copies on top of main. The second form also checks out topic for you, so the explicit git switch is not needed there. The table maps one form onto the other.
| Plain form | Equivalent --onto form | Meaning |
|---|---|---|
| `git rebase main` | `git rebase --onto main main` | Move the current branch onto `main` |
| `git rebase main topic` | `git rebase --onto main main topic` | Check out `topic`, then move it onto `main` |
You can confirm the equivalence in the demo repository. The next commands rewind topic to its pre-rebase state and repeat the operation in the explicit form. The final lines compare the resulting file trees.
first_tree=$(git rev-parse 'topic^{tree}')
# Go back to the state before the first rebase.
git reset --hard ORIG_HEAD
# Repeat the operation with the explicit --onto form.
git rebase --onto main main topic
second_tree=$(git rev-parse 'topic^{tree}')
if [ "$first_tree" = "$second_tree" ]; then
echo "Same file contents after both forms"
fiThe trees match because the same changes were applied to the same base. The commit IDs may or may not match, depending on whether the committer timestamps fall in the same second. The file contents are what matter here.
What a plain rebase skips
Step 1 above is slightly more selective than "everything in main..topic". While replaying, Git compares each commit's change with the commits that are already on the upstream side. If the same change is already there, even under a different commit ID, Git leaves that commit out. This happens, for example, when someone cherry-picked one of your commits onto main.
Merge commits are also left out by default. A plain rebase flattens the range into a straight line of ordinary commits. If your branch contains merges that you want to preserve, add --rebase-merges. This chapter does not use that option.
When the plain form is the wrong tool
The plain form works only when the boundary and the destination are the same branch. These situations need --onto.
- Your branch was started from another feature branch, and you want it on
mainwithout bringing the other feature's commits along. - You want to remove a few commits from the middle of a branch.
- Your branch was started from a branch that has since been squash-merged, so the original commits no longer match anything on
main.
All three are the topics of later chapters. In each, the boundary and the destination are different commits, which is why the third argument is needed.
Defaults you may meet
You will sometimes see git rebase with no argument at all. Git then uses the upstream that the current branch tracks, shown by git rev-parse --abbrev-ref @{upstream}. A branch has a tracking upstream if you created it with git switch -c topic origin/topic or ran git push -u. If the branch tracks nothing, Git stops with an error that says there is no tracking information.
This is a different use of the word "upstream" from the one in <upstream>..<branch>, though the two meet in the no-argument form. In this tutorial, "upstream" in a rebase command means the boundary argument unless the text says "tracking upstream".
Summary of the model
- A plain rebase replays
upstream..branch, oldest commit first, on top of the upstream. - Replayed commits are copies with new IDs. The originals remain until they are garbage collected.
git rebase Xis the same asgit rebase --onto X X.--ontolets you pick the destination and the boundary independently.
Exercise
You are on branch topic, started from main. Without running anything, write the --onto form that is equivalent to git rebase main, and then the form that is equivalent to git rebase main topic. Which commits does Git replay in each case?
Show answer
git rebase main is equivalent to git rebase --onto main main. git rebase main topic is equivalent to git rebase --onto main main topic, which also checks out topic first. In both cases Git replays the commits in main..topic, meaning those reachable from topic but not from main. It places them on top of main, oldest first, then moves topic to the last copy.
Exercise
After a plain rebase, git log shows the same commit messages as before, but git rev-parse gives different IDs for the commits. Why? Give two reasons that the new commits are different objects from the originals.
Show answer
A commit ID is a hash of the commit's contents, including its parent ID and committer information. The replayed commits have a new parent (the new base, or the previous copy), so their hashes change. The committer timestamp is also set at replay time. The commit messages, the author, and the introduced changes stay the same.