The `git squash` command is used to combine multiple commits into a single commit, simplifying your project history and making it easier to manage.
Here’s how you can squash the last three commits:
git rebase -i HEAD~3
In the interactive rebase interface, replace `pick` with `squash` (or `s`) for the commits you want to combine.
What is Git Squash?
The git squash command refers to the process of condensing multiple commits into a single, cohesive commit. It effectively "squashes" various changes into one commit in order to streamline the commit history. This is particularly useful for maintaining clarity and organization within your project’s version control.

Why Use Git Squash?
Using the git squash command has several advantages:
-
Cleaner Commit History: Squashing commits helps eliminate unnecessary commits that clutter the history. This clarity can make it easier to track changes and understand project evolution.
-
Improved Collaboration: When working in teams, a clean commit history makes it easier for others to review changes and understand the development process.
-
Focused Commit Messages: Squashing allows for a single, comprehensive commit message that summarizes the changes made across multiple commits. This can be particularly useful for major features or fixes.

Understanding the Git Workflow
Overview of Git Commit History
A meaningful commit history is crucial for any project. It allows developers to trace back to significant changes, understand the rationale behind those changes, and efficiently manage bugs or feature rollbacks. When commits are left unsquashed and cluttered, it becomes increasingly difficult to maintain a cohesive understanding of a project’s timeline.
How Git Squash Fits into the Workflow
Git squash is most beneficial during the development cycle, particularly before merging branches back into the main or master branch. It is essential to know when to apply the git squash command:
- Feature Branch Cleanup: Before merging a feature branch, squash all intermediate commits to create a single commit that succinctly encapsulates the entire feature.
- Preparing for a Release: Squashing can be used as a final step to ensure only essential changes are recorded prior to a release.

The Basics of Git Squash
What Does the Git Squash Command Do?
The primary function of the git squash command is to modify commit structure. By squashing commits, it allows you to take multiple change sets and condense them into one, preserving the overall diff while simplifying the commit history.
Syntax of Git Squash
The basic syntax for squashing commits using git is as follows:
git rebase -i HEAD~N
In this command, `N` represents the number of commits you want to squash. For instance, if you want to squash the last three commits, you would replace `N` with `3`.

Step-by-Step Guide to Using Git Squash
Preparing to Squash Commits
Before you squash, it is advisable to take a few precautionary steps:
-
Create a Backup: It’s always wise to create a new branch to avoid losing any work. You can do this with:
git checkout -b backup-branch
-
Identify the Commits: Determine which commits you want to squash and ensure they are on the same branch.
Initiating the Squash Process
Start the squash process by using the interactive rebase command:
git rebase -i HEAD~N
After executing this command, your text editor will launch with a list of the last N commits. Each line will start with the word `pick`.
Marking Commits for Squashing
In the text editor, you can keep the first commit as `pick` and change subsequent commits to `squash` (or simply `s`). This signals to Git that these commits should be combined with the first.
For example, your editor might display this:
pick 1a2b3c4 First commit
squash 2b3c4d5 Second commit
squash 3c4d5e6 Third commit
Completing the Squash
After saving and closing the editor, Git will proceed to squash the commits. You'll be prompted to create a new commit message. Make sure to summarize what has been accomplished with the squashed commits. Write a message that captures the essence of the changes made.
If you encounter merge conflicts, Git will halt the process, giving you a chance to resolve any issues. Once resolved, add the files and continue the rebase:
git add <filename>
git rebase --continue
Example Walkthrough
Consider a scenario where you made three commits while developing a feature:
- Commit A: Added feature structure.
- Commit B: Implemented feature logic.
- Commit C: Fixed minor bugs in the feature.
To squash these into a single commit, you’d run:
git rebase -i HEAD~3
You’d change the editor to:
pick 1a2b3c4 Added feature structure
squash 2b3c4d5 Implemented feature logic
squash 3c4d5e6 Fixed minor bugs
After saving and providing a new concise commit message, your commit history will now be clean and representative of the feature added.

Best Practices for Git Squash
When to Use Git Squash
Be deliberate about your squashing. It’s best to use the git squash command when working on local branches that will eventually be merged into a shared branch. Avoid squashing commits that have already been pushed to public repositories to prevent collaboration issues.
Tips for Clean Commit Messages
Commit messages should be focused and reflective of the changes made. Aim for clarity and relevance. A good practice is to use an imperative tone, such as “Add” or “Fix”, and always include the scope of changes for quick reference.

Troubleshooting Common Issues
Dealing with Conflicts
While squashing can yield a cleaner history, conflicts may arise, especially when multiple contributors are working on overlapping changes. Use the following approach:
- Keep an eye on `git status` to understand which files are unstaged or problematic.
- Navigate to affected files, resolve the conflicts, and then continue.
Recovering From Mistakes
If at any point you're unsure of the squash or something goes wrong, you can abort the rebase:
git rebase --abort
This command will return your branch to its previous state before the rebase commenced.

Advanced Git Squash Techniques
Squashing Multiple Commits
To squash more than two commits, simply adjust the `N` value based on how many commits you'd like to combine. The same interactive rebase flow applies.
Combining Squash with Other Commands
You can often align git squash with other commands, such as `git merge` or during `git pull requests`. Before merging changes, squash them to keep the master branch clean.
git merge --squash feature-branch
This command allows you to merge a feature branch into the main branch, while squashing all commits from that feature branch.

Conclusion
In summary, the git squash command serves as a powerful tool for maintaining clarity in your commit history. Through thoughtful squashing of commits, developers can facilitate smoother project collaboration and more efficient code reviews.
Get hands-on experience with the git squash command, practice the steps outlined here, and take the opportunity to enhance not only your own workflow but also improve collaborative efforts with your team.

FAQs About Git Squash
What is the difference between merge and squash?
Merging combines branches without modifying history, while squashing condenses multiple commits into one, thereby simplifying the history.
Can I squash remote commits?
Squashing should be performed on local branches before pushing to remote to prevent overwriting shared history.
Is it possible to un-squash commits?
While squashing is intended to create a clear history, you can manually recover individual commits before squashing or use the reflog for recovery in certain scenarios.

Additional Resources
Engaging with additional resources—books, articles, and community forums—can significantly extend your understanding of Git, including the intricacies of the git squash command. Consider pursuing external materials for ongoing learning, and don’t hesitate to connect with the developer community for further insights and support.