Back to Blog
Lesson 36 of the Git & GitHub: Git & GitHub from Zero course
GitAugust 23, 20264 min read

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.

gitversion controlrebasesquashhistory cleanup
A vivid close-up photo of a pumpkin on a dark background, ideal for seasonal themes.

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:

  1. a1b2c3d - Implement user login
  2. e4f5g6h - Fix typo in login
  3. i7j8k9l - Add unit tests for login

To squash these, you tell Git to perform an interactive rebase on the last three commits:

Bash
git rebase -i HEAD~3

Git will open your default text editor with a list of commits that looks like this:

TEXT
pick 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:

TEXT
pick 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

  1. Create a file named feature.txt in your project.
  2. Make three separate small commits by modifying the file and running git commit after each change.
  3. Run git log --oneline to confirm you have three distinct commits.
  4. Use git rebase -i HEAD~3 to squash those three commits into one.
  5. 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 pick to squash. 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.

Similar Posts