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 d4e5f6a Add signup page
# pick b7c8d9e Fix typo in login
# pick c0d1e2f 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 d4e5f6a Fix profile image upload
# squash b7c8d9e Add profile validation

# Save - a new editor opens to combine commit messages
Output
[detached HEAD e1f2a3b] 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.

Frequently asked questions

How do you squash commits?
Git covers this with 2 copy-ready snippets on this page. The "Interactive Rebase" snippet in Git uses `git rebase -i HEAD~<n>`.
Which command does the Git example use?
The "Interactive Rebase" snippet uses `git rebase -i HEAD~<n>`, from the Rebasing section of the Git cheat sheet.
What other Git snippets are shown for "squash commits"?
Besides "Interactive Rebase", this page also shows "Squash Multiple Commits into One".
Is there anything to watch out for?
Yes. For "Interactive Rebase": 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.