CodeOath
← All posts
Git65 min total · 17 parts

Git Internals and Workflows: Branching, Merging, and Rebasing

Contents — Part 9 of 17: Interactive Rebase: Squash, Reword, Reorder
Part 9 of 17 · ~1 min

Interactive Rebase: Squash, Reword, Reorder

git rebase -i opens an editable list of the commits about to be replayed, letting you change more than just their base — you can squash several commits into one, reword commit messages, reorder commits, or drop them entirely, before they're replayed onto the new base:

git rebase -i HEAD~3   # interactively rewrite the last 3 commits
pick a1b2c3d Add login form
squash e4f5g6h Fix typo in login form
reword h7i8j9k Add validation
  • pick keeps a commit as-is.
  • squash (or s) merges a commit into the one before it, combining their changes into a single commit and prompting you to write a combined message.
  • reword (or r) keeps the commit's changes but lets you edit its message.
  • drop (or d, or simply deleting the line) discards a commit entirely.
  • Reordering the lines themselves replays the commits in the new order — though this can produce its own conflicts if a later commit in the new order depends on something an earlier one (in the old order) provided.

Interactive rebase is where the "never rewrite shared history" rule applies most directly and most often gets violated by accident — squashing three messy work-in-progress commits into one clean commit is genuinely good practice on a branch that's still entirely local, and a real problem the moment any of those three commits has already been pushed and someone else might have based work on them.