Rebase Onto and Stacked Branches

Updated · 10 min read
hardGit 2.53gitrebase-ontoupdate-refsstacked-branchesinterview

git rebase --onto is a form of rebase that lets you choose exactly which commits to move and where to put them. A plain git rebase main moves every commit of your branch that isn't on main. With --onto, you give a starting point as well, and Git moves only the commits after it. It is used to fix a branch that was started from the wrong place, to remove commits from the middle of a branch, and to move one branch of a chain of dependent branches. --update-refs goes with it: it keeps such a chain of stacked branches together while rebasing.

Picture a train with several carriages. A plain rebase uncouples all of your carriages and attaches them to a new engine. --onto lets you say: "uncouple only the carriages after this one, and attach them to that engine instead".

Syntax

Terminal
git rebase --onto <newbase> <upstream> [<branch>]
PartDescription
<newbase>It is used to name the commit the moved commits will sit on.
<upstream>It is used to mark where to start: commits after it (not including it) are moved.
<branch>It is used to name the branch to move. Git switches to it first. Defaults to the current branch.

In words: "take the commits on <branch> that come after <upstream>, and replay them on <newbase>." A plain git rebase main is the same as git rebase --onto main main.

Example 1: A Branch Started from the Wrong Place

Navika started feature/hostel from main and made two commits. Then she began receipts, but forgot to switch back to main first, so feature/receipts was created on top of the unfinished hostel work:

Terminal
mkdir fee-tracker
cd fee-tracker
git init --quiet
echo 'Term 1 fee: $12,000' > fees.txt
git add fees.txt
git commit --quiet -m "Add the term fee"
git switch --quiet -c feature/hostel
echo 'Hostel fee: $9,000' >> fees.txt
git commit --quiet -am "Add the hostel fee"
echo "Hostel fee is per term" >> fees.txt
git commit --quiet -am "Explain the hostel fee"
git switch --quiet -c feature/receipts
echo "Receipt No. 1" > receipts.txt
git add receipts.txt
git commit --quiet -m "Add receipt numbers"
git log --oneline --graph --all
Output
* 4f3a6f5 (HEAD -> feature/receipts) Add receipt numbers
* 5bbe131 (feature/hostel) Explain the hostel fee
* 472cbfd Add the hostel fee
* 6b651a0 (main) Add the term fee

The receipts feature is ready, but the hostel feature isn't. A pull request from feature/receipts would include both hostel commits. A plain git rebase main wouldn't help either: it would move all three commits, because none of them is on main. She needs only the commits after feature/hostel to move to main:

Terminal
git rebase --onto main feature/hostel feature/receipts
Output
Successfully rebased and updated refs/heads/feature/receipts.
Terminal
git log --oneline --graph --all
Output
* 771a6a4 (HEAD -> feature/receipts) Add receipt numbers
| * 5bbe131 (feature/hostel) Explain the hostel fee
| * 472cbfd Add the hostel fee
|/
* 6b651a0 (main) Add the term fee

Explanation: --onto main is the new base. feature/hostel is the starting point: the commits after it on feature/receipts, just "Add receipt numbers", were moved. The receipts branch now starts from main, and the hostel branch is untouched.

Only the commits after feature/hostel move to main

Example 2: Remove Commits from the Middle

--onto can also drop commits. Navika's branch has three commits, and the middle one, a debugging line, shouldn't be there:

Terminal
git switch --quiet -c feature/totals main
echo "Total = term fees" > totals.txt
git add totals.txt
git commit --quiet -m "Add the total"
echo "DEBUG: print every fee" >> totals.txt
git commit --quiet -am "Print fees for debugging"
echo "Rounded to the nearest dollar" > rounding.txt
git add rounding.txt
git commit --quiet -m "Round the total"
git log --oneline -3
Output
42c6b95 (HEAD -> feature/totals) Round the total
95afb40 Print fees for debugging
e86ecf7 Add the total

To drop "Print fees for debugging" (HEAD~1), she moves the commits after it onto the commit before it (HEAD~2):

Terminal
git rebase --onto HEAD~2 HEAD~1
Output
Successfully rebased and updated refs/heads/feature/totals.
Terminal
git log --oneline -3
cat totals.txt
Output
50008c2 (HEAD -> feature/totals) Round the total
e86ecf7 Add the total
6b651a0 (main) Add the term fee
Total = term fees

Explanation: the commits after HEAD~1 (only "Round the total") were replayed onto HEAD~2 ("Add the total"), so the debugging commit is no longer in the branch. Note that this only works cleanly when later commits don't depend on the dropped one. Interactive rebase, in the next module, does the same with a list you can edit.

Stacked Branches and --update-refs

Large features are often split into stacked branches: part-1 adds the data, part-2 builds on it, and each is reviewed as its own pull request. When main moves, the whole stack has to be rebased. A rebase only moves the branch you are on, so rebasing part-2 leaves part-1 behind on the old commits.

--update-refs (Git 2.38 and later) fixes that: while rebasing, it also moves every other branch that points to one of the commits being replayed.

Terminal
git switch --quiet -c part-1 main
echo 'Exam fee: $800' >> fees.txt
git commit --quiet -am "Add the exam fee"
git switch --quiet -c part-2
echo "Exam receipts use the same numbers" >> fees.txt
git commit --quiet -am "Print receipts for the exam fee"
git switch --quiet main
echo "Fee Tracker" > README.md
git add README.md
git commit --quiet -m "Add the README"
git switch --quiet part-2
git log --oneline --graph main part-2
Output
* 2f814ea (main) Add the README
| * a579326 (HEAD -> part-2) Print receipts for the exam fee
| * 51238be (part-1) Add the exam fee
|/
* 6b651a0 Add the term fee

part-1 is part of part-2's history. Navika rebases the top of the stack onto main, with --update-refs:

Terminal
git rebase --update-refs main
Output
Successfully rebased and updated refs/heads/part-2.
Updated the following refs with --update-refs:
	refs/heads/part-1
Terminal
git log --oneline --graph main part-1 part-2
Output
* 1630f85 (HEAD -> part-2) Print receipts for the exam fee
* 91eaa22 (part-1) Add the exam fee
* 2f814ea (main) Add the README
* 6b651a0 Add the term fee

Explanation: Git replayed both commits on top of main, and because part-1 pointed to one of them, it moved part-1 to that commit's copy, as the message says. Both branches are now up to date and still stacked. Without --update-refs, part-1 would still point to the old commit, and its pull request would show stale work. Set git config --global rebase.updateRefs true to make this the default.

Common Mistakes

Mixing up the order of the arguments. It is --onto <newbase> <upstream> <branch>: where to, after which commit, which branch.

Giving the commit you want to move as <upstream>. <upstream> itself is not moved, only what comes after it. Name the commit before the first one you want.

Dropping a commit that later commits depend on. The rebase stops with conflicts. Use --abort, or resolve them knowing the dropped change is gone.

Rebasing one branch of a stack without --update-refs. The other branches stay on the old commits. Use --update-refs, or turn it on in the configuration.

Using --onto on shared branches. It is still a rebase, so the golden rule applies.

Under the Hood

Every rebase has three inputs: the new base, the set of commits to move, and the branch to update. A plain git rebase main calculates the commits as main..HEAD and uses main as the new base. --onto simply separates the two: the commits are <upstream>..<branch>, and the new base is whatever you name. That's why the same option can move a branch to another base (Example 1) or cut commits out (Example 2): in both cases, Git replays the commits in a range onto a commit of your choice.

--update-refs works by adding update-ref steps to the rebase's list of actions: after replaying a commit that a branch pointed to, Git records the new position, and updates all those branches at the end, together.

Interview Corner

What does git rebase --onto do?

git rebase --onto <newbase> <upstream> <branch> replays the commits of <branch> that come after <upstream> onto <newbase>. It lets you choose the range of commits to move and their new base independently.

Your feature branch was created from another feature branch by mistake. How do you move it onto main?

git rebase --onto main <other-feature> <your-feature>. Only the commits after <other-feature> are moved onto main.

How can you remove a commit from the middle of a branch with rebase?

git rebase --onto <commit-before-it> <the-commit> replays everything after the unwanted commit onto the commit before it, which removes it. git rebase -i with drop does the same interactively.

What are stacked branches, and what does --update-refs do?

Stacked branches are branches built on top of each other, such as part-2 based on part-1. git rebase --update-refs moves every branch that points to a commit being rebased, so the whole stack stays consistent after one rebase.

Practice

Challenge: move the hostel work. The hostel feature is now ready, but it should go on top of the updated main, including the README commit. Rebase feature/hostel onto main, and print its log.

What does Git print? Before you open the solution, decide whether you need --onto here, and how many commits are replayed.

Show solution
Terminal
git switch --quiet feature/hostel
git rebase main
Output
Successfully rebased and updated refs/heads/feature/hostel.
Terminal
git log --oneline -4
Output
e092fb8 (HEAD -> feature/hostel) Explain the hostel fee
133a42c Add the hostel fee
2f814ea (main) Add the README
6b651a0 Add the term fee

No --onto needed: all of feature/hostel's own commits should move, and that is exactly what a plain git rebase main does. Its two hostel commits were replayed on top of the README commit.

Checkpoint. In a practice repository, create a branch from the wrong branch and fix it with --onto. Then build a stack of three branches, move main on, and rebase the top branch with --update-refs. Check with git log --oneline --graph --all that all three moved.

Quick check: Rebase onto

4 questions
1In git rebase --onto main feature/hostel feature/receipts, which commits move?
2What does git rebase --onto HEAD~2 HEAD~1 do on a three-commit branch?
3What does --update-refs change?
4Since which Git version is --update-refs available?

Key Takeaways

Quick reference

CommandIt is used to
git rebase --onto main old-base featureMove feature's own commits from old-base to main
git rebase --onto HEAD~2 HEAD~1Drop the commit HEAD~1
git rebase --update-refs mainRebase a stack and move every branch in it
git config --global rebase.updateRefs trueAlways update stacked branches
git push --force-with-lease origin part-1 part-2Push a rebased stack

Was this tutorial helpful?

Lesson 56 of 106