bashintermediate

Squash Multiple Git Commits

Interactive rebase to squash multiple commits into one clean commit before merging a feature branch.

bash
# Squash last 3 commits interactively
git rebase -i HEAD~3

# In the editor that opens, change 'pick' to 'squash' (or 's'):
# pick abc1234 First commit
# squash def5678 Second commit
# squash ghi9012 Third commit
# Save and close — edit the combined commit message

# Squash all commits on a feature branch into one
git checkout feature/my-feature
git reset --soft main
git commit -m "feat: complete feature implementation"

# Alternative: squash merge (from main)
git checkout main
git merge --squash feature/my-feature
git commit -m "feat: add my feature"

# Fixup (squash without editing message) — keeps first commit message
# In interactive rebase, use 'fixup' or 'f' instead of 'squash'
# pick abc1234 Main commit message
# fixup def5678 WIP
# fixup ghi9012 Fix typo

Use Cases

  • Cleaning up messy commit history
  • Creating a single commit for a pull request
  • Combining WIP commits before merging

Tags

Related Snippets

Similar patterns you can reuse in the same workflow.