Interactive Rebase and Squashing Commits
10 min read
Rewriting History Interactively
Interactive rebase (git rebase -i) opens an editor listing your commits. You can reorder, edit, squash, or drop individual commits before they become part of the permanent history.
bash
git rebase -i HEAD~4 # interactively edit the last 4 commitsThe editor shows lines like pick abc1234 add login form. Change the action keyword to modify behaviour:
pick— keep the commit as-is.reword— keep the commit but edit its message.squash(s) — meld this commit into the previous one, combining messages.fixup(f) — like squash but discard this commit's message.drop(d) — remove the commit entirely.
Squashing Example
During development you might create "wip", "fix typo", and "actually fix bug" commits. Before opening a PR, squash these into one clean commit.
text
pick abc1234 add login form
squash def5678 fix typo in login form
squash ghi9012 handle empty password edge caseAt CPCODELAB, you don't need to obsess over squashing — the merge strategy on GitHub does a final squash anyway. But practising interactive rebase is a valuable skill for cleaning up messy branches before senior review.
Ready to test yourself?
Practice Git & GitHub— quiz & coding exercises