Sub topic 9
Moving a range of commits with --onto
This chapter assumes you have used git rebase on a single branch and have read the earlier chapters on stacks and --update-refs. You need Git 2.38 or newer. The chapter explains the three-argument form of rebase. That form is what lets you restack a branch after its parent was squash-merged.
Two arguments you already know, one you may not
A plain rebase takes one argument:
git rebase mainGit reads this as "take the commits on my branch that are not on main, and replay them on top of main". The same argument plays two roles. It decides which commits to replay, and it decides where they land.
--onto separates those two roles:
git rebase --onto <newbase> <upstream> [<branch>]| Argument | Role |
|---|---|
| `<upstream>` | Selects the commits to move. Git replays everything reachable from the branch but not from `<upstream>`. |
| `<newbase>` | The commit the replayed commits are placed on top of. |
| `<branch>` | Optional. The branch to rebase. Git switches to it first. If you leave it out, Git rebases the current branch. |
The set of moved commits is the range <upstream>..<branch>. You may know that notation from git log. This is the same range. You can check what a rebase will move before you run it:
git log --oneline feature-1..feature-3Every commit in that output is replayed, and nothing else is. git rebase main is shorthand for git rebase --onto main main.
What happens internally
Git works out the range, then detaches HEAD at <newbase>. It then cherry-picks each commit of the range in order. When the last one is applied, Git moves the branch name to the new tip and reattaches HEAD to it. Every replayed commit gets a new ID, because its parent changed.
The commits in <upstream> and below are not touched. They stay where they were, and the rewritten commits no longer refer to them.
In this picture the command was git rebase --onto main B D, run with D's branch checked out. Commit B is the upstream, so C and D are the range. They are replayed on main, and B is left behind.
Pairing --onto with --update-refs
In a stack, the range often contains the tips of other branches. With --update-refs, Git moves each of those branches to its rewritten commit. This is the same behaviour you saw in the earlier chapter. --onto only changes where the range starts and where it lands.
The rule to remember is that only branches inside the replayed range are updated. A branch that points at the upstream commit or below it is not in the range, so Git leaves it alone.
Restacking after a squash merge
This is the case --onto exists for. Suppose the stack is main, feature-1, feature-2, feature-3, and feature-1 was squash-merged into main on the remote.
A squash merge creates one new commit on main that contains the combined changes of feature-1. That commit is not any of the commits on feature-1. Git sees two unrelated histories that happen to make the same change.
If you ran a plain git rebase origin/main from feature-3, Git would try to replay the feature-1 commits as well. Their changes are already in main inside the squash commit. Git can drop commits that match an upstream commit exactly (same patch), but a squash commit does not match any one of them. You would get conflicts, or duplicated changes, in code you have already merged.
--onto avoids the problem. You tell Git that everything up to and including feature-1 is not part of the move:
git fetch origin
git switch feature-3
git rebase --update-refs --onto origin/main feature-1The range is feature-1..feature-3. That contains the commits of feature-2 and feature-3 only. Git replays them onto the new origin/main, and --update-refs moves feature-2 along with feature-3.
You can try this safely in a throwaway repository. This script builds the stack, squash-merges the bottom branch into main, and restacks:
set -e
rm -rf onto-demo && git init -b main onto-demo && cd onto-demo
echo base > base.txt && git add . && git commit -m "Base"
git switch -c feature-1
echo a > a.txt && git add . && git commit -m "Add a"
git switch -c feature-2
echo b > b.txt && git add . && git commit -m "Add b"
git switch -c feature-3
echo c > c.txt && git add . && git commit -m "Add c"
# Simulate the squash merge of feature-1
git switch main
git merge --squash feature-1
git commit -m "Add a (squash)"
# Restack
git switch feature-3
git rebase --update-refs --onto main feature-1
git log --oneline --decorate --graphThe final log shows feature-2 and feature-3 on top of the squash commit on main. feature-1 still points at its old commit, off to the side.
Cleaning up the leftover branch
feature-1 was the upstream, so it is not in the range, and Git did not touch it. Its commits are now stale, and the work is in main. Delete it locally once you have checked the result:
git branch -D feature-1You need -D rather than -d because Git cannot see that the squash commit contains the branch's work, so it considers the branch unmerged. Check that git diff main feature-3 --stat shows only the changes you expect from feature-2 and feature-3 before you delete.
After the restack, push the changed branches with --force-with-lease, as in the earlier chapter:
git push --force-with-lease origin feature-2 feature-3Retarget the pull request for feature-2 to main if your hosting service has not done it for you.
Other uses of --onto
Because --onto separates what moves from where it lands, it solves several other problems.
Move one branch off its parent
Say feature-2 does not really need feature-1, and you want it to depend on main only:
git rebase --onto main feature-1 feature-2The range is feature-1..feature-2, which holds only the commits of feature-2. They are replayed on main. Note that feature-3 still sits on the old feature-2 commits. You now need to restack it too, with git rebase --onto feature-2 <old-feature-2-tip> feature-3. Take the old tip from the reflog (feature-2@{1}) or from a note you made before starting.
Remove a branch from the middle of a stack
To drop the commits of feature-2 and keep feature-3:
git rebase --onto feature-1 feature-2 feature-3The range is feature-2..feature-3. Those commits land directly on feature-1, and the commits of feature-2 are no longer in feature-3's history. If the later commits depended on code from feature-2, expect conflicts.
Take a few commits from the end of a branch
git rebase --onto main HEAD~3This moves the last three commits of the current branch onto main. It is the same idea as the first example, with a relative commit as the upstream.
Edge cases and common errors
- Wrong upstream, too many commits. If you name an upstream that is too old, the range is larger than you meant, and old commits are replayed. Always run
git log --oneline <upstream>..<branch>first and read the list. - Wrong upstream, too few commits. If the upstream is too new, commits you wanted are left behind and are no longer part of the branch. The reflog can bring them back.
- Branch name versus commit. After a squash merge you cannot name the squash commit as the upstream, because the old commits are not its ancestors. Name the old branch tip (here
feature-1). That is why you should keep the bottom branch until the restack is done. - Remote-tracking branches go stale. If you run
git rebase --onto origin/main ...withoutgit fetch originfirst, you rebase onto an oldorigin/main. Fetch first. - Empty commits. If a replayed commit makes changes that are already present on the new base, Git drops it as empty by default. Expect fewer commits after the rebase if part of the work was merged separately.
- Conflicts.
--ontoconflicts are resolved the same way as any rebase conflict. Fix the files,git addthem, then rungit rebase --continue. If you are unsure,git rebase --abortreturns every branch to its earlier state. - Worktrees.
--update-refsskips branches that are checked out in another worktree, so check that your stack branches are not open elsewhere.
Reading the command aloud
A useful habit is to turn the command into a sentence. git rebase --update-refs --onto origin/main feature-1 from feature-3 reads as: "Take everything after feature-1 up to feature-3, put it on origin/main, and move any branch names found along the way." If the sentence does not match what you intend, change the command before you run it.
Exercise
A stack is main, feature-1, feature-2, feature-3. feature-1 was squash-merged into main on origin, and you have run git fetch origin. You are on feature-3. Write the command that restacks feature-2 and feature-3 onto the new main, and explain why git rebase --update-refs origin/main would be the wrong choice.
Show answer
Run git rebase --update-refs --onto origin/main feature-1. The range feature-1..feature-3 contains only the commits of feature-2 and feature-3, and --update-refs moves feature-2 too. A plain git rebase --update-refs origin/main would also try to replay the commits of feature-1. Their changes are already in main inside the squash commit, which does not match any single original commit, so Git cannot drop them as duplicates and you would get conflicts or repeated changes.
Exercise
You are on feature-3 and want to remove feature-2's commits from the stack so that feature-3 sits directly on feature-1. Which command does this, and which range does it replay?
Show answer
git rebase --onto feature-1 feature-2 feature-3. It replays the range feature-2..feature-3, which is feature-3's own commits, onto feature-1. The commits of feature-2 are no longer in feature-3's history.