Advanced

How to squash commits in Git

Combine multiple commits into a single commit.

Squash last N commits interactively
git rebase -i HEAD~3

Opens an editor for the last 3 commits. Change "pick" to "squash" (or "s") on all but the first.

When to use: Cleaning up messy commit history before a pull request.

In the editor: keep the first as "pick", change the rest to "squash". Then write the final commit message.

Squash using merge --squash
git switch main
git merge --squash feature-branch
git commit -m "Feature: description"

Combines all changes from the feature branch into one staged change on main.

When to use: When merging a feature branch and wanting one clean commit.

Squash using reset
git reset --soft HEAD~3
git commit -m "Combined: description"

Moves HEAD back 3 commits but keeps all changes staged. Then recommit as one.

When to use: Simple alternative to interactive rebase for recent commits.

← Back to Git Reference