Sub topic 13
Moving a range of commits with --onto
This chapter assumes you can read git log --graph output, know that a rebase creates new commits with new hashes, and have used git rebase origin/main. Here you will learn the three-argument form git rebase --onto, which moves a chosen range of commits to a new base.
The problem it solves
A plain git rebase main takes everything on your branch that main lacks and replays it on top of main. Sometimes you need more control. A common case is a branch built on top of another feature branch:
main: A---B
\
featureA: C---D
\
featureB: E---FfeatureB contains commits C and D from featureA, plus its own E and F. Suppose featureA is abandoned, or its commits reached main another way. You want E and F alone on top of main.
The three arguments
git rebase --onto main featureA featureBThe command reads as "replay onto main the commits after featureA, up to featureB":
| Argument | Role |
|---|---|
| `main` | The new base. Replayed commits are placed on top of it. |
| `featureA` | The upstream. Commits reachable from this are excluded. |
| `featureB` | The branch to move. If omitted, Git uses the current branch. |
The range replayed is featureA..featureB, meaning commits reachable from featureB but not from featureA. Here that is E and F. Git switches to featureB, then replays E and F on top of main. The branch pointer moves to the new commits.
E' and F' have the same changes as E and F but different hashes, because their parent changed. The original E and F still exist until Git garbage-collects them. You can reach them through the reflog.
Worked example
Check the layout first, so you know what the range contains.
git log --oneline --graph main featureA featureB
git log --oneline featureA..featureBThe second command lists exactly the commits that will move. If the list is wrong, change the upstream argument before you rebase. Then run the rebase:
git rebase --onto main featureA featureB
git log --oneline --graph main featureBConflicts behave as in any rebase. Resolve them, run git add, then git rebase --continue, or use --abort to go back.
Common uses
- Moving a branch off a parent branch that was squash-merged. Squashing creates a new commit, so a plain
git rebase mainwould try to replay C and D again and conflict with their squashed copy. Using--ontowith the old parent as upstream skips them. - Retargeting a branch that was started from the wrong base.
- Removing a stretch of commits from the middle of a branch.
git rebase --onto A Bdrops the commits after A up to B and keeps those after B.
Common errors
- Choosing the wrong upstream. Too early, and you replay commits you meant to leave behind. Too late, and you lose commits you wanted. Always preview with
git log upstream..branch. - Forgetting that the result has new hashes. If the branch was already pushed and others use it, read the rule on shared history before you rewrite it, and push with
--force-with-lease. - Mixing up the order. The new base comes first, then the upstream, then the branch.
Exercise
A branch featureB was created from featureA. featureA was then squash-merged into main. Write the command that moves only the commits unique to featureB onto main, and the command that previews them first.
Show answer
Preview with git log --oneline featureA..featureB. Then run git rebase --onto main featureA featureB. The upstream is featureA, so its commits are excluded, and only the commits unique to featureB are replayed onto main.
Exercise
In git rebase --onto main featureA featureB, what range of commits is replayed, and why do the replayed commits get new hashes?
Show answer
The range is featureA..featureB: commits reachable from featureB but not from featureA. They get new hashes because their parent commit changes to the tip of main, and a commit's hash depends on its parent.