Squash commits

2 snippets in Git

GitGit

Interactive Rebase

Git · Rebasing
syntax
git rebase -i HEAD~<n>
git rebase -i <commit>
example
# Rewrite the last 4 commits:
git rebase -i HEAD~4

# Editor opens with:
# pick a1b2c3d Add login page
# pick d4e5f6g Add signup page
# pick h7i8j9k Fix typo in login
# pick l0m1n2o Update styles
#
# Change 'pick' to: squash, reword, edit, drop, etc.

Note Commands: pick (keep as-is), reword (change message), squash (merge into previous commit), fixup (squash but discard message), edit (pause to modify), drop (delete commit). Save and close the editor to execute.

Squash Multiple Commits into One

Git · Rebasing
syntax
git rebase -i HEAD~<n>
# Change 'pick' to 'squash' or 'fixup' for commits to merge
example
git rebase -i HEAD~3

# In the editor, change to:
# pick a1b2c3d Add user profile feature
# squash d4e5f6g Fix profile image upload
# squash h7i8j9k Add profile validation

# Save — a new editor opens to combine commit messages
output
[detached HEAD x1y2z3w] Add user profile feature
 Date: Mon Mar 30 14:22:01 2026 +0000
 3 files changed, 120 insertions(+), 15 deletions(-)

Note Use 'squash' to combine commit messages. Use 'fixup' (or just 'f') to discard the squashed commit's message entirely. Great for cleaning up WIP commits before merging to main.