Skip to main content

Command Palette

Search for a command to run...

Streamlining Git History with Squash Commits

Published
2 min readView as Markdown

Introduction

Git is a powerful version control tool, but a messy commit history can make projects harder to manage. This is where Squash Commits come into play. They allow you to combine multiple commits into one, making your history cleaner and easier to read.

Example: "Have you ever seen a long list of commits like 'Fix typo,' 'Update README,' or 'Final fix'? That’s when you need squash commits!"


1. What is a Squash Commit?

Definition

A squash commit is the process of merging multiple commits into a single commit to tidy up the commit history.

Why use it?

  • Simplifies commit history.

  • Makes it easier to track major changes.

  • Helps create cleaner and more professional pull requests (PRs).


2. How to Use Git Squash Commit

2.1. Using git rebase

Basic command:

git rebase -i HEAD~n

HEAD~n: Combine the last n commits.

Steps:

  1. Run the command, and a list of commits will appear.

  2. Change pick to squash (or s) for the commits you want to merge.

  3. Save and edit the commit message if necessary.

Example:

Suppose you have the following commits:

pick abc123 Add feature A
pick def456 Fix bug in feature A
pick ghi789 Update documentation

You want to squash them into a single commit:

pick abc123 Add feature A
squash def456 Fix bug in feature A
squash ghi789 Update documentation

Then, edit the commit message to:

Add feature A with bug fixes and updated documentation

2.2. Important Notes

  • Squashing commits rewrites commit history → Avoid using it on shared branches.

  • Use it only on branches that haven’t been merged yet.


3. When Should You Use Squash Commits?

  • Before submitting a Pull Request to make commit history clean.

  • After making multiple small commits during development.

  • In team projects, to simplify code review.


4. Benefits of Squash Commits

  • Reduces "noise" in the commit history.

  • Enhances professionalism in team workflows.

  • Makes it easier for others to understand the purpose of changes.


5. Conclusion

Squash commits are a simple yet powerful way to maintain a clean and professional commit history. Use them to combine meaningful changes, avoid clutter, and improve collaboration in team projects.

Remember, a clean commit history reflects well on you as a developer!


Call to Action

"Start using squash commits in your next project and experience the difference in your Git history today!"

More from this blog

Khang Nguyen

119 posts