Interactive Rebase - Tidy Your History
git rebase -i
You've already seen plain git rebase replay commits onto a new base. Interactive rebase uses that same replaying mechanism, but lets you edit your own recent (local, unpushed) commits along the way - reorder them, merge several into one, fix a typo in a message, or throw a commit away entirely, before anyone else ever sees them.
$ git rebase -i HEAD~4 # edit the last 4 commits
This opens a text editor listing those commits, oldest first, each with a verb you can change:
| Verb | Action |
|---|---|
pick | Keep the commit exactly as-is (the default for every line) |
reword | Keep the changes, but let you edit the commit message |
squash | Merge this commit into the previous one, combining both messages for you to edit |
fixup | Like squash, but silently discards this commit's message and keeps only the previous one's |
drop | Delete the commit entirely, as if it never happened |
| (reorder the lines) | Changes the order commits are replayed in |
Tip: The classic use case: you've got "add feature", "fix typo", "oops", "actually works now" as four separate local commits. Change the last three tofixup(orsquashif you want to keep/edit their messages) and rebase turns all four into one clean commit before you ever open a PR.git commit --fixup=<hash>followed bygit rebase -i --autosquasheven automates finding and ordering the right fixup commits for you.
Danger: Interactive rebase rewrites commit IDs, exactly like plain rebase - because it is a rebase under the hood, just with edits applied along the way. Only run it on local, unpushed commits, or a branch that's genuinely only ever used by you. Never rewrite shared history that someone else has already pulled and might have built more commits on top of.