How to Squash Commits in Git (Three Ways)
Squashing combines several commits into one so history stays readable. Three ways to do it — git reset --soft, interactive rebase, and GitHub's 'Squash and merge' — when each fits, how to push afterwards safely, and when you shouldn't squash.
While working on a feature, you end up with commits like "wip", "fix typo", "actually fix it", "oops". Squashing combines them into one clean commit — "Add password reset" — before they go into the main branch.
This is especially common with AI coding tools, which may commit after every small step.
Way 1: GitHub's "Squash and merge" (easiest)
If you use pull requests, you may not need to squash locally at all. On the PR page, choose Squash and merge from the merge button's dropdown. All the PR's commits land on main as one commit, and you edit its message there.
Many teams set this as the default in Settings → General → Pull Requests. (What is a pull request?)
Way 2: git reset --soft (simplest locally)
To squash your last 4 commits into one:
git reset --soft HEAD~4
git commit -m "Add password reset"
reset --soft moves the branch back 4 commits but keeps all their changes staged. The new commit contains everything. (git reset vs revert)
To squash everything since you branched off main:
git reset --soft $(git merge-base main HEAD)
git commit -m "Add password reset"
Way 3: interactive rebase (most control)
Interactive rebase lets you choose exactly which commits to combine, reorder them and edit messages:
git rebase -i HEAD~4
An editor opens:
pick a1b2c3d Add reset form
pick b2c3d4e wip
pick c3d4e5f fix typo
pick d4e5f6a Send reset email
Change pick to squash (or s) for commits to fold into the one above, or fixup (f) to fold in and discard their messages:
pick a1b2c3d Add reset form
fixup b2c3d4e wip
fixup c3d4e5f fix typo
pick d4e5f6a Send reset email
Save and close, and you get two clean commits. (Git rebase vs merge)
Interactive rebase needs an editor in your terminal. Many AI coding tools can't drive interactive editors, so for them the reset --soft method is the one to ask for.
Fixup commits, automatically
If you know a new commit fixes an earlier one:
git commit --fixup a1b2c3d
git rebase -i --autosquash main
The fixup is placed and marked automatically.
Pushing after a squash
If you'd already pushed the branch, your local history now differs from the remote, so a normal push is rejected. On your own feature branch, force-push safely:
git push --force-with-lease
--force-with-lease refuses if someone else pushed to the branch in the meantime, unlike plain --force. (Git push rejected)
Never squash and force-push a shared branch like main. Everyone else's copy would be out of sync.
When not to squash
- Commits that are each meaningful — "Add database migration" and "Add API endpoint" can be useful separately, for review and for git bisect.
- Shared branches others have pulled.
- Huge features where one giant commit becomes hard to review or revert.
The goal is a readable history, not the fewest commits. (Good commit messages)
The summary
- Pull requests: use Squash and merge.
- Locally:
git reset --soft HEAD~Nthen commit. - For fine control:
git rebase -iwithsquash/fixup. - Force-push with
--force-with-lease, and only on your own branch.
EasySpawn keeps your repository on a persistent server where Claude Code can tidy up its own commits before you open the pull request. See how it works or join the waitlist.
Related: git reset vs git revert · Git Rebase vs Merge · How to Write Good Commit Messages · Using Git With Claude Code
Keep reading
zsh vs bash: What's the Difference and Which Should You Use?
bash and zsh are both shells — the programs that read your terminal commands. Why macOS switched to zsh, the differences you'll actually notice (config files, completion, globbing, arrays), Oh My Zsh, and why scripts should usually still be written for bash.
What Is WSL? Running Linux on Windows, Explained
WSL lets you run a real Linux system inside Windows without dual-booting or a separate virtual machine to manage. What it is, why web developers use it, how to install it, where your files live, and the performance and networking gotchas.