Skip to main content
Branching & Merging

git rebase

Reapply commits from your current branch onto another base

Practical Example

git checkout feature-login
git rebase main
# Replays feature commits on top of main
# For interactive: git rebase -i HEAD~3
# To abort a bad rebase: git rebase --abort

Command Overview & How It Works

Rewrites commit history by taking commits from your branch and replaying them one by one on top of another branch. The result is a linear history — it looks like all work was done sequentially on the same branch. Use git rebase main while on a feature branch to incorporate the latest main changes without creating a merge commit. Use git rebase -i (interactive rebase) to squash, reorder, edit, or drop commits — a powerful tool for cleaning up history before sharing it. Use git rebase --onto to move a range of commits to a completely different parent. The golden rule of rebasing: never rebase commits that have been pushed to a shared branch or that others have based work on. Rebase rewrites commit hashes, so force-pushing a rebased branch disrupts collaborators.

Practical Tips

  • Never rebase commits that others have already pulled — it rewrites hashes and breaks their history.
  • Use git rebase -i HEAD~n to squash, reword, reorder, or drop recent commits before pushing them.
  • If a rebase goes sideways, git rebase --abort restores your branch to its pre-rebase state.

Related Commands in Branching & Merging