A squash commit means combining multiple commits into a single commit before merging into a main branch.
🔹 Why Squash Commits?
During development, you may have many small commits like:
- fix typo
- fix bug
- update UI
- refactor code
Squashing them: ✔ Keeps Git history clean ✔ Makes commits meaningful ✔ Easier code review & rollback
🧠 What Squashing Does
Before squash:
A -- B -- C -- D
After squash:
A -- D'
(B, C, D are merged into one commit)
✅ How to Squash Commits (Interactive Rebase)
git rebase -i HEAD~3
Then change:
pick 123abc commit1
pick 456def commit2
pick 789ghi commit3
to:
pick 123abc commit1
squash 456def commit2
squash 789ghi commit3
Save → edit commit message → done.
🔥 Squash While Merging (GitHub / GitLab)
Most platforms provide:
- “Squash and Merge” option
✔ Combines all PR commits into one
⚠️ Important Notes
- Avoid squashing shared public branches
- Best for feature branches
- Rewrites Git history
🎯 Short Interview Answer
A squash commit combines multiple commits into one to keep the Git history clean and readable. It is commonly used before merging feature branches into the main branch.
⭐ One-line summary
Squash commits clean up commit history by merging many commits into one.