# Git Squash Commits: Combine a Branch into One Commit

_Squash rewrites a run of commits as one commit with the same final tree. Pick the method by how much control you need over which commits merge._

## Goal

Turn a branch with commits like `Add login form`, `Fix typo` and `Address review` into one commit before review or merge. Every method below produces the same files. They differ in which commits they combine and what happens to the messages.

The examples start from this branch, run on Git 2.43:

```text
$ git log --oneline
50019dd Address review
17bc2e3 Fix typo
37198ff Add login form
d053cc9 Initial commit
```

## Squash every commit on the branch with `git reset --soft`

This is the fastest method when the whole branch should become one commit. `git reset --soft` moves the branch pointer back and leaves every change staged. The next commit records them all at once:

```bash
git reset --soft "$(git merge-base main HEAD)"
git commit -m "Add login form"
```

```text
$ git log --oneline
c2e7911 Add login form
d053cc9 Initial commit
```

`git merge-base main HEAD` finds the commit where the branch left `main`, so you do not count commits by hand. Use `HEAD~3` instead when you want to squash only the last three. The old messages are gone, so write the new one from scratch.

## Choose commits with interactive rebase

Use `git rebase -i` when only some commits should combine, or when you want to keep the old messages. Start it at the commit before the first one you want to change:

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

Git opens a todo list, oldest commit first:

```text
pick 37198ff Add login form
pick 17bc2e3 Fix typo
pick 50019dd Address review
```

Leave the first line as `pick`. Change each later line to `squash` or `s` to merge it into the commit above and combine the messages. Use `fixup` or `f` instead to merge it and throw its message away. Save and close the editor. With `squash`, Git opens a second editor with all three messages so you can edit the result.

```text
$ git log --oneline
b6c84fc Add login form
d053cc9 Initial commit
```

The first commit in the list cannot be a `squash`, because there is no commit above it to merge into. If the rebase stops on a conflict, resolve it and run `git rebase --continue`, or run `git rebase --abort` to go back.

## Mark fixes as you go with `--fixup`

If you know a commit is a correction to an earlier one, say so when you commit it. Git names the commit `fixup! ` and `--autosquash` moves it into place:

```bash
git commit -a --fixup=37198ff
```

```text
$ git log --oneline
dde9590 fixup! Add login form
50019dd Address review
17bc2e3 Fix typo
37198ff Add login form
```

When the branch is ready, rebase with `--autosquash`:

```bash
git rebase -i --autosquash main
```

The todo list arrives with the fixup already moved under its target and marked `fixup`:

```text
pick 37198ff Add login form
fixup dde9590 fixup! Add login form
pick 17bc2e3 Fix typo
pick 50019dd Address review
```

Save the list as it is. Set `git config --global rebase.autoSquash true` to make `--autosquash` the default for interactive rebases.

## Squash at merge time with `git merge --squash`

To leave the branch alone and land it as one commit on `main`, squash during the merge:

```bash
git switch main
git merge --squash feature
git commit -m "Add login form (#12)"
```

`git merge --squash` stages the combined change without committing and without recording the branch as a parent. GitHub's **Squash and merge** button does the same on the server. [Merge vs Rebase](/learn/concepts/git/merge-vs-rebase) compares the history each strategy leaves behind.

Because the branch is not a parent of the new commit, Git does not count it as merged:

```text
$ git branch -d feature
error: the branch 'feature' is not fully merged.
```

Delete it with `git branch -D feature` once you have confirmed the squashed commit is on `main`.

## Push the squashed branch

Squashing gives the branch new commit IDs. If you already pushed it, a plain push fails:

```text
 ! [rejected]        feature -> feature (non-fast-forward)
```

Push with a lease, which refuses to overwrite commits on the remote that you have not fetched:

```bash
git push --force-with-lease origin feature
```

[Force push with lease](/learn/concepts/git/force-with-lease) explains the check. If you squash a branch that other branches are built on, those branches still hold the old commits. Restack them as in [Rebase a Stacked Branch After a Squash Merge](/learn/answers/rebasing-and-stacks/rebase-stack-after-squash-merge).

## Undo a squash

The old commits stay in the [reflog](/learn/concepts/git/git-reflog) after any of these methods. Find the entry from before the squash and reset to it:

```text
$ git reflog -3
c2e7911 HEAD@{0}: commit: Add login form
d053cc9 HEAD@{1}: reset: moving to d053cc9ca5934a268418dd7641fa08a72edc6a63
50019dd HEAD@{2}: commit: Address review
```

```bash
git reset --hard HEAD@{2}
```

`git reset --hard` discards uncommitted changes, so commit or stash them first.

## How Treq handles it

When you merge a [Treq workspace](/docs/concepts/workspaces#merging-workspaces), you pick **Squash and merge** to combine the workspace commits into one commit on the target. Treq drops empty commits first, then removes the workspace after a clean merge.

## Related

- [Merge vs Rebase](/learn/concepts/git/merge-vs-rebase): when squash, merge and rebase each fit
- [What are Stacked PRs?](/learn/concepts/git/stacked-prs): keep changes small enough that each PR squashes to one clear commit
- [Recover a Deleted Git Branch](./recover-deleted-git-branch.md): find commits again after a reset or delete
