git rebase
advancedRe-apply your commits on top of a new base — and rewrite history.
git rebaseWhat it does
git rebase takes your commits, lifts them off your branch's old base, and replays them onto the tip of another branch — one by one, as new commits. The result is a linear history. Because everything is re-created, every moved commit gets a brand-new hash.
before
after · linear
Each commit is re-created as a new object — h hashes rolled, history straight.
Watch it — press play
A narrated, step-by-step walkthrough that shows how to do it — not just theory. It adapts to the learner mode you picked in the top bar.
Step 1
Newer base, same ideas
You want your feature to sit on top of the newest main — not on a stale fork point.
git rebase mainfeature grew from the old main
Under the hood
Old commits are recreated, not copied
Rebase finds the common ancestor, collects your commits up to the branch tip, then for each one: reads its tree and message, re-diffs it against the new base, and writes a BRAND-NEW commit object. Identical content, different parent — different hash.
before: main ▸ a feature ▸ a — x — y after: main ▸ a — x — y (feature gone, replayed) feature ▸ a — x — y (x' and y' new hashes)
Every parent change rehashes the whole chain
Since a commit's hash depends on its parent, replacing the first parent re-rolls the hash of every descendant. That's why rebasing rewrites 'your' commits but never the branch you rebased onto.
The three-way apply per commit
For each commit, Git performs a mini three-way merge between the old base, your commit, and the new base. Conflicts pause the rebase so you can fix them before continuing.
Danger: you're editing shared history
Rebasing changes hashes. Anyone who already pulled your old-hashed commits will have a divergent history that requires force-push reconciliation. Rule: rebase local work, never shared work.
Aliases the pros type
Memorize the git rebase concept, then let one of these shortcuts make it instant. Aliases are real git config keys — add one with:
git config --global alias.st # → git rebaseg rbgit rebasePlain rebase shorthand.
g rbigit rebase -iInteractive rewrite — the power button.
g rb maingit rebase mainRe-sync your feature onto updated main.
Command anatomy
-iInteractive mode — reorder, squash, fixup, drop commits.
--onto <base>Replay commits onto an arbitrary new base.
--rootAlso rebase the very first commit (requires interaction).
--abortCancel the process and restore the original branch.
When to reach for it
Keeping a feature branch in sync with a fast-moving main before merging.
Squashing a pile of WIP commits into one clean commit with -i.
You want a perfectly linear history (GitHub squash-merge culture).
Pro tip
Squash or fixup is the killer feature of interactive rebase for messy work: `git rebase -i main` then change 'pick' to 'fixup' on the commits you want folded. Your heavy WIP history becomes one elegant commit — but only before pushing.