Git Squash Commits: 4 Easy Ways, Tested Step by Step
To squash commits in Git, combine several small commits into one with interactive rebase, reset –soft, merge –squash or fixup commits with autosquash. Here is each method with before-and-after logs we ran on Git 2.56, plus how to push safely and undo a squash.
- To git squash commits, there is no
git squashcommand; you squash withrebase -i,reset --soft,merge --squashor--fixupplus--autosquash. - In our test, four commits became one and
git diffagainst a backup branch was empty: squashing changes history, not code. - Squashing rewrites commits, so after pushing you need
git push --force-with-lease, and only on your own branches. - A bad squash is easy to undo with
git reflogor a backup branch made beforehand.
A feature branch often ends up looking like “Add login form”, “Fix typo”, “WIP”, “Address review comments”. Those commits helped while you worked, but they make the main branch history noisy and harder to search, revert or bisect. Learning to git squash commits lets you hand over one clean, meaningful commit instead. We ran every command below on Git 2.56 in a throwaway repository with a demo identity.
What does squashing mean?
Squashing combines several commits into a single new commit that contains all their changes. The file content at the end is identical; only the history is shorter. Because the squashed commit is new, it gets a new hash, which is why squashing counts as rewriting history, like a rebase. If rebase itself is unfamiliar, read our git merge vs rebase guide first.
Our messy branch
$ git log --oneline
53525d2 Address review comments
269f98a WIP
ec1ea71 Fix typo
362d7e9 Add login form
893db6f Initial commit
The goal: turn the top four commits into one called “Add login form with validation”. Before any history rewrite, make a safety net:
git branch backup-feature
Method 1: interactive rebase (most flexible)
git rebase -i HEAD~4
Git opens an editor listing the last four commits, oldest first. Keep the first as pick and change the others to squash (or s):
pick 362d7e9 Add login form
squash ec1ea71 Fix typo
squash 269f98a WIP
squash 53525d2 Address review comments
Save and close. Git opens a second editor with all four messages combined; replace them with one good message and save. Our result:
Successfully rebased and updated refs/heads/feature/login.
$ git log --oneline
08989b9 Add login form with validation
893db6f Initial commit
$ git diff backup-feature --stat
(no output)
The empty diff proves the code is exactly the same. Use fixup (f) instead of squash to throw away a commit’s message automatically, which is ideal for “Fix typo” and “WIP”. Interactive rebase also lets you reorder, edit or drop commits in the same pass, which makes it the most flexible option. In scripts, we set GIT_SEQUENCE_EDITOR and GIT_EDITOR to automate the editors; normally you just edit by hand.
Tip: if you do not know how many commits to count, use the branch point instead: git rebase -i $(git merge-base HEAD main) lists every commit on your branch.
Method 2: reset –soft (fastest)
git reset --soft HEAD~4
git commit -m "Add login form with validation"
$ git log --oneline
9138307 Add login form with validation
893db6f Initial commit
reset --soft moves the branch pointer back four commits but keeps every change staged, so a single commit captures them all. Again, the diff against the backup was empty. It is the quickest way to squash everything on a branch into one commit, with no editor involved. It cannot keep some commits separate; for that, use interactive rebase. Our git reset vs revert guide explains --soft, --mixed and --hard.
Method 3: merge –squash (squash while merging)
git switch main
git merge --squash backup-feature
git commit -m "Add login form (squashed)"
Squash commit -- not updating HEAD
app.txt | 4 ++++
$ git status --short
M app.txt
$ git log --oneline --graph
* d114bb0 Add login form (squashed)
* 893db6f Initial commit
Git applied all the branch changes to main and staged them, but did not commit, which is why we committed ourselves. The feature branch is left untouched. This is what the “Squash and merge” button does on GitHub and GitLab. One catch: because main does not record a real merge, Git will not know the branch was merged, so delete it afterwards to avoid merging it twice.
Method 4: fixup commits and autosquash (cleanest habit)
If you already know a new change belongs to an earlier commit, mark it at commit time:
git commit --fixup HEAD~1
$ git log --oneline -4
130a78e fixup! Add logout button
0228579 Change colour
1bc8545 Add logout button
Later, one command folds every fixup into its target, even with other commits in between:
GIT_SEQUENCE_EDITOR=true git rebase -i --autosquash HEAD~3
$ git log --oneline -3
4edf05c Change colour
a207434 Add logout button
d114bb0 Add login form (squashed)
The fixup disappeared into “Add logout button”, and the file contained the corrected content. Setting GIT_SEQUENCE_EDITOR=true accepts the generated plan without opening an editor. On our first attempt the fixup changed the same lines as the commit in between, so Git stopped with a conflict. Fixups move commits around, so conflicts are possible; resolve them, or run git rebase --abort to return to where you started.
Which squash method should you use?
| Method | Best for | Keeps some commits? | Editor needed |
|---|---|---|---|
rebase -i |
Cleaning a branch carefully | Yes | Yes |
reset --soft |
Squashing everything quickly | No | No |
merge --squash |
Squashing at merge time | No | No |
--fixup + --autosquash |
Planned fixes during review | Yes | No (optional) |
Pushing a squashed branch
If the commits were already pushed, the remote still has the old ones and a normal push is rejected. Use:
git push --force-with-lease
--force-with-lease refuses to overwrite the remote if someone else pushed in the meantime, unlike plain --force. Only do this on branches that you alone work on. Your CI pipeline will run again on the new commit, as described in our GitHub Actions CI/CD guide.
How to undo a squash
Old commits are not deleted immediately. The reflog records where the branch pointed before:
$ git reflog -4 feature/login
08989b9 feature/login@{0}: rebase (finish): refs/heads/feature/login onto 893db6f
53525d2 feature/login@{1}: commit: Address review comments
269f98a feature/login@{2}: commit: WIP
ec1ea71 feature/login@{3}: commit: Fix typo
To go back, run git reset --hard feature/login@{1} (or the hash 53525d2). Our backup branch also still pointed at 53525d2, which is the simplest undo of all. For bringing back a single commit instead, see our git cherry-pick guide.
Writing a good squashed commit message
A squash is your chance to replace “WIP” and “Fix typo” with a message that will still make sense in a year. A simple template works well:
Add login form with validation
- Email and password fields with client-side checks
- Shows a clear error for wrong credentials
- Rate-limits repeated failed attempts
Refs: TICKET-142
Keep the first line under about 50 characters and written as a command (“Add”, “Fix”, “Remove”). Leave a blank line, then explain what changed and why in short bullets, and link the ticket. Tools such as git log --oneline, release notes and semantic versioning changelogs read that first line, so make it count.
Squash locally or with the merge button?
GitHub, GitLab and Bitbucket all offer “squash and merge” on pull requests. It produces the same single commit as merge --squash, with no local commands and no force-push. Squash locally instead when you want to keep a few logical commits rather than one, or when reviewers should see a clean history before merging. Many teams do both: tidy with fixups during review, then squash-merge at the end.
When not to squash
- Shared branches: rewriting commits others have pulled causes confusing conflicts.
- Meaningful steps: a refactor and a feature are easier to review and revert as separate commits.
- Huge squashes: one 3,000-line commit hides what changed and makes
git bisectless precise. - Team rules: some teams prefer merge commits that keep full history. Agree on one approach.
Final thoughts
When you git squash commits, you trade noisy history for a readable one without changing a single line of code. Use interactive rebase for careful clean-ups, reset --soft for a quick “make it one commit”, merge --squash or your platform’s squash button at merge time, and fixup commits as a daily habit. Make a backup branch first, push with --force-with-lease, and remember the reflog if anything goes wrong. Keep our Git commands cheat sheet close for the rest.
Frequently asked questions
How do I squash the last 3 commits in Git?
Run git rebase -i HEAD~3, change pick to squash on the second and third lines, save, and write one commit message. Or run git reset –soft HEAD~3 followed by git commit.
What is the difference between squash and fixup?
Both combine a commit into the one above it during an interactive rebase. squash lets you edit the combined message, while fixup discards the squashed commit message.
Does squashing commits change my code?
No. The final files are identical; only the commit history changes. In our test, a diff between the squashed branch and a backup of the original branch was empty.
Can I squash commits after pushing?
Yes, but you must update the remote with git push –force-with-lease, and you should only do it on branches nobody else is using.
How do I undo a git squash?
Use git reflog to find the commit the branch pointed to before the squash, then git reset –hard to that entry, or reset to a backup branch you created first.
Keep learning
Need help with testing, WordPress or SEO?
We help teams ship better software and grow their sites. Tell us what you are working on and we will suggest a clear next step.