On This Page

How to Squash Commits in Git (Interactive Rebase Guide)

Quick Answer: To combine multiple local commits into one clean commit before merging: (1) Run git rebase -i HEAD~N (where N is the number of commits), (2) Keep pick on the first commit and change pick to squash (or s) for the rest, (3) Save and edit the combined commit message.


Why Squash Commits?

During feature development, developers often create messy intermediate commits like "wip", "fix typo", or "debugging". Squashing combines these small, noisy commits into one logical commit (e.g., "Add OAuth2 authentication") before merging into main.


Method 1: Interactive Rebase (git rebase -i)

Step 1: Start Interactive Rebase

Specify how many past commits you want to inspect:

# Inspect the last 3 commits
$ git rebase -i HEAD~3

Step 2: Mark Commits to Squash

An editor window will open listing your commits from oldest (top) to newest (bottom):

pick a1b2c3d Add login form HTML
pick e4f5g6h Add login validation JS
pick 7h8i9j0 Fix syntax typo in login JS

Change pick to squash (or s) on all commits you want to merge into the top commit:

pick a1b2c3d Add login form HTML
squash e4f5g6h Add login validation JS
squash 7h8i9j0 Fix syntax typo in login JS

Save and close the editor.

Step 3: Write New Combined Commit Message

A second editor window will open. Write the single final commit message you want to keep:

Add user login feature with validation

Save and exit. Your 3 commits are now squashed into 1!


Method 2: Squash Merge (git merge --squash)

If you want to merge an entire feature branch into main as a single squashed commit without rebasing manually:

$ git checkout main
$ git merge --squash feature-branch
$ git commit -m "Add complete feature-branch functionality"

This stages all changes from feature-branch into your workspace as one uncommitted change set, ready for a single commit.

Read our complete /git-rebase and /git-merge guides for deeper architecture explanations.