Command guide

How to Squash Commits in Git (3 Ways)

By Admin · Updated · 4 min read · Checked against the official Git docs

Squashing combines several commits into one. The quickest way to squash your last few commits is git reset --soft HEAD~N followed by a single git commit. Interactive rebase (git rebase -i) does the same with more control, and git merge --squash squashes a whole branch as you merge it.

Quick answer

git reset --soft HEAD~4
git commit -m "Add search box"

Replace 4 with how many commits you want to combine.

Squash the last Git commits into one with reset --soft or rebase -i

How we tested: every command on this page was run in a real Git repository (Git 2.43) on 18 November 2026, and the output shown is copied from the terminal. Your commit hashes will differ.

Here’s a typical feature branch before review: one real commit and three “please don’t look at these” commits.

$ git log --oneline -5
4781aa5 make tests pass
14485fd fix typo
d505ca2 wip
7773c76 Add search box
e33ff44 Add homepage

Method 1: reset –soft (fastest)

$ git reset --soft HEAD~4
$ git commit -m 'Add search box'
[main f28475a] Add search box
 1 file changed, 4 insertions(+)
 create mode 100644 search.js
$ git log --oneline -2
f28475a Add search box
e33ff44 Add homepage

Four commits became one, with all their changes. --soft moves the branch back four commits but leaves every change staged, so the next commit contains all of it. No editor, no todo list.

Count carefully. git log --oneline first, and make sure HEAD~N doesn’t reach past your own commits into ones that are already on main.

Method 2: interactive rebase (more control)

Use this when you want to keep some commits separate, reorder them, or squash ones that aren’t at the top. Run git rebase -i HEAD~4 and Git opens a list, oldest first:

pick 7773c76 Add search box
fixup d505ca2 wip
fixup 14485fd fix typo
fixup 4781aa5 make tests pass

Leave the first line as pick and change the others. squash (or s) folds a commit into the one above it and lets you edit the combined message. fixup (or f) does the same but throws its message away, which is what you want for “wip” and “fix typo”. Save, close:

$ git rebase -i HEAD~4      # changed lines 2-4 from 'pick' to 'fixup'
Successfully rebased and updated refs/heads/main.
$ git log --oneline -2
a27f7da Add search box
b90b2d0 Add homepage

Plan ahead with –fixup and –autosquash

If you already know a new change belongs to an earlier commit, tell Git when you commit it. Here we fix something in “Add search” while an unrelated commit sits in between:

$ printf 'fix\n' >> search.js
$ git add search.js
$ git commit --fixup HEAD~1
[main dcee7eb] fixup! Add search
 1 file changed, 1 insertion(+)
$ git log --oneline -3
dcee7eb fixup! Add search
f9d1b73 oops
544ffe9 Add search
$ git rebase -i --autosquash HEAD~3
Successfully rebased and updated refs/heads/main.
$ git log --oneline -3
7cab037 oops
ac2244e Add search
cc2a31a Add homepage

--autosquash moved the fixup! commit under its target and marked it for you, so the todo list needed no editing. “oops” stayed separate because it wasn’t marked. Set git config --global rebase.autoSquash true to make this the default.

Method 3: squash a whole branch when merging

$ git merge --squash feature/search
Updating 173acb5..8b59647
Fast-forward
Squash commit -- not updating HEAD
 s.js | 1 +
 1 file changed, 1 insertion(+)
 create mode 100644 s.js
$ git commit -m 'Add search (squashed)'
[main 47e273a] Add search (squashed)
 1 file changed, 1 insertion(+)
 create mode 100644 s.js
$ git log --oneline -2
47e273a Add search (squashed)
173acb5 Add homepage

All four commits from feature/search land on main as one. Note that git merge --squash stages the changes but doesn’t commit (that’s the “not updating HEAD” line), so you always need the git commit after it. GitHub’s and GitLab’s “Squash and merge” buttons do the same thing on the server.

One side effect: Git doesn’t record this as a merge, so it won’t know feature/search was merged. git branch -d feature/search will complain that it’s not fully merged; use -D once you’ve checked.

If the commits were already pushed

Squashing rewrites history, so push your branch with git push --force-with-lease. Do this on your own feature branch, not on main or any branch other people have pulled.

FAQ

How do I squash the last 3 commits?

Run git reset --soft HEAD~3 and then git commit -m "message". Or run git rebase -i HEAD~3 and mark the second and third lines squash or fixup.

What is the difference between squash and fixup?

Both fold a commit into the one above it. squash keeps its message so you can merge the messages; fixup discards it.

Can I squash commits after pushing?

Yes, then push with git push --force-with-lease. Only do it on branches nobody else is working on.

How do I undo a squash?

Find the commit from before the squash in git reflog and run git reset --hard <hash>.

Written by Admin

Writes practical Git guides for Git Commands. Every command is checked against the official Git documentation.

About Git Commands