Git Squash Commits: Combine Multiple Commits Into One
Learn how to squash multiple git commits into a single clean commit using interactive rebase. Keep your pull request history clean.
git rebase -i HEAD~3Detailed Explanation
What Does Squashing Commits Mean?
Squashing combines multiple commits into a single commit. This is typically done before merging a feature branch to keep the main branch history clean and readable.
Method 1: Interactive Rebase
# Squash the last 3 commits
git rebase -i HEAD~3
This opens your editor with something like:
pick a1b2c3d Add login form
pick f4e5d6a Fix typo in login
pick 7g8h9i0 Add validation to login
Change pick to squash (or s) for the commits you want to combine:
pick a1b2c3d Add login form
squash f4e5d6a Fix typo in login
squash 7g8h9i0 Add validation to login
Save and close. Git will then open a new editor window where you can write the combined commit message.
Method 2: Soft Reset
# Reset to 3 commits ago, keeping all changes staged
git reset --soft HEAD~3
# Create a single new commit
git commit -m "Add login form with validation"
This is simpler but gives you less control over the resulting message.
Method 3: Squash Merge
git checkout main
git merge --squash feature-branch
git commit -m "Add login feature"
This takes all commits from feature-branch and stages them as a single changeset on main.
When to Squash
- Before merging a PR — collapse "WIP", "fix typo", and "address review comments" into meaningful commits.
- After experimentation — when your commit history reflects trial and error rather than logical steps.
When NOT to Squash
If each commit represents a meaningful, atomic change, keep them separate. A well-structured commit history is more valuable than a single giant commit.
Use Case
A developer's feature branch has 15 commits including several WIP and typo-fix commits. Before merging, they squash them into 2 logical commits that describe the feature clearly.