git
squash
commit

Git: squash the last N commits into one

Codemia Knowledge Hub ·

To squash the last N commits into one, move the branch pointer back N commits with git reset --soft, which keeps all their changes staged, then make a new commit:

bash
git reset --soft HEAD~N
git commit -m "Your combined commit message"

This is the simplest and most reliable method. An interactive rebase (git rebase -i HEAD~N) also works and lets you choose which commits to keep, squash or reword, but the soft reset is faster when you want everything collapsed into one commit.

Method 1: git reset --soft

A soft reset moves the branch pointer back and leaves the working tree and staging area alone. Every change from the last N commits stays staged, ready for the new commit.

Example: squash the last 3 commits

bash
1# Check your current log first
2git log --oneline -5
3
4# Output:
5# a1b2c3d Fix typo in README
6# e4f5g6h Add input validation
7# i7j8k9l Add user registration form
8# m0n1o2p Previous feature commit
9# q3r4s5t Older commit
10
11# Soft reset back 3 commits
12git reset --soft HEAD~3
13
14# All changes from the 3 commits are now staged
15git status
16# Changes to be committed:
17#   modified:   README.md
18#   new file:   src/register.js
19#   modified:   src/validation.js
20
21# Create one new commit with all the changes
22git commit -m "Add user registration with validation"

The three commits are now replaced by a single commit that contains all their changes.

Why --soft and not --mixed or --hard

FlagMoves HEADResets Staging AreaResets Working Tree
--softYesNoNo
--mixed (default)YesYesNo
--hardYesYesYes

With --soft, changes from the "removed" commits stay staged. With --mixed they become unstaged modifications, and with --hard they are discarded. You want --soft because you plan to recommit exactly those changes.

Method 2: interactive rebase

Interactive rebase lets you decide per commit: keep, squash, reword, edit, reorder or drop.

bash
git rebase -i HEAD~3

Your editor opens with a list like this:

 
pick i7j8k9l Add user registration form
pick e4f5g6h Add input validation
pick a1b2c3d Fix typo in README

To fold the second and third commits into the first, change pick to squash (or s):

 
pick i7j8k9l Add user registration form
squash e4f5g6h Add input validation
squash a1b2c3d Fix typo in README

Save and close. Git opens a second editor where you write the combined message, starting from all three original messages.

Rebase commands

CommandShortEffect
pickpKeep the commit as-is
squashsMerge into the previous commit, combine messages
fixupfMerge into the previous commit, discard this message
rewordrKeep the commit but edit its message
editePause rebase to amend the commit
dropdRemove the commit entirely

fixup to keep only the first message

If you just want the first commit's message and no editing step:

 
pick i7j8k9l Add user registration form
fixup e4f5g6h Add input validation
fixup a1b2c3d Fix typo in README

The resulting commit carries the message of the pick commit.

Method 3: git merge --squash for a whole branch

To squash a feature branch into main, git merge --squash stages all of the branch's changes as one change:

bash
git checkout main
git merge --squash feature-branch
git commit -m "Add user registration feature"

No merge commit is created. The changes are committed as a single commit on main, and the branch's individual commits do not appear in main's history.

Which method to use

ScenarioBest Method
Collapse last N commits into one, no fussgit reset --soft HEAD~N
Selectively squash some commits, keep othersgit rebase -i HEAD~N
Squash a feature branch into maingit merge --squash feature-branch
Rewrite commit messages during squashgit rebase -i HEAD~N with reword
Drop specific commits while squashing othersgit rebase -i HEAD~N with drop

Pushing after a squash

Squashing rewrites history, so the new commit has a different hash from the originals. If the originals are already on the remote, you need a force push:

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

Prefer --force-with-lease over --force. It refuses to push if someone else pushed to the remote branch since your last fetch, so you can't overwrite their work by accident.

Don't force push to shared branches like main or develop without coordinating with your team. Squash before pushing, or squash on merge in the pull request UI.

Squash and merge in pull requests

GitHub, GitLab and Bitbucket offer a "Squash and merge" button that squashes all PR commits into one at merge time. For teams this is the safest option:

  • No one rewrites history locally
  • No force pushes are needed
  • The PR keeps its full commit history for review context
  • The target branch gets one clean commit

On GitHub, allow or require squash merging in the repository settings under "Merge button".

Undoing a bad squash with git reflog

If you squash and immediately see a mistake, git reflog shows the earlier state:

bash
1git reflog
2# 9f8e7d6 HEAD@{0}: commit: Add user registration with validation
3# m0n1o2p HEAD@{1}: reset: moving to HEAD~3
4# a1b2c3d HEAD@{2}: commit: Fix typo in README
5# ...
6
7# Restore the pre-squash state
8git reset --hard HEAD@{2}

The reflog keeps entries for about 90 days by default, so you have a safety net even after heavy history rewriting.

Squashing a range that contains merge commits

Rebase replays commits linearly, so git rebase -i can produce conflicts or confusing results when the range includes merge commits. The soft reset is more reliable here:

bash
git reset --soft HEAD~N
git commit -m "Squashed feature with merged changes"

A soft reset ignores the shape of the commits it collapses. It moves the pointer and leaves the accumulated changes staged.

Mistakes to avoid

Wrong N. This is the most common error. Run git log --oneline first to see which commit should become the new base. If N reaches back before your feature branch started, you squash commits that belong to other work.

Force pushing a shared branch. It overwrites other people's commits. Use --force-with-lease, and only force push branches you own. For shared branches, squash on merge in the hosting UI.

A throwaway combined message. The point of squashing "fix typo", "WIP" and "addressing review comments" is to replace them with one clear, descriptive message.

Uncommitted changes before git rebase -i. The rebase fails or behaves unexpectedly. Commit or stash first.

Mixing up squash and fixup. squash combines messages and lets you edit them; fixup silently discards the message. Pick based on whether you need that message.

Squashing commits already cherry-picked elsewhere. The squashed commit has a new hash, so Git does not see it as the same change, which leads to conflicts when the branches merge.


Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Basis Lab
No course covers this one yet

Basis Lab writes one for you from a sentence about what you want to be able to do, then teaches it and checks you understood. Your first course is free.

Build my course
Interview Questions practice on Codemia

Over 11,000 interview practice questions for 300+ companies, searchable by company and role.

Browse interview questions