---
title: "How do I squash my last few commits into one in Git?"
description: "Combine several small commits into one clean commit using interactive rebase or git reset --soft, and learn when you need to force push afterwards."
url: https://www.danny.engineering/answer/git/how-do-i-squash-my-last-few-commits-into-one-in-git
author: Danny
published: 2026-11-27T00:00:00.000Z
updated: 2026-11-27T17:24:00.000Z
tags: ["Git"]
---

# How do I squash my last few commits into one in Git?

## The short answer


The quickest way to squash the last few commits is to move the branch back with `git reset --soft`, which keeps every change staged, and then make one new commit. Use interactive rebase instead when you want to pick which commits to combine.


```bash
git reset --soft HEAD~3
git commit -m "Add retry logic to the API client"
```


If those commits were already pushed, you will need `git push --force-with-lease` to update the remote branch.


## Why it happens


While working, it is normal to make lots of small commits: "wip", "fix typo", "actually fix it". They are useful as save points but noisy in the shared history. Squashing rewrites those commits into one, so the history explains what changed and why.


Squashing always creates a new commit with a new hash. The old commits are no longer on the branch. That is fine on your own feature branch, but it matters once other people have based work on the old commits.


## How to fix it


### Reset soft and recommit


`git reset --soft` moves the branch pointer back but leaves your files and the staging area untouched. Everything from the removed commits is staged, ready to commit again as one.


```bash
git log --oneline -5      # check how many commits to squash
git reset --soft HEAD~3   # undo the last 3 commits, keep the changes staged
git commit                # write one new message
```


This is simple and hard to get wrong, but it squashes everything after the reset point and you write the message from scratch.


### Squash everything since you branched


If you want the whole feature branch as one commit, reset to the point where it left main:


```bash
git reset --soft $(git merge-base HEAD main)
git commit -m "Add user profile page"
```


### Interactive rebase


Interactive rebase lets you choose commit by commit. Start it with the number of commits to look at:


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


Git opens an editor with one line per commit, oldest first:


```bash
pick a1b2c3d Add retry helper
pick d4e5f6a Fix typo
pick 0a1b2c3 Handle timeouts
pick 9f8e7d6 Update tests
```


Leave the first line as `pick` and change the others to `squash` (or `s`) to merge them into the commit above and edit the combined message, or to `fixup` (or `f`) to merge them and throw their messages away. Save and close the editor, and Git does the rest.


```bash
pick a1b2c3d Add retry helper
fixup d4e5f6a Fix typo
squash 0a1b2c3 Handle timeouts
fixup 9f8e7d6 Update tests
```


### Plan the squash with fixup commits


If you already know a new commit belongs with an earlier one, mark it as you make it:


```bash
git commit --fixup a1b2c3d
git rebase -i --autosquash main
```


`--autosquash` moves each fixup commit under its target and marks it for you, so you only need to save the todo list.


### Squash when merging the pull request


GitHub, GitLab and Bitbucket all offer a "squash and merge" option. If your team uses it, you do not need to squash locally at all; the branch lands on main as one commit.


### Push the result


If the branch was already on the remote, a normal push is rejected because the histories no longer match. Force push, but use the lease option so you do not overwrite commits someone else pushed in the meantime:


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


## Common pitfalls

- Squashing commits that are already on main or on a branch other people use. Everyone else then has commits that no longer exist upstream. Only rewrite your own branches.
- Using `git reset --hard` by mistake. That throws the changes away rather than keeping them staged. If it happens, `git reflog` can usually get them back.
- Counting wrong with `HEAD~n`. Check `git log --oneline` first.
- Marking the first line of an interactive rebase as `squash`. There is nothing above it to squash into, so Git stops with an error.
- Using plain `--force` instead of `--force-with-lease`.

## Related reading

- [Git documentation: git rebase, interactive mode](https://git-scm.com/docs/git-rebase#_interactive_mode)
- [Pro Git: Rewriting history](https://git-scm.com/book/en/v2/Git-Tools-Rewriting-History)
