Coding

How to Squash Commits in Git (rebase -i, reset --soft, merge --squash)

To squash your last 3 commits into one, run git reset --soft HEAD~3 and then git commit -m "New message". For more control, run git rebase -i HEAD~3 and change pick to squash (or fixup) on every line except the first.

git reset --soft HEAD~3
git commit -m "Add counter to app"

Both methods rewrite history. That is fine for commits that only exist on your machine or your own feature branch; if they are already pushed, read the section on pushing at the end before you start.

Method 1: squash the last N commits with git reset --soft

This is the quickest way when you want all of the last N commits to become one. --soft moves the branch back N commits but leaves the files and the staging area exactly as they are, so all the changes are staged and ready to commit again. The git-reset documentation uses this exact example (git reset --soft HEAD~5; git commit) to combine five commits.

$ git log --oneline -4
b474aaf WIP 3
e65cf2b WIP 2
e951193 WIP 1
9e41718 Add login form

$ git reset --soft HEAD~3
$ git status --short
M  app.txt
$ git commit -m "Add counter to app"
$ git log --oneline -2
12f11c3 Add counter to app
9e41718 Add login form

(Output from a test repository with Git 2.45; your hashes will differ.) Count carefully: HEAD~3 means "three commits back", so the three newest commits are squashed and 9e41718 is kept. If you have other uncommitted changes, commit or stash them first, or they will end up in the squashed commit too.

Method 2: squash with interactive rebase

Interactive rebase lets you choose which commits to combine, reorder them and rewrite messages. Start with the number of commits you want to look at:

git rebase -i HEAD~3

Git opens your editor with one line per commit, oldest first (the reverse of git log):

pick 89663b8 Add parser
pick f3a7c15 Handle empty input
pick 28cc02a Fix typo in parser

Change pick to squash (or s) on the lines you want to fold into the commit above them. The first line must stay pick, because there is nothing above it to fold into.

pick 89663b8 Add parser
squash f3a7c15 Handle empty input
squash 28cc02a Fix typo in parser

Save and close the editor. Git combines the three, then opens the editor again with all three messages so you can write the final one. Save that, and the rebase finishes with Successfully rebased and updated refs/heads/main.

To squash every commit on a feature branch since it left main, without counting, use the branch point instead of a number: git rebase -i main.

squash vs fixup

CommandKeeps the folded commit's changesMessage resultOpens an editor
squash / sYesAll messages combined, for you to editYes
fixup / fYesFolded commit's message discardedNo
fixup -CYesUses this commit's message instead of the first one'sNo
fixup -cYesUses this commit's message, opens editor to adjust itYes
drop / dNoCommit removed entirelyNo

Use fixup for "fix typo" and "oops" commits whose messages add nothing.

Fixup commits and --autosquash

If you already know a new change belongs in an earlier commit, record it that way and let Git arrange the rebase list for you:

$ git commit -a --fixup=6e374be
$ git log --oneline -3
1e44652 fixup! Add parser
a71a539 Other change
6e374be Add parser

$ git rebase -i --autosquash HEAD~3

With --autosquash, Git moves every commit whose message starts with fixup! or squash! directly under the commit it names and marks it fixup or squash. You only have to save the list. After the rebase in this example, the log shows just Other change and Add parser. Set git config --global rebase.autoSquash true to make this the default for interactive rebases.

Method 3: squash a whole branch when merging (git merge --squash)

To land a feature branch on main as a single commit without rewriting the branch itself:

git switch main
git merge --squash feature
git commit -m "Add export feature"

--squash stages the combined result of the branch but does not commit or record a merge, so you write the commit yourself. This is what the "Squash and merge" button on hosting platforms produces. One side effect: Git doesn't consider the branch merged, so git branch -d feature refuses with error: the branch 'feature' is not fully merged. Once you have checked the squashed commit is on main, delete the branch with git branch -D feature.

How to push after squashing

If the original commits were already pushed, a normal git push is rejected because your branch no longer contains them. Force-push, but with the safer flag:

git push --force-with-lease

--force-with-lease only overwrites the remote branch if it still points where your last fetch saw it. If someone else pushed in the meantime, the push fails instead of silently deleting their commits, which is what plain --force would do. Don't squash commits on a shared branch like main that others have already pulled; the reasoning is the same as for rebasing, covered in git merge vs rebase.

If the rebase goes wrong

During an interactive rebase, git rebase --abort puts the branch back exactly where it was before you started. If a conflict stops the rebase partway, fix the files, git add them and run git rebase --continue. If you already finished and dislike the result, git reflog lists where HEAD has been; reset to the entry from before the squash with git reset --hard HEAD@{n}. To move a single commit to another branch instead of combining commits, see git cherry-pick, and to throw away a commit entirely, see how to undo the last commit.

Frequently asked questions

What is the difference between squash and fixup?
Both fold a commit into the one above it in the interactive rebase list. squash keeps the folded commit's message and opens an editor so you can combine the messages. fixup throws the folded commit's message away and keeps only the first commit's message, so no editor opens.
Can I squash commits that are already pushed?
Yes, but it rewrites history, so you must force-push afterwards, and anyone who based work on the old commits will have to reconcile. Use git push --force-with-lease rather than --force, and avoid squashing commits on shared branches such as main.
How do I undo a squash?
Use the reflog. Run git reflog, find the entry from just before the rebase or reset (for example HEAD@{3}), and run git reset --hard HEAD@{3}. This restores the branch to its pre-squash commits as long as Git has not yet garbage-collected the old commits.
Who gets credited as author after a squash?
According to the git-rebase documentation, when commits with different authors are folded together, the resulting commit is attributed to the author of the first commit. Add Co-authored-by lines to the message if you want to credit the others.

Sources