Sign In Create Account
Squash & Merge

Squash & Merge

Maintain clean Git history with strategic squashing and merging techniques.

When to Squash

Use squash and merge for pull requests to maintain clean Git history on main branches.

✅ Good Candidates for Squashing

  • • Feature branches with multiple WIP commits
  • • Fix-up commits addressing code review feedback
  • • Small features that should be atomic in history
  • • Experimental commits that were cleaned up
  • • Commits with typo fixes during development

❌ Don't Squash When

  • • Each commit represents meaningful progress
  • • Commits solve different problems
  • • History provides valuable context
  • • Multiple developers worked on the branch
  • • Commits already follow conventions perfectly

Benefits of Squash and Merge

  • Clean History: Each feature is one logical commit
  • Easy Rollback: Revert entire features with single command
  • Better Bisecting: Git bisect works more effectively
  • Readable Log: Git log shows meaningful progression

Manual Squashing

Use interactive rebase to manually squash commits before creating pull requests.

Interactive Rebase Squashing

# Interactive rebase to squash commits
git checkout feature/user-dashboard
git rebase -i develop

# In the interactive editor, change 'pick' to 'squash' for commits to combine:
pick abc1234 feat: initial dashboard structure
squash def5678 wip: add user stats
squash ghi9012 fix: typo in component name
squash jkl3456 refactor: improve data fetching

# Save and exit. Git will prompt for the final commit message:
feat(dashboard): implement user dashboard with statistics

# Result: One clean commit with all changes
# Push the squashed branch
git push --force-with-lease origin feature/user-dashboard

Interactive Rebase Commands

  • pick: Keep commit as-is
  • reword: Keep commit but edit message
  • edit: Keep commit but allow editing
  • squash: Combine with previous commit
  • fixup: Like squash but discard message
  • drop: Remove commit entirely

Platform Squash and Merge

Most Git hosting platforms (GitHub, GitLab, Bitbucket) offer squash and merge options.

GitHub/GitLab Squash Merge Result

# When merging PR/MR, use platform's "Squash and merge" option
# This combines all commits into one with a clean message:

feat(auth): implement user authentication system (#42)

* Initial login form implementation
* Add password validation
* Integrate with OAuth2 provider  
* Add comprehensive tests
* Address code review feedback

GitHub Squash and Merge

Use "Squash and merge" button. GitHub will combine all commits and let you edit the final commit message before merging.

GitLab Squash Option

Enable "Squash commits when merge request is accepted" in the merge request settings.

Squashing Strategies

Feature Branch Strategy

Squash all commits in a feature branch into a single commit when merging to develop.

Feature Branch Before Squashing

# Feature branch commit history
* f7f3f6d feat: add login form
* 310154e wip: form validation  
* a5f4a0d fix: typo in validation message
* c5f4a0d fix: address code review feedback
* d6g7h8i test: add authentication tests

# After squash merge to develop
* 1a2b3c4 feat(auth): implement user login with validation (#45)

Logical Grouping Strategy

Group related commits together while keeping logically separate changes distinct.

Logical Grouping Example

# Original commits
* abc1234 feat: add user model
* def5678 feat: add user validation  
* ghi9012 feat: add user controller
* jkl3456 test: add user model tests
* mno7890 test: add user controller tests
* pqr1234 docs: update API documentation

# After logical squashing
* xyz7890 feat(user): implement user management system
* abc4567 test(user): add comprehensive user tests  
* def8901 docs(api): update user endpoint documentation

Best Practices

✅ Squashing Best Practices

  • • Write meaningful commit messages for squashed commits
  • • Include pull request number in squash commit message
  • • Use conventional commit format for squashed commits
  • • Test thoroughly before squashing and merging
  • • Delete feature branch after successful merge

⚠️ Squashing Considerations

  • Lost History: Individual commit context is lost
  • Harder Debugging: Can't bisect through feature development
  • Attribution: Co-authors may need explicit credit
  • Review Difficulty: Large squashed commits are hard to review

Co-Author Attribution

When squashing commits from multiple authors, include co-author credits:

feat(auth): implement OAuth2 authentication (#42)

Co-authored-by: Jane Smith <[email protected]>
Co-authored-by: Bob Johnson <[email protected]>