Clean Up Messy Git History by Squashing Commits
Squashing commits turns 'fix typo', 'oops', 'actually fix it' into one clean, meaningful commit.
What
Git interactive rebase lets you combine multiple small commits into a single, well-described commit before merging to main. This keeps your project history readable and makes code reviews much easier.
Why It Matters
A clean Git history is essential for debugging (git bisect), code reviews, and onboarding new team members. Nobody wants to scroll through 15 'WIP' commits to understand a feature.
Example
# Squash the last 3 commits into one
git rebase -i HEAD~3
# In the editor, change 'pick' to 'squash' (or 's')
# for commits you want to combine:
pick abc1234 Add user auth feature
squash def5678 Fix typo in auth
squash ghi9012 Update auth tests
# Save and write a new combined commit messageCommon Mistake
Running interactive rebase on commits that have already been pushed to a shared branch. This rewrites history and causes conflicts for everyone else on the team.
Quick Fix
Only squash commits on your local feature branch BEFORE pushing or merging. If already pushed, use `git merge --squash` on the target branch instead.
Key Takeaways
- 1Too many small commits? Squash them!
- 2git rebase -i HEAD~N
- 3Change 'pick' to 'squash' for commits to merge
- 4Never rebase shared/pushed commits
- 5Use git merge --squash as a safe alternative
Was this tip helpful?
Help us improve the DevOpsPath daily collection