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
git merge
Joins the history of another branch into the current one, creating a merge commit when histories diverge. Preserves the full branching topology.
git cherry-pick
Applies the changes from one or more specific commits onto the current branch as new commits. Useful for porting bug fixes across branches.
git reset
Moves the current branch tip to a specified commit, optionally updating the index and working tree. Used to unstage files or rewind local history.
git branch
Lists, creates, renames, or deletes branches — pointers to commits. Does not switch branches on its own; use switch or checkout for that.
git checkout
Legacy multipurpose command that switches branches, restores files, or detaches HEAD to a commit. In modern Git (2.23+), prefer 'git switch' for branches and 'git restore' for files.