Cheat Sheets · Git Cheat Sheet · Git Squash Last N Commits — Combine Multiple Commits
Git Squash Last N Commits — Combine Multiple Commits
Soft Resetgit reset --soft HEAD~N && git commit -m "feat: clean message"
Interactive Rebasegit rebase -i HEAD~N (mark top as pick, others as squash)
Two approaches to squashing
| Approach | Commands | Best For |
|---|---|---|
Soft Reset | git reset --soft HEAD~N && git commit -m "feat: clean message" | Quickly squashing the top N commits into one |
Interactive Rebase | git rebase -i HEAD~N (mark top as pick, others as squash) | Selective squashing, reordering, or editing messages |
Squashing combines messy "wip", "typo", and "fix" commits into a single clean commit before merging into main or opening a pull request.
Related Git recipes
FAQ
What is the difference between soft reset and rebase -i for squashing?
Soft reset collapses everything in one step without prompts. Interactive rebase gives you full control to reorder, squash, or drop individual commits.
Can I squash commits that were already pushed?
Only on private feature branches. If pushed, you must push with --force-with-lease. Never squash on shared branches.
How to squash all commits on a feature branch against main?
Run git reset $(git merge-base main HEAD) and commit.
Stop memorizing — build it interactively
Build squash workflows in the Git BuilderFull reference: Git Cheat Sheet