How to squash commits in Git

· Category: Git

Short answer

Squashing combines multiple commits into one, making history cleaner and easier to follow.

Steps

  1. Interactive rebase:
git rebase -i HEAD~4
  1. Change pick to squash or s for commits you want to combine.
  2. Save and edit the commit message.
  3. Alternatively, squash on merge via GitHub/GitLab.

Tips

  • Squash fixup commits and work-in-progress checkpoints.
  • Keep logically separate changes unsquashed for better bisecting.
  • Always squash before merging feature branches if the team prefers linear history.

Common issues

  • Squashing shared commits can cause issues for collaborators.
  • Resolving conflicts during interactive rebase requires careful attention.