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:
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
The three commits are now replaced by a single commit that contains all their changes.
Why --soft and not --mixed or --hard
| Flag | Moves HEAD | Resets Staging Area | Resets Working Tree |
--soft | Yes | No | No |
--mixed (default) | Yes | Yes | No |
--hard | Yes | Yes | Yes |
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.
Your editor opens with a list like this:
To fold the second and third commits into the first, change pick to squash (or s):
Save and close. Git opens a second editor where you write the combined message, starting from all three original messages.
Rebase commands
| Command | Short | Effect |
pick | p | Keep the commit as-is |
squash | s | Merge into the previous commit, combine messages |
fixup | f | Merge into the previous commit, discard this message |
reword | r | Keep the commit but edit its message |
edit | e | Pause rebase to amend the commit |
drop | d | Remove the commit entirely |
fixup to keep only the first message
If you just want the first commit's message and no editing step:
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:
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
| Scenario | Best Method |
| Collapse last N commits into one, no fuss | git reset --soft HEAD~N |
| Selectively squash some commits, keep others | git rebase -i HEAD~N |
| Squash a feature branch into main | git merge --squash feature-branch |
| Rewrite commit messages during squash | git rebase -i HEAD~N with reword |
| Drop specific commits while squashing others | git 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:
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:
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:
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.
Related reading
- How to add images to README.md on GitHub?
- How to determine the URL that a local Git repository was originally cloned from
- I ran into a merge conflict. How do I abort the merge?
- Reset local repository branch to be just like remote repository HEAD
- What does cherry-picking a commit with Git mean?
- A Memory-Adaptive Merge Algorithm?
- About bubble sort vs merge sort
- Accidentally committed .idea directory files into git
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseNo 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 courseInterview Questions practice on Codemia
Over 11,000 interview practice questions for 300+ companies, searchable by company and role.