AlgoMaster Logo

squash merge

Last Updated: January 3, 2026

6 min read

Squash merging is a powerful technique in Git that allows developers to integrate changes from one branch into another while condensing multiple commits into a single commit. This method is particularly useful for keeping a project’s history clean and manageable.

Understanding how to effectively use squash merges can greatly enhance your workflow, especially when working in teams or on feature branches.

In this chapter, we’ll explore the concept of squash merging in depth, discuss its advantages, and provide practical examples to illustrate its effectiveness.

What is Squash Merge?

At its core, a squash merge takes all the changes from a feature branch and combines them into a single commit before merging into the target branch.

This means that instead of preserving the entire commit history of the feature branch, you end up with one commit that represents all those changes collectively.

The squash merge can be visualized as follows:

In this example, commits D, E, and F from the feature branch have been squashed into a single commit G on the main branch.

Understanding how this works under the hood is critical. When you squash merge, Git effectively creates a new commit that contains the changes from the feature branch while discarding the individual commits. This helps in keeping the commit history cleaner and easier to navigate.

How to Perform a Squash Merge

Squash merging can be performed using the git merge command with the --squash option. Here’s a step-by-step guide to executing a squash merge.

Step 1: Prepare Your Branches

Before you can squash merge, make sure your branches are up to date. For example, if you are working on a feature branch, ensure you have the latest changes from the main branch:

Step 2: Execute the Squash Merge

Now that you have updated your branches, you can execute the squash merge:

This command applies all changes from the feature branch to the main branch but does not create a commit yet.

Step 3: Commit the Squashed Changes

After executing the squash merge, you’ll need to commit the changes:

After this command, the feature branch's changes are now reflected in the main branch as a single commit.

Advantages of Squash Merging

Squash merging offers several benefits, particularly when it comes to maintaining a clean commit history:

  • Simplified History: Instead of cluttering your history with numerous small commits, you can encapsulate all changes in a single, descriptive commit. This is particularly useful for feature branches that may contain multiple "work-in-progress" commits.
  • Easier Reverts: If you ever need to revert the feature, having a single commit makes it straightforward. You can simply revert that one commit instead of dealing with multiple commits.
  • Clearer Context: By writing a single, comprehensive commit message that explains the feature or fix, you provide better context for future developers looking at the commit history.
  • Better for Release Management: In environments where releases are made from specific commits, squashing changes into a single commit ensures that only relevant changes are included in the release.

Best Practices for Squash Merging

While squash merging is incredibly useful, there are best practices to follow to maximize its effectiveness:

Use Meaningful Commit Messages

When squashing, the commit message should clearly describe the changes being made. A well-written message provides context for others and serves as documentation for why changes were made.

A good practice is to summarize the purpose of the entire feature in the message while possibly referencing issues or tickets associated with the changes.

Squash Before Merging

If you’re working in a collaborative environment, consider squashing your feature branch before merging it into the main branch. This keeps the main branch clean and ensures that only relevant changes are added.

Review Commit History Regularly

Regularly reviewing your commit history can help identify when a squash merge might be appropriate. Consider squashing commits that are closely related or that do not contribute significantly to the project’s history.

Handling Squash Merge Conflicts

Even with squash merges, conflicts can arise. Understanding how to resolve these conflicts is crucial to maintaining a smooth workflow.

Conflict Scenarios

Conflicts can occur during the squash merge step when there are overlapping changes between the branches. When you attempt to perform the squash merge, Git will alert you to these conflicts.

When a conflict arises, Git will mark the files that need to be resolved. You can use:

to see which files are in conflict.

Resolving Conflicts

To resolve conflicts, open the affected files and look for the conflict markers (e.g., <<<<<<<, =======, >>>>>>>). Edit the files to resolve the conflicts manually, then proceed with the merge process.

After resolving the conflicts, you can add the changes:

Then, continue with the commit:

Real-World Use Cases for Squash Merge

Squash merging shines in various real-world scenarios:

Feature Development

When working on a feature that involves multiple commits (e.g., iterative changes, bug fixes), squashing those commits into one before merging into the main branch keeps the history clean and focused.

Hotfixes

For urgent fixes that may go through several trials before arriving at a solution, squashing commits ensures only the final, successful fix is recorded in the history.

Pull Requests

When submitting a pull request to a shared repository, using squash merges can help maintain a tidy history for the project, making it easier for maintainers to review and understand the changes.

Conclusion

In summary, squash merging is a powerful technique that enhances your ability to maintain a clean and organized commit history in Git. It simplifies the process of integrating changes, especially when those changes are the result of multiple iterative commits.

By understanding the nuances of squash merges, including how to handle conflicts, write meaningful commit messages, and apply best practices, you can significantly improve your Git workflow. Whether you're working alone or as part of a team, mastering squash merges will make your development process smoother and more efficient.