branching

git rebase

Replays commits from your branch on top of another base, producing a linear history without merge commits. Powerful for cleaning up history before pushing.

git rebase [<options>] [<upstream> [<branch>]]

Common flags

Flag Purpose
-i / --interactive Edit, reorder, squash, or drop commits interactively
--onto <newbase> Rebase onto a specific commit, transplanting range
--continue Resume after resolving conflicts
--abort Cancel and return to pre-rebase state
--autosquash Auto-arrange fixup!/squash! commits during interactive rebase
--autostash Stash and reapply local changes automatically

Examples

git rebase main

Replay current branch commits on top of main

git rebase -i HEAD~5

Interactively edit the last five commits

git rebase --onto main feature-old feature-new

Move commits from one base to another

Gotcha

Never rebase commits that have been pushed to a shared branch — it rewrites history and breaks every collaborator's clone.

Related commands

← All Git commands · Git cheat sheet · Git rebase vs merge