Squashing combines multiple commits into a single one. This is useful for cleaning up a feature branch before merging — turning a series of "WIP" or "fix typo" commits into one coherent commit with a meaningful message.

## Start an interactive rebase

To squash the last 3 commits, use [`git rebase`](/git/commands/git-rebase) in interactive mode:

```bash
git rebase -i HEAD~3
```

Replace `3` with however many commits back you want to go. Git will open your editor with a list of commits, oldest first:

```
pick a1b2c3d Add user authentication
pick e4f5a6b Fix validation bug
pick 7c8d9e0 Update tests
```

You can check your commit history beforehand with [`git log`](/git/viewing-previous-commits) to identify which commits to squash.

## Choose what to do with each commit

Change `pick` to `squash` or `fixup` for any commit you want to fold into the one above it:

```
pick a1b2c3d Add user authentication
squash e4f5a6b Fix validation bug
fixup 7c8d9e0 Update tests
```

| Command | What it does |
|---------|-------------|
| `pick` | Keep the commit as-is |
| `squash` | Fold into the commit above; lets you edit the combined message |
| `fixup` | Fold into the commit above; discards this commit message silently |

Save and close the editor. If you used `squash`, Git opens the editor again to write the final combined message. If you only need to fix a typo in your last commit message, see [how to edit your last commit message](/git/faqs/edit-last-commit-message) instead.

## Do not squash commits that are already pushed

Squashing rewrites history. If those commits have already been pushed to a shared branch, rewriting them will cause divergence for anyone else who has pulled them.

If you need to squash already-pushed commits, force-push afterward with [`git push`](/git/publishing-local-changes):

```bash
git push --force-with-lease origin your-branch
```

`--force-with-lease` is safer than `--force` — it refuses to push if someone else has pushed to the branch since you last fetched.

Ready to streamline your Git workflow? [DeployHQ](https://www.deployhq.com) deploys your code automatically whenever you push to your repository.