Sub topic 9
Moving a range of commits with --onto
This chapter assumes you have used git rebase on a single branch, know that a branch is a pointer to a commit, and have Git 2.38 or newer installed. It covers the three-argument form of rebase that lets you choose exactly which commits move and where they land.
The plain rebase has one fixed idea of "which commits"
A plain git rebase main picks the commits to replay by itself. It takes everything reachable from your current branch that is not reachable from main, then replays those commits on top of main.
That default is usually right. It fails when the commits you want to leave behind are not reachable from the new base, which happens after a squash merge, as the next chapter shows.
The three arguments of --onto
--onto separates two questions that a plain rebase answers together:
- Where should the commits land? This is the
--ontotarget. - Which commits move? This is the range after the target, written as
upstream(excluded) up to the branch you have checked out (included).
git rebase --onto <newbase> <upstream> [<branch>]Git replays the commits in upstream..branch on top of newbase. Commits at or before upstream are skipped. If you omit <branch>, Git uses the current branch.
A worked example
Suppose a stack looks like this, with feature-2 built on feature-1:
You want feature-2 to stop depending on feature-1 and sit directly on main. Run:
git switch feature-2
git rebase --onto main feature-1Git computes the range feature-1..feature-2, which is commits C and D. It replays them on main as new commits C' and D'. Commits A and B stay on feature-1, which is not touched.
You can preview the range before you run the rebase:
git log --oneline feature-1..feature-2If this list shows exactly the commits you expect to move, the rebase will move exactly those.
Combining --onto with --update-refs
--update-refs works with --onto. If your stack has three branches and you move the top two onto a new base, the middle branch pointer moves along with them.
git switch feature-3
git rebase --update-refs --onto main feature-1Here the range is feature-1..feature-3. It contains the commits of feature-2 and feature-3. Because feature-2 points at one of those commits, --update-refs moves it to its rewritten commit. After the rebase, feature-2 and feature-3 sit on main, and feature-1 stays where it was.
Common errors
- Wrong upstream, too many commits. If you name an upstream that is not in your branch history, Git replays far more commits than you intended. Check with
git log --oneline <upstream>..<branch>first. - Wrong upstream, too few commits. If you name an upstream that is too high in the stack, commits you wanted to keep are silently dropped from the new history. They remain reachable only from the old branch pointers and the reflog.
- Forgetting the branch is rewritten. The replayed commits get new IDs. Any remote copy of the branch must be force-pushed afterwards, ideally with
--force-with-lease. - Running it with a dirty working tree. Commit or stash first, or Git refuses to start.
Why this matters for stacks
Stacks make --onto useful because the commits you want to move are a slice of history, not a whole branch. Removing a branch from the middle, reordering branches, and restacking after a base branch is merged all come down to naming a slice and a destination.
Exercise
A stack is main, feature-1, feature-2, with feature-2 checked out. You run git rebase --onto main feature-1. Which commits are replayed, and what happens to feature-1?
Show answer
The commits in feature-1..feature-2, meaning the commits unique to feature-2, are replayed on top of main and get new IDs. feature-1 is not moved and keeps its original commits.
Exercise
What command shows you which commits git rebase --onto main feature-1 will move when feature-2 is checked out?
Show answer
git log --oneline feature-1..feature-2