Git Squash Commits Guide
In this tutorial, you'll learn about Git Squash Commits Guide. We cover key concepts, practical examples, and best practices.
A messy commit history with "fix typo", "oops", "wip" commits makes code review difficult and pollutes the git log. Squashing combines multiple commits into one, creating a clean, meaningful history before pushing or merging.
The Problem
git log --oneline
Shows:
a1b2c3 Fix typo
d4e5f6 Add tests
g7h8i9 WIP
j0k1l2 Implement login feature
m3n4o5 Initial commit
Wrong Approach
# WRONG — resetting and losing all commit messages
git reset --soft HEAD~4
git commit -m "lost all individual commit messages"
Right Approach
git rebase -i HEAD~4
In the editor, change pick to squash:
pick m3n4o5 Initial commit
pick j0k1l2 Implement login feature
squash g7h8i9 WIP
squash d4e5f6 Add tests
squash a1b2c3 Fix typo
Save and exit. Then write the final commit message.
Expected output:
[detached HEAD z9y8x7] Implement login feature with tests
5 files changed, 120 insertions(+)
Successfully rebased and updated refs/heads/main.
Step-by-Step Guide
Step 1: Identify commits to squash
git log --oneline
Step 2: Start interactive rebase
git rebase -i HEAD~4
Step 3: Mark commits as squash
In the editor, change pick to squash (or s) for all commits you want to combine into the commit above them.
Step 4: Write the squashed commit message
Git opens an editor with all commit messages combined. Write a clear, descriptive message and save.
Step 5: Verify the result
git log --oneline -5
Step 6: Force push if the branch was shared
git push --force-with-lease origin feature-branch
Prevention Tips
- Squash commits before merging feature branches into main
- Use
git commit --fixupandgit rebase -i --autosquashfor automatic squashing - Keep commits atomic — one logical change per commit
- Write meaningful commit messages from the start
- Squash before creating a pull request, not after
Common Mistakes with squash commits
- Using
headandtailinstead of pattern matching, causing runtime errors on empty lists - Forgetting that lazy evaluation defers computation until the value is forced, causing space leaks with unevaluated thunks
- Using
returnto exit a function early instead of wrapping a pure value in the monad
These mistakes appear frequently in real-world GIT code. DodaTech's contributors have identified these patterns through analysis of open-source projects and production systems.
Practice Exercise
Write a pure function that safely divides two integers using Maybe, then test it with edge cases like division by zero and negative numbers.
This exercise reinforces the concepts covered in this guide. Try implementing it before checking online solutions.
FAQ
Built by the developers of DodaTech
Doda Browser, DodaZIP & Durga Antivirus Pro