Sub topic 4
Reading and writing git rebase --onto
This chapter shows how to read a git rebase --onto command and how to write one from a description of what you want. It assumes you can commit, branch, switch, and read git log output. It also assumes you know a branch is a movable pointer to a commit, and that you have seen a plain git rebase main. Later chapters apply the command to specific jobs. Here the goal is to understand the three arguments well enough that you never have to guess.
The shape of the command
git rebase --onto <newbase> <upstream> <branch>Each argument answers a different question.
| Argument | Question it answers | Typical value |
|---|---|---|
| `<newbase>` | Where should the copied commits be placed? | `main`, `origin/main`, a commit ID |
| `<upstream>` | Which commits should be left behind? | The old base: a branch, tag, or commit ID |
| `<branch>` | Which branch should be moved? | The branch with the commits you want to keep |
The split between <newbase> and <upstream> is the point of --onto. A plain git rebase main uses main for both jobs. With --onto, the destination and the cut-off point are separate, so you can place commits somewhere other than the commit you measured from.
How Git picks the commits
Git does not move commits. It never edits an existing commit, because a commit's ID depends on its contents and its parent. It makes new copies, in four steps.
- Compute the range
<upstream>..<branch>. This is every commit reachable from<branch>that is not reachable from<upstream>. The commits that get replayed are exactly the ones in this range. - Switch to
<branch>, if you named it, and then detachHEADat<newbase>. - Apply the range's changes one commit at a time, oldest first, as if by
git cherry-pick. Each copy keeps the original message and author but has a new parent, so it gets a new ID. - Point
<branch>at the last copy.
The original commits are not deleted. They stay in the object database until garbage collection, and the old branch tip stays reachable through ORIG_HEAD and the reflog. A later chapter uses that for recovery.
If a commit in the range makes the same change as a commit already in <newbase>'s history, Git recognizes the patch as identical and skips it. This helps when part of your branch was already merged by a cherry-pick or a rebase. It does not help after a squash merge, because the squashed commit's patch is different. The chapter on restacking covers that case.
A plain rebase is a special case
When you run git rebase main on topic, Git fills in the missing pieces for you:
# 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 topicThe range is main..topic, the commits on topic that main lacks. They are replayed onto main. --onto lets you break the link between the two roles, so the range can be bounded by one commit while the destination is another.
A worked example you can run
The following script builds a small repository in which topic was started from next, not from main. It uses bash and a helper that creates one new file per commit, so the replay never conflicts.
git init -q -b main demo && cd demo
c() { echo "$1" > "$1.txt"; git add "$1.txt"; git commit -q -m "$1"; }
c m1; c m2 # shared start of main
git switch -q -c next
c n1; c n2; c n3 # work on next
git switch -q -c topic
c t1; c t2 # topic is built on top of next
git switch -q main
c m3; c m4 # main moves on without next
git switch -q topicThe history now looks like this, with main at m4, next at n3, and topic at t2:
m1---m2---m3---m4 main
\
n1---n2---n3 next
\
t1---t2 topicYou want topic to contain only t1 and t2, sitting on top of main, without the n commits. Name the three arguments from that sentence. The new base is main. The commits to leave behind are everything up to next, so the upstream is next. The branch to move is topic.
git rebase --onto main next topicThe range next..topic is t1 and t2, so only those two are copied:
m1---m2---m3---m4 main
\ \
\ t1'---t2' topic
\
n1---n2---n3 nextnext is untouched. topic now has the new commits t1' and t2' on top of m4.
Check the range before you run it
The range upstream..branch is exactly what git rebase --onto replays, so you can read it first with git log. If the list is longer or shorter than you expected, your upstream is wrong, and you have not changed anything yet.
# 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 topicIn the demo, the first command prints two lines, t2 and t1. After the rebase, git log --oneline main..topic prints the same two messages with different IDs. That change of IDs is how you can tell the commits were copied.
Leaving out the branch argument
The third argument is optional. Without it, Git rebases the branch you are currently on.
git switch topic
git rebase --onto main nextThis is the same as the full form. Naming the branch is slightly safer when you write commands into notes or scripts, because it does not depend on which branch happens to be checked out. It also saves you from the extra git switch.
Arguments can be any commit expression
<newbase> and <upstream> accept anything Git can resolve to a commit: branch names, remote-tracking branches such as origin/main, tags, full or abbreviated commit IDs, and expressions such as HEAD~3 or topic~2. Commit IDs are useful when the old base branch has since been deleted or moved, and you only have the ID in your reflog or terminal history.
# Replay everything after commit 4f2a9c1 onto the current origin/main.
git rebase --onto origin/main 4f2a9c1 topicRun git fetch first when the new base is a remote-tracking branch. Otherwise you rebase onto whatever your last fetch saw.
Reading someone else's command
To read an unfamiliar --onto command, go left to right with three questions:
- Where do the copies land? That is the first argument.
- What is subtracted from the branch? That is the second argument, with everything reachable from it.
- What moves? That is the third argument, or the current branch if it is missing.
For example, git rebase --onto release v2.1 hotfix means: take the commits on hotfix that are not in v2.1, and replay them on release.
Common mistakes
- Swapping the first two arguments. Writing
git rebase --onto next main topicbounds the range bymainand places the copies onnext. Ifmainhas diverged, the range can include commits you did not mean to move. Preview withgit log --oneline <upstream>..<branch>to catch this. - Using the wrong upstream. If you pick a commit that is too old, the range includes extra commits and you copy more than you intended. If it is too new, you leave behind work you wanted. The log preview shows both problems.
- Using an upstream that has been rewritten. If
nextwas itself rebased or amended, the originalnextcommits thattopicstill contains are no longer reachable from the newnext. They fall insidenext..topic, so they are copied again as duplicates. Use the old tip ofnext, found in the reflog, as the upstream. - Forgetting that the branch moves. The command changes
topiconly. If other branches point into the replayed range, they keep pointing at the old commits, and a later chapter shows how--update-refshandles that. - Rebasing commits others have built on. Copies have new IDs. Anyone who based work on the old commits will have to untangle the result, so rebase only commits that you alone depend on, or that your team has agreed to rewrite.
Exercise
A branch feature-x was started from release, and you now want only its own commits placed on top of main. Write the command, and write the git log command that previews the commits that will be replayed.
Show answer
Run git rebase --onto main release feature-x. Preview with git log --oneline release..feature-x. The new base is main, the upstream release removes the commits that feature-x inherited, and feature-x is the branch being moved.
Exercise
What is the exact --onto form of git rebase main when you are on topic, and why does it show that a plain rebase is a special case?
Show answer
It is git rebase --onto main main topic. The upstream and the new base are the same commit, so the range main..topic is replayed onto main. --onto only matters when you want the new base to differ from the upstream.
Exercise
You run git rebase --onto main next topic, and the log preview showed six commits when you expected two. Name two likely causes.
Show answer
The upstream may be wrong, for example too old to exclude all of the inherited commits. Or next may have been rewritten since topic was started, so the original next commits are no longer reachable from it and fall inside next..topic. Find the old tip of next in the reflog and use it as the upstream.