Sub topic 6
Moving a Branch Off a Feature Branch and Onto main
This chapter assumes you can commit, branch, switch, and read git log. It also assumes you know that a branch is a movable pointer to a commit. You should know what a plain git rebase main does, though you don't need to be fluent with it.
The situation
You started topic from next, an unfinished feature branch, instead of from main. topic has two commits of its own. Now you want topic to depend only on main, so it can be reviewed and merged without next.
A plain git rebase main would not help. Git would replay every commit on topic that is not on main. That includes the three commits that belong to next.
The three arguments
--onto separates two jobs that a plain rebase combines:
git rebase --onto <newbase> <upstream> <branch><newbase>is where the copied commits are placed.<upstream>bounds the range. Git copies the commits reachable from<branch>but not from<upstream>. In range notation, that isupstream..branch.<branch>is the branch to move.
For our case:
# Before: topic was started from next, not from main.
#
# o---o---o---o---o main
# \
# o---o---o next
# \
# o---o topic
git rebase --onto main next topic
# After: only the commits in next..topic are replayed onto main.
#
# o---o---o---o---o main
# | \
# | o'---o' topic
# \
# o---o---o nextThe primed commits (o') are new commits. They have the same changes and messages as the originals but different parents, so they have different hashes. The next branch is not touched.
Preview, run, verify
Because --onto rewrites history, check the range first. The output of git log next..topic is exactly the list of commits Git will replay.
# List the commits that --onto will replay.
git log --oneline next..topic
# Run the rebase.
git rebase --onto main next topic
# Inspect the new shape of the history.
git log --oneline --graph --decorate main next topicIf the preview shows commits that are not yours, your upstream is wrong. Stop before running the rebase.
After the rebase, git log --oneline main..topic should show only your two commits. next..topic is no longer a useful check, because topic no longer contains next.
A plain rebase is a special case
Passing <branch> makes Git switch to it first. These two forms are identical:
# Plain rebase: the upstream is also the new base.
git switch topic
git rebase main
# The same operation written out with --onto.
git rebase --onto main main topicA plain rebase uses main for both jobs. --onto lets you give each job a different value.
Common mistakes
- Swapping the arguments. The new base comes first and the upstream second. Running
git rebase --onto next main topicwould placetopiconnext. - Using the wrong upstream. If you give
mainas the upstream, Git replays all ofmain..topic, including thenextcommits. - Skipping the preview. One
git logcommand catches both of the mistakes above.
Summary of the workflow
- Name the three pieces: where it goes (
main), what it currently sits on (next), and what moves (topic). - Run
git log --oneline next..topicand confirm it lists only your commits. - Run
git rebase --onto main next topic. - Check
git log --oneline --graph --decorate main next topic.
Exercise
A branch fix was started from feature-x, not from main. git log --oneline feature-x..fix shows 2 commits. Write the command that moves those 2 commits onto main, and say which branch is not changed.
Show answer
git rebase --onto main feature-x fix. The feature-x branch is not changed. Only fix is rewritten, and it now contains copies of its 2 commits on top of main.
Exercise
You run git rebase --onto main main topic while topic has no commits from any other feature branch. Why does this give the same result as git rebase main?
Show answer
A plain git rebase main uses main as both the new base and the upstream. Writing --onto main main topic supplies the same value for both, so the two commands copy the same range, main..topic, onto the same base.