Squashing Commits: Clean Up Your Git History Like a Pro
Learn how to use git rebase to squash commits into a single, clean history. Master the workflow to keep your repository professional and easy to navigate.

Previously in this course, we covered best practices for commit messages to ensure our project history remains descriptive and helpful. In this lesson, we take that a step further by learning how to consolidate those small, incremental commits into a single, coherent snapshot.
Why Squash Commits?
When you are deep in the middle of a feature, you likely make several small commits. You might have "fixed typo," "added logic," and "forgot a semicolon." While these are great for saving your progress, they clutter the main project history and make it harder for reviewers to follow the logical progression of your work.
Squashing, or combining these small commits into one, is a core part of history cleanup. It allows you to present a polished, logical set of changes to your team rather than a messy "work-in-progress" trail.
Mastering Interactive Rebase for Squashing
The primary tool for this task is the git rebase -i command (the 'i' stands for interactive). This opens an editor that lets you choose exactly how to manipulate your recent commits.
Worked Example: The Cleanup Workflow
Imagine you have three commits on your current feature branch that you want to turn into one:
a1b2c3d- Implement user logine4f5g6h- Fix typo in logini7j8k9l- Add unit tests for login
To squash these, you tell Git to perform an interactive rebase on the last three commits:
Bashgit rebase -i HEAD~3
Git will open your default text editor with a list of commits that looks like this:
TEXTpick a1b2c3d Implement user login pick e4f5g6h Fix typo in login pick i7j8k9l Add unit tests for login
To squash the second and third commits into the first, change the word pick to squash (or just s) for those two lines:
TEXTpick a1b2c3d Implement user login squash e4f5g6h Fix typo in login squash i7j8k9l Add unit tests for login
Save and close the file. Git will then combine these into one commit and prompt you to write a new, final commit message that summarizes the entire set of changes.
Hands-on Exercise
- Create a file named
feature.txtin your project. - Make three separate small commits by modifying the file and running
git commitafter each change. - Run
git log --onelineto confirm you have three distinct commits. - Use
git rebase -i HEAD~3to squash those three commits into one. - Verify your history again with
git log --oneline. You should now see only one commit representing the total work.
Common Pitfalls
- Rebasing Shared History: Never rebase commits that have already been pushed to a remote repository where others are working. Rebasing rewrites history (it creates new commit hashes), which will cause significant pain for your teammates if they have already pulled your original commits.
- The "Oops" Moment: If you mess up an interactive rebase, don't panic. You can abort the process entirely by running
git rebase --abort. This restores your branch to its state before you started the rebase. - Forgetting to Save: Ensure you actually save the file in your text editor after changing
picktosquash. If you close the editor without saving, Git will think you changed your mind and cancel the rebase.
FAQ
Q: Is squashing the same as merging? A: No. Merging joins two branches together. Squashing is a way of modifying your own local commit history before you merge that work into a shared branch. For a deeper dive into these concepts, check out Git Rebase vs. Merge: A Guide to Clean Commit History.
Q: When should I NOT squash? A: If a commit represents a distinct, logical unit of work that provides value on its own, keep it. Squashing is for cleaning up "noise" and "typo-fix" commits, not for erasing the history of a complex, multi-part feature.
Recap
Interactive rebase is a powerful tool for maintaining a clean and professional commit history. By using git rebase -i, you can collapse multiple, incremental commits into a single, meaningful snapshot. Remember to use this primarily on your local, unpushed work to avoid disrupting your team's workflow. If you want to refine your skills further, Git Interactive Rebase: How to Clean Up Your Local History provides additional techniques for managing your commits.
Up next: We will learn about Tagging Releases to mark specific, stable versions of your project.
Work with me

Laravel REST API Development
Clean, secure, well-documented Laravel REST APIs โ the backend engine for your app, mobile client, or SaaS. Built by an API specialist.

React & Next.js Dashboard / Admin UI Development
A clean, data-rich dashboard UI in React or Next.js โ charts, tables, and real-time data that your users will actually enjoy using.

