Difference between 'rebase master' and 'rebase --onto master' from a branch derived from a branch of master
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
The difference between git rebase master and git rebase --onto master ... only becomes interesting when your current branch was not created directly from master. If your branch was created from another feature branch, plain rebase master and rebase --onto master upstream current select different commit ranges to replay.
Example Branch Shape
Suppose history looks like this:
You are currently on feature-work.
The key fact is that feature-work was branched from feature-base, not from master.
What git rebase master Does
If you run:
Git finds the merge base between feature-work and master, which here is C. Then it takes every commit reachable from feature-work that is not reachable from master and replays them onto master.
That means Git will try to replay:
- '
D' - '
E' - '
F' - '
G'
So after the rebase, feature-work effectively includes both the feature-base commits and its own commits on top of master.
That is correct if you really want to transplant the entire combined line of work.
What git rebase --onto master feature-base feature-work Does
Now consider:
This means:
- take commits reachable from
feature-work - but exclude commits reachable from
feature-base - replay the remaining commits onto
master
So Git replays only:
- '
F' - '
G'
The feature-base commits D and E are not copied, because feature-base is explicitly named as the cutoff point.
Afterward, the history conceptually becomes:
Now feature-work depends directly on master, not on feature-base.
Why --onto Matters
The power of --onto is that it separates:
- the new base you want
- the old boundary where replay should start
Plain rebase master only answers the first question. --onto lets you answer both.
That is why --onto is the right tool when you want to “peel off” a sub-branch from a longer feature stack and move only that sub-branch somewhere else.
When to Use Each Form
Use plain:
when the current branch should carry along everything since its merge base with master.
Use:
when the current branch was stacked on another branch and you want only the commits unique to the current branch.
This is common when:
- an intermediate feature branch is not going to merge
- you want to retarget a child branch directly onto
master - you are cleaning up a stacked branch workflow
Think in Terms of Commit Sets
The easiest mental model is:
- '
git rebase masterreplays “everything on my branch since divergence from master”' - '
git rebase --onto master feature-base feature-workreplays “everything on feature-work after feature-base”'
That framing is more useful than memorizing syntax alone.
Common Pitfalls
- Using plain
rebase masteron a stacked branch and accidentally replaying commits from the parent feature branch too. - Forgetting which branch name should be the old cutoff in the
--ontocommand. - Running rebase without first drawing the commit graph or checking it with
git log --graph --oneline --decorate. - Assuming
--ontois only for advanced edge cases when it is actually the clean solution for stacked branches. - Rebasing shared public branches without coordinating with anyone who already based work on them.
Summary
- '
git rebase masterreplays all commits on the current branch that are not already onmaster.' - On a branch derived from another feature branch, that can include parent-branch commits you did not intend to move separately.
- '
git rebase --onto master feature-base feature-workreplays only the commits unique tofeature-workafterfeature-base.' - '
--ontois the right tool for retargeting stacked branches.' - Draw the commit graph first so you know exactly which commits each rebase form will select.
Related reading
- Difference between stdmerge and stdinplace_merge?
- Differences between Git-scm, msysGit Git for Windows
- Differences between git pull origin master git pull origin/master
- Differences between git remote update and fetch?
- Differences between git submodule and subtree
- Disable warning about detached HEAD
- Display last git commit comment
- django migrations - workflow with multiple dev branches
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseNo course covers this one yet
Basis Lab writes one for you from a sentence about what you want to be able to do, then teaches it and checks you understood. Your first course is free.
Build my courseInterview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.