Sub topic 4
Reading and writing git rebase --onto
This chapter assumes you can commit, branch, switch, and read git log. It also assumes you know a branch is a movable pointer to a commit. A plain git rebase main helps, but you can learn the --onto form without it.
The three arguments
The full form takes three arguments:
git rebase --onto <newbase> <upstream> <branch>Each argument answers a different question.
| Argument | Question it answers | Example |
|---|---|---|
| `<newbase>` | Where should the commits end up? | `main` |
| `<upstream>` | Which commits should be left behind? | `next` |
| `<branch>` | Which branch is being moved? | `topic` |
Git selects the commits to replay with the range <upstream>..<branch>. That range means "reachable from <branch> but not reachable from <upstream>". Git copies each of those commits, in order, on top of <newbase>. Then it points <branch> at the last copy.
The <upstream> argument only draws a boundary. Nothing is moved onto it. This is the difference from a plain rebase, where one argument does both jobs.
Reading a command aloud
Take this history, where topic was started from next and not from main:
o---o---o---o---o main
\
o---o---o next
\
o---o topicNow read the command from right to left:
git rebase --onto main next topictopicis the branch to move.nextmarks which commits to leave behind: those reachable fromnext.mainis where the remaining commits are placed.
The remaining two commits, next..topic, are replayed on top of main. The next branch is not touched.
Where plain rebase fits
A plain rebase is the same operation with one name used twice:
# These two commands do the same thing.
git rebase main
git rebase --onto main main topicIn the second form, main is both the new base and the boundary. That is only correct when your branch was started from main. When it was started from somewhere else, you need to give the two roles different values.
Leaving out the branch
The last argument is optional. If you leave it out, Git rebases the branch you are on:
git switch topic
git rebase --onto main nextIf you give the branch, Git switches to it first. Either way, you finish on that branch.
Common mistakes
- Swapping
<newbase>and<upstream>. The new base comes first. Swapped, you replay the wrong range onto the wrong commit. The command can still succeed, so check the result. - Choosing an upstream that is too old. The range then includes commits you meant to leave behind. They are copied too, and you may get conflicts that do not belong to your work.
- Choosing an upstream that is too new. Commits you wanted to keep fall outside the range, and the rebase silently leaves them out.
- Expecting the old commits to vanish. The copies are new commits with new IDs. The originals stay reachable from other branches, such as
nextabove. They also stay in the reflog, which a later chapter uses for recovery.
A second reading
Consider this command:
git rebase --onto origin/main feature-a feature-bRead it as follows. Take the commits on feature-b that are not on feature-a. Place them on origin/main. Then move feature-b there. The branch feature-a is only a marker for where to stop copying.
Exercise
A branch fix was started from release, and you want only its own commits placed on top of main. Write the command, and name the argument that decides which commits are copied.
Show answer
git rebase --onto main release fix. The <upstream> argument, here release, decides which commits are copied. The range release..fix is replayed onto main, which is the new base.
Exercise
What does git rebase --onto main main topic do, and what shorter command is it equivalent to?
Show answer
It copies the commits in main..topic onto main and moves topic to the copies. It is equivalent to git rebase main run while on topic.