Git Squash: Squashing Commits for a Cleaner History


Git is a powerful version control system that allows developers to track changes, collaborate with others, and manage their codebase effectively. One of Git’s essential features is the ability to create a clean and organized commit history. Git’s “squash” feature allows you to combine multiple commits into a single, more coherent commit. In this blog post, we’ll explore what squashing commits means, why it’s useful, and how to do it with practical examples.

Understanding Git Squash

The “git squash” enables you to merge multiple commits into a single, cleaner commit. This means that instead of having a long and cluttered commit history, you can group related changes together, making it easier to navigate through your project’s history.

Why Squash Commits?

Squashing commits can be particularly beneficial for several reasons:

  1. Clean History: A concise commit history makes it easier for team members and future maintainers to understand the project’s development.
  2. Relevant Commit Messages: You can provide meaningful, high-level commit messages that describe the purpose and significance of the changes.
  3. Merge Commits: Squashed commits simplify the process of merging feature branches into the main branch, as you only have one commit to merge.
  4. Bug Tracking: When troubleshooting, it’s easier to identify which commit introduced a bug or caused an issue.
  5. Documentation and Changelogs: A well-structured commit history can be used to generate documentation and changelogs automatically.

Related Posts:

  1. Git Pull vs Fetch
  2. Git Merge vs. Rebase

Now, let’s dive into how to squash commits in Git.

How to Squash Commits

Squashing commits is relatively straightforward, but it requires some knowledge of Git’s interactive rebase feature. Here’s a step-by-step guide:

Suppose you have a Git repository with several small, incremental commits related to a feature or bug fix, and you want to squash them into one commit for a cleaner commit history.

Let’s say you have the following commit history:

* abc123 - Fix issue #123
* def456 - Address code review feedback
* ghi789 - Update documentation
* jkl012 - Add new feature
* mno345 - Initial commit

Now, you want to squash the last four commits (def456, ghi789, jkl012) into a single commit. Here’s how you can do it:

Step 1: Start an interactive rebase by running the following command:

git rebase -i HEAD~4

This command tells Git to perform an interactive rebase on the last 4 commits.

Step 2: An editor will open with a list of the selected commits and their actions:

pick def456 - Address code review feedback
pick ghi789 - Update documentation
pick jkl012 - Add new feature
pick mno345 - Initial commit

Step 3: Change “pick” to “squash” (or simply “s”) for the commits you want to squash:

pick def456 - Address code review feedback
squash ghi789 - Update documentation
squash jkl012 - Add new feature
pick mno345 - Initial commit

Step 4: Save and exit the text editor.

Step 5: Git will prompt you to edit the commit message for the combined commit. You can edit the message as needed, which might look something like this:

Updated documentation and added new feature

Step 6: Save and exit the commit message editor.

Step 7: Git will complete the rebase, and you will have a single, squashed commit with the updated message. Your commit history will now look like this:

* abc123 - Fix issue #123
* 123456 - Updated documentation and added new feature
* mno345 - Initial commit

Step 8: Since you’ve rewritten history, you may need to force push the changes to the remote repository:

git push -f

That’s how you use “git squash” to combine multiple commits into a single, cleaner commit in your Git history.

Note: Keep in mind that rewriting history, including squashing commits, should be used with caution, especially when collaborating with others on a shared Git repository. It can make it challenging for others to synchronize their work with your changes. Therefore, it’s essential to communicate and coordinate with your team when using squash and force-pushing.

Summary

In summary, “git squash” is a technique for combining multiple commits into a single, more coherent commit using the interactive rebase feature in Git. It’s a helpful way to maintain a cleaner and more organized commit history.

References

  1. Squashing the Last N Commits into One Commit
  2. Squash and merge
  3. Git Top Daily Life Usage Commands

Similar Posts

About the Author

Atul Rai
I love sharing my experiments and ideas with everyone by writing articles on the latest technological trends. Read all published posts by Atul Rai.