Zum Inhalt springen

🔄 Git Squashing: How to Clean Up Your Commit History Like a Pro

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!

Schreibe einen Kommentar

Deine E-Mail-Adresse wird nicht verĂśffentlicht. Erforderliche Felder sind mit * markiert