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:

VerbAction
pickKeep the commit exactly as-is (the default for every line)
rewordKeep the changes, but let you edit the commit message
squashMerge this commit into the previous one, combining both messages for you to edit
fixupLike squash, but silently discards this commit's message and keeps only the previous one's
dropDelete 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 to fixup (or squash if 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 by git rebase -i --autosquash even 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.