0
0
GitConceptBeginner · 3 min read

What is Squash Merge in Git: Explanation and Example

A 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.

bash
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
Output
Switched to branch 'main' Already up to date. Switched to branch 'feature-branch' # Interactive rebase editor opens # After saving, commits are combined Switched to branch 'main' Updating abc1234..def5678 Fast-forward file1.txt | 10 ++++++++++ 1 file changed, 10 insertions(+) Enumerating objects: 5, done. Counting objects: 100% (5/5), done. Compressing objects: 100% (3/3), done. Writing objects: 100% (3/3), 500 bytes | 500.00 KiB/s, done. Total 3 (delta 0), reused 0 (delta 0) To github.com:user/repo.git abc1234..def5678 main -> 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.

Key Takeaways

Squash merge bundles all feature branch commits into one before merging.
It simplifies the main branch history for easier understanding and review.
Use it when detailed commit history is not required.
Helps keep project history clean and easier to manage.
Avoid squash merge if you need to preserve every individual commit.