What is Squash Merge in Git: Explanation and Example
squash merge in Git combines all changes from a feature branch into a single commit before merging it into the main branch. This keeps the main branch history clean by avoiding multiple small commits from the feature branch.How It Works
Imagine you are writing a story with many small edits on a separate paper. Instead of adding each small edit one by one to the final book, you combine all your edits into one neat paragraph and then paste it into the book. This is what squash merge does in Git.
When you work on a feature branch, you might make many small commits like fixing typos, adjusting code, or adding small parts. Squash merge takes all those commits and bundles them into one single commit. Then, it adds that single commit to the main branch.
This helps keep the main branch's history simple and easy to read, showing only the final result of your feature work instead of every small step.
Example
This example shows how to squash merge a feature branch into the main branch using Git commands.
git checkout main git pull origin main git checkout feature-branch git rebase -i main # In the interactive editor, change 'pick' to 'squash' for commits to combine # Save and close the editor git checkout main git merge feature-branch # Push the updated main branch git push origin main
When to Use
Use squash merge when you want to keep your main branch history clean and easy to understand. It is helpful when your feature branch has many small or fix-up commits that do not need to be tracked individually.
For example, in team projects, squash merging helps reviewers see the feature as one complete change instead of many small steps. It also makes reverting changes easier because you only need to undo one commit.
However, if you want to keep detailed history of every change for debugging or auditing, avoid squash merge and use a regular merge instead.
Key Points
- Squash merge combines multiple commits into one before merging.
- It keeps the main branch history clean and simple.
- Useful for feature branches with many small commits.
- Helps reviewers see the feature as a single change.
- Not ideal if detailed commit history is needed.