Git
Rebase
Version Control
Software Development
Branching

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.

Browse interview questions

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:

text
1A---B---C  master
2         \
3          D---E  feature-base
4               \
5                F---G  feature-work

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:

bash
git checkout feature-work
git rebase master

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:

bash
git checkout feature-work
git rebase --onto master feature-base feature-work

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:

text
1A---B---C  master
2         \
3          D---E  feature-base
4         \
5          F'---G'  feature-work

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:

bash
git rebase master

when the current branch should carry along everything since its merge base with master.

Use:

bash
git rebase --onto master feature-base feature-work

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 master replays “everything on my branch since divergence from master”'
  • 'git rebase --onto master feature-base feature-work replays “everything on feature-work after feature-base”'

That framing is more useful than memorizing syntax alone.

Common Pitfalls

  • Using plain rebase master on a stacked branch and accidentally replaying commits from the parent feature branch too.
  • Forgetting which branch name should be the old cutoff in the --onto command.
  • Running rebase without first drawing the commit graph or checking it with git log --graph --oneline --decorate.
  • Assuming --onto is 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 master replays all commits on the current branch that are not already on master.'
  • 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-work replays only the commits unique to feature-work after feature-base.'
  • '--onto is the right tool for retargeting stacked branches.'
  • Draw the commit graph first so you know exactly which commits each rebase form will select.

Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Basis Lab
No 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 course
Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions