Published on

Git Squash: Clean Up Your Commits History

Authors
  • avatar
    Name
    Hieu Cao
    Twitter

Introduction

Git is an indispensable tool for developers, enabling efficient collaboration and version control. Among its myriad of features, Git squash stands out as a powerful way to condense multiple commits into one, creating a cleaner and more understandable commit history.

This blog explains what Git squash is, why you should use it, and how to effectively squash commits using practical examples.

What Is Git Squash?

Git squash is the process of combining multiple commits into a single commit. This technique is particularly useful when you have a series of small, incremental commits that you want to present as a single, cohesive change. Squashing commits helps:

  • Improve commit history clarity: Makes it easier to understand the changes made in a project.
  • Prepare for pull requests: Ensures your contributions are concise and meaningful.
  • Reduce noise in collaborative projects: Helps teams maintain a clean project history.

When to Use Git Squash

Here are some scenarios where Git squash is beneficial:

  1. During feature development: You made several "work-in-progress" commits while developing a feature.
  2. Before merging branches: To clean up the commit history before merging into the main branch.
  3. Fixing mistakes: You want to combine commits that include redundant changes, typo fixes, or minor adjustments.

How to Squash Commits in Git

To squash commits, you typically use the interactive rebase feature. Follow these steps:

Step 1: Check Your Commit History

Run the following command to view your commit history:

git log --oneline

This will display a compact list of your recent commits.

Step 2: Start an Interactive Rebase

Decide how many commits you want to squash, then start an interactive rebase:

git rebase -i HEAD~<n>

Replace <n> with the number of commits to include in the rebase. For example, if you want to squash the last 3 commits, use:

git rebase -i HEAD~3

Step 3: Mark Commits for Squashing

An editor will open, showing a list of commits:

pick abc123 Initial commit
pick def456 Add feature implementation
pick ghi789 Fix minor bug
  • pick means "use this commit as is."
  • Change pick to squash (or s) for the commits you want to squash into the previous one:
pick abc123 Initial commit
squash def456 Add feature implementation
squash ghi789 Fix minor bug

Step 4: Combine Commit Messages

Git will prompt you to edit the commit message for the squashed commit. You can keep the original messages, combine them, or write a new one. For example:

# This is a combination of 3 commits.
# The first commit's message is:
Initial commit

# The following commit messages will also be included:
Add feature implementation
Fix minor bug

Edit it as needed, then save and close the editor.

Step 5: Complete the Rebase

Git will apply the squashed commits. If there are conflicts, resolve them and continue the rebase:

git rebase --continue

Once the process is complete, your commits will be squashed into one.

Example

Suppose you have these commits:

commit abc123 Initial commit
commit def456 Add feature implementation
commit ghi789 Fix minor bug

After squashing the last two commits, your history will look like this:

commit jkl012 Initial commit with feature and bug fix

Tips for Using Git Squash

  1. Squash before sharing: Only squash commits in your local branch before pushing to a shared branch.
  2. Backup your branch: Create a backup branch before rebasing to avoid losing changes.

Conclusion

Git squash is a valuable tool for maintaining a clean and professional commit history. By combining related commits, you can make your changes more digestible for collaborators and reviewers. Start using Git squash today to enhance your Git workflow!

Happy coding!