Git Rebase Onto
Replay a chosen range of commits on a new base, not every commit since the branches split.
A plain git rebase main replays every commit on your branch that main does not have. git rebase --onto lets you say where that range starts. You use it when the branch carries commits you no longer want, such as a parent branch that was squash merged.
Introduction
Rebase takes a list of commits and recreates them on top of another commit. Without --onto, Git builds that list from one argument and uses the same argument as the new base. That works while the base branch still contains your branch's history. It fails when the base was rewritten, because Git can no longer tell which commits belong to you.
--onto separates the two jobs. One argument says where the commits go. Another says which commits to leave behind.
Understanding the Concept
The full form takes three arguments:
git rebase --onto <newbase> <upstream> <branch>
Definition
Newbase
The commit the replayed commits are placed on. Any commit works, not only a branch name.
Definition
Upstream
The commit that marks where the range starts. Git replays the commits reachable from the branch but not from upstream.
Git first checks out <branch>. It then collects the commits in <upstream>..<branch>, resets the branch to <newbase>, and applies those commits one at a time. Leave out <branch> and Git uses the current branch.
| Command | Commits replayed | Placed on |
|---|---|---|
git rebase main | main..HEAD | main |
git rebase --onto main part-1 | part-1..HEAD | main |
git rebase --onto main part-1 part-2 | part-1..part-2 | main, after checking out part-2 |
git rebase --onto HEAD~2 HEAD~1 | HEAD~1..HEAD | HEAD~2, which drops the commit at HEAD~1 |
The Git documentation states that <newbase> "May be any valid commit, and not just an existing branch name." The same holds for <upstream>. When the old parent branch is gone, pass its last commit hash from the reflog instead.
Applying It in Practice
Moving a branch off a squash-merged base. part-2 was built on part-1. part-1 then landed on main as one squashed commit. The examples below ran on Git 2.43.0 in a scratch repository:
$ git log --oneline --graph --all
* 273b8f8 Add parser (#1)
| * bbbd1d8 Document CLI flag
| * cf39507 Add CLI flag
| * 68ab8cb Fix parser review comments
| * b09391d Add parser
|/
* 94784c9 Initial commit
main has the squashed commit 273b8f8, but not b09391d or 68ab8cb. A plain rebase therefore tries to replay all four commits and stops on the first one:
$ git rebase main
Auto-merging a.txt
CONFLICT (add/add): Merge conflict in a.txt
error: could not apply b09391d... Add parser
Name part-1 as the upstream so its commits stay behind:
$ git rebase --onto main part-1 part-2
Successfully rebased and updated refs/heads/part-2.
$ git log --oneline --graph main part-2
* 8a2ca3a Document CLI flag
* 334a206 Add CLI flag
* 273b8f8 Add parser (#1)
* 94784c9 Initial commit
Only the two part-2 commits moved. Rebase a Stacked Branch After a Squash Merge is the short fix for this case.
Moving a whole stack. With three branches stacked as part-1, part-2, part-3, a plain --onto from the top moves only part-3. part-2 keeps pointing at the old commits. Add --update-refs and Git moves every local branch inside the rebased range:
$ git switch part-3
$ git rebase --onto main part-1 --update-refs
Successfully rebased and updated refs/heads/part-3.
Updated the following refs with --update-refs:
refs/heads/part-2
Push each moved branch with --force-with-lease. Rebase a Whole Stack With --update-refs covers making that option the default.
Dropping commits. git rebase --onto HEAD~2 HEAD~1 replays only the top commit onto HEAD~2, which removes the commit in between:
$ git log --oneline -3
5a893da Real fix
7901c07 Debug logging
2c6584e Add parser (#1)
$ git rebase --onto HEAD~2 HEAD~1
Successfully rebased and updated refs/heads/main.
$ git log --oneline -2
8bae080 Real fix
2c6584e Add parser (#1)
Engineering Considerations
Pick the upstream with care. It must be the last commit you want to leave out. Pass the wrong one and Git either replays commits that already landed or silently drops some of your own. Run git log --oneline <upstream>..<branch> first. That command lists exactly the commits the rebase will replay.
Rebase rewrites commit IDs. Every commit after the new base gets a new hash, as cf39507 became 334a206 above. Anyone who based work on the old commits has to rebase too, so restrict rewrites to branches you own or a stack of pull requests you restack together.
--update-refs skips any branch checked out in another worktree. The Git documentation says so directly. Agents working in separate Git worktrees keep their branches on the old commits until each one rebases on its own.
Scaling and Operations
A stack of five branches means four --onto decisions after each squash merge, and each needs the right upstream. Teams that run several agents at once hit this daily. The usual answer is a tool that records each branch's parent, so the upstream is never guessed.
Treq records that parent for you. A workspace can target another workspace's branch. When the pull request below merges, the workspace above moves onto the merged workspace's own target, with only its own commits. That is the same result as git rebase --onto, without picking the upstream by hand.
Next Steps
- Merge vs Rebase: when to replay commits and when to join histories
- What are Stacked PRs?: build and restack dependent branches
- Cherry-pick vs Rebase: copy single commits instead of moving a branch
- Auto-rebase AI Branches: keep agent branches current as their base moves