What is squashing commits in Git?
Learn what squashing commits means in Git, how to squash with interactive rebase, squash vs fixup, and why it's risky to rewrite already-shared history.
Expected Interview Answer
Squashing is combining a series of consecutive commits into a single commit, replacing many small, messy checkpoints (like fix typo, wip, actually fix it) with one clean commit that represents the complete change.
It is most commonly done with an interactive rebase (git rebase -i), where you mark commits as squash or fixup so their changes and messages fold into the commit above them, or by pull-request platforms offering a 'squash and merge' option automatically. Squashing rewrites history, so it should generally be limited to commits that have not been pushed and shared yet, or to a feature branch about to be merged, since rewriting shared history can create painful divergence for anyone who already pulled the old commits.
- Turns messy work-in-progress commits into one clean commit
- Makes git log and code review far easier to follow
- Keeps the main branch's history meaningful and readable
- Simplifies reverting a whole feature as a single unit
- Commonly automated via a repository's squash-and-merge option
AI Mentor Explanation
Squashing commits is like condensing a messy, stop-start period of play — a wide, a no-ball, a dropped catch, a recall — into a single clean entry on the scorecard that reads how the over ended. Nobody scrolling the scorecard later needs every fumbled retry; they need the final, tidy result.
Step-by-Step Explanation
Step 1
Identify commits to combine
Pick a consecutive range of commits on your feature branch, usually the messy WIP/fix-up history since branching from main.
Step 2
Start an interactive rebase
Run git rebase -i HEAD~N (or git rebase -i main) to open a list of the commits in an editor.
Step 3
Mark commits as squash or fixup
Change 'pick' to 'squash' (keep the message for editing) or 'fixup' (discard the message) on all but the first commit in the group.
Step 4
Write the final message
Git combines the marked commits' changes and lets you write one clean commit message summarizing the whole change.
Step 5
Push carefully
Since history was rewritten, use git push --force-with-lease on your own feature branch, never on shared branches others have already pulled.
What Interviewer Expects
- Explains squashing combines multiple commits into one
- Knows the interactive rebase workflow (pick/squash/fixup)
- Understands squashing rewrites history, so it's risky on shared branches
- Mentions --force-with-lease over a plain --force after rewriting history
- Knows platform-level 'squash and merge' as an alternative to manual rebase
Common Mistakes
- Squashing commits that have already been pushed and pulled by teammates
- Confusing squash (keeps message for editing) with fixup (discards it)
- Force-pushing without --force-with-lease, risking overwriting others' work
- Losing meaningful intermediate history that should have stayed separate
Best Answer (HR Friendly)
“Squashing means combining several small, messy commits — like quick fixes and typo corrections — into one clean commit that represents the whole finished change. It makes project history much easier to read, but it should only be done to your own unshared work, since rewriting history that others already have can cause confusion.”
Code Example
git rebase -i HEAD~4
# In the editor, change all but the first line's 'pick' to 'squash':
# pick a1b2c3d Add login form
# squash e4f5g6h fix typo
# squash i7j8k9l wip
# squash m1n2o3p actually fix validation
# Save, write one combined commit message, then:
git push --force-with-lease origin feature-branchFollow-up Questions
- What is the difference between squash and fixup in an interactive rebase?
- Why is git push --force-with-lease safer than a plain --force?
- When is it unsafe to squash commits that have already been pushed?
- How does a platform's 'squash and merge' button relate to git rebase -i?
- How would you undo a squash if you made a mistake mid-rebase?
MCQ Practice
1. What does squashing commits do?
Squashing merges the changes and history of several commits into one, replacing many small commits with a single clean one.
2. In an interactive rebase, what is the difference between 'squash' and 'fixup'?
Both fold a commit into the one above, but squash lets you edit the combined message while fixup silently discards the folded commit's message.
3. Why is squashing risky on a branch others have already pulled?
Because squashing rewrites commit hashes, anyone who already pulled the old commits will have a diverged history, complicating their next pull or merge.
Flash Cards
What does squashing commits mean? — Combining several consecutive commits into a single commit with one combined message.
How do you squash commits interactively? — git rebase -i HEAD~N, then mark commits as 'squash' or 'fixup' instead of 'pick'.
Squash vs fixup? — Squash keeps the commit message for editing; fixup discards the folded commit's message entirely.
Why is squashing risky on shared branches? — It rewrites commit history, which diverges from anyone who already pulled the original commits.