As software engineers, we often make multiple small commits during development â and thatâs okay. But when itâs time to merge a feature branch into your main codebase, a messy commit history can make things harder to read, debug, and review.
Thatâs where Git squashing comes in.
In this article, Iâll walk you through what Git squashing is, why it matters, and how to do it cleanly, even if you have untracked changes.
đ§ What Is Git Squashing?
Git squashing is the process of combining multiple commits into a single, cleaner commit. Itâs especially useful when youâve made several âwork-in-progressâ (WIP) commits that arenât meaningful on their own.
For example, instead of these commits:
âď¸ Create login form
âď¸ Fix typo
âď¸ Add missing styles
âď¸ Adjust padding
You can squash them into:
âď¸ Create login form with styles and fixes
This keeps the project history tidy and professional â making life easier for reviewers and your future self.
⨠When Should You Squash?
Before merging a feature branch into main or develop
After exploratory coding that involved many small changes
When preparing pull requests for clean code reviews
đ ď¸ How to Squash Commits in Git
Letâs say you made 3 commits on your feature branch that you want to squash:
git log –oneline
a3f1c7f Fix button spacing
b7d2f1c Add button styles
c4a1e90 Create button component
â
Step 1: Start the interactive rebase
git rebase -i HEAD~3 # The last 3 commits the from HEAD.
This tells Git you want to rebase the last 3 commits.
â
Step 2: Mark commits to squash
Git opens an editor (like Nano or Vim). Youâll see something like:
pick c4a1e90 Create button component
pick b7d2f1c Add button styles
pick a3f1c7f Fix button spacing
Change the second and third lines from pick to squash:
pick c4a1e90 Create button component
squash b7d2f1c Add button styles
squash a3f1c7f Fix button spacing
Save and exit (CTRL + O, Enter, then CTRL + X in Nano) Or a shorter way is CTRL S(save) and CTRL X(exit)
â
Step 3: Edit the final commit message
Git will prompt you to write a new commit message. You can combine or simplify the existing messages:
Create button component with styles and spacing fixes
Save and quit.
â
Result: One Clean Commit!
Now when you run:
git log –oneline
Youâll see:
abc1234 Create button component with styles and spacing fixes
A single, clean commit that reflects the real purpose of your work.
đ¤ What About Pushing to GitHub?
If you havenât pushed this branch before:
Just push as usual:
git push origin your-branch
If you already pushed before squashing:
Youâve rewritten commit history, so Git requires a force push:
git push –force origin your-branch
Or, safer:
git push –force-with-lease origin your-branch
This makes sure you donât overwrite changes others may have pushed.
đĄ Bonus: What If I Have Untracked Changes?
No problem. Just stage and commit them after the squash:
git add .
git commit -m „Handle final UI tweaks“
Then push everything together.
đ§ź Why It Matters
Squashing isnât just about tidiness â itâs about clarity, professionalism, and respect for your teamâs time. Clean commit history makes code reviews easier, debugging faster, and Git blame more helpful. Itâs a small practice with a big impact.
đ Final Thoughts
Git is powerful, and squashing is one of its most elegant tools. If youâre working on features that involve lots of intermediate changes, use squashing to present your work in the clearest way possible.
Make squashing part of your development flow â your teammates (and future you) will thank you.
đ Have thoughts or tips on Git squashing? Letâs connect â drop a comment or share your own Git habits below!