Synopsis
git rebase [-i] [--onto <newbase>] [<upstream> [<branch>]]
Description
The git rebase command moves a sequence of commits to a new base. It rewrites history by replaying each commit on top of the new base, producing a linear chain rather than a merge commit. Rebasing is invaluable for keeping feature branches up to date, cleaning up local history before pushing, and squashing fixup commits.
Because rebasing creates new commits with different SHAs, never rebase commits that have been published and pulled by others — they will end up with diverging histories. The interactive mode (-i) lets you reorder, edit, squash, drop, or fix up commits.
In day-to-day use, git rebase integrates closely with shell aliases, editor plugins, and continuous integration. Power users often add aliases that combine flags they always pass, or wrap the command in scripts that enforce team conventions. Output formatting can be customized via Git config — pretty formats, color schemes, and pager behavior are all tunable. When something goes wrong, the first diagnostic step is usually to re-run the command with GIT_TRACE=1 in the environment, which reveals the underlying plumbing calls. For unusual situations, the --help output (git rebase --help) opens the full manual page with details on every option, including those rarely used in casual workflows but essential for debugging or scripting at scale.
Understanding how git rebase interacts with the rest of Git's data model — the object database, the index, refs, and the working tree — pays dividends. Each command operates on some subset of these pieces, and knowing which it touches helps predict outcomes and recover from mistakes. Reading the official Git documentation alongside hands-on practice in a throwaway repository is the fastest way to internalize the nuances. Most production issues with Git stem from one of three causes: surprising default behavior, partial network operations, or rewriting history that was already shared. A working mental model of git rebase's side effects helps avoid all three.
Common Options
| Option | Description |
|---|---|
-i, --interactive | Open an editor to manipulate the commit list. |
--onto <newbase> | Rebase onto an arbitrary commit, not the upstream. |
--continue | Resume after resolving conflicts. |
--abort | Cancel the rebase and return to the original state. |
--skip | Skip the current patch and continue. |
--autosquash | Automatically order fixup/squash commits. |
--autostash | Stash local changes before rebasing and pop after. |
-r, --rebase-merges | Preserve merge commits during rebase. |
Examples
git rebase main
# Replay current branch's commits on top of main
git rebase -i HEAD~5
# Interactively edit the last 5 commits
git rebase --onto main feature-base feature-tip
# Move only commits between feature-base and feature-tip onto main
git rebase --continue
# Resume after fixing conflicts
Common Mistakes
Rebasing public, shared branches breaks every collaborator's history. Reserve rebase for local cleanup. During conflict resolution, beginners often run git commit instead of git rebase --continue, leaving the rebase in a broken state. If you get lost mid-rebase, git rebase --abort always returns you to safety.
Related Commands
git merge, git cherry-pick, git reflog, git pull --rebase