Git Interactive Rebase
Rewrite, reorder, squash, and clean up commits before sharing them
Overview
Interactive rebase (git rebase -i) opens an editor listing commits so you can reorder, edit, squash, or drop them before publishing. It is the primary tool for polishing a messy feature branch into a clean, reviewable history. Only rebase commits that have not been shared, since rewriting published history disrupts collaborators.
Syntax / Usage
Start the interactive session against a base, then choose an action per commit in the todo list.
# Edit the last 4 commits
git rebase -i HEAD~4
# Rebase everything since diverging from main
git rebase -i origin/main
# In the editor, replace "pick" with an action:
# reword - change the commit message
# edit - pause to amend the commit
# squash - merge into previous, combine messages
# fixup - merge into previous, discard message
# drop - remove the commit entirely
Examples
Squash three work-in-progress commits into one clean commit:
git rebase -i HEAD~3
# mark the first as "pick", the rest as "fixup"
# result: a single commit with the first commit's message
Split or amend an older commit using edit:
git rebase -i HEAD~5
# set the target commit to "edit"; the rebase pauses there
git commit --amend
git rebase --continue
Reorder commits so a fix lands before the feature that needs it—just rearrange the lines in the todo list and save.
Common Mistakes
- Rebasing commits already pushed and pulled by teammates
- Force pushing with
--forceinstead of the safer--force-with-lease - Deleting a commit with
dropwhen you meant to squash it - Getting stuck mid-rebase and forgetting
git rebase --abortto reset - Squashing so aggressively that meaningful history and context are lost
See Also
rebase commit branch