Before You Squash
Warning: squashing replaces commits with new ones that have different hashes. If others have already pulled the old commits, their clones no longer match the branch, and the next pull mixes old and new commits. Squash only on your own branch, or on commits you have not pushed. Commit or stash uncommitted changes first, so the working tree is clean:
git status
git log --oneline -5
Squash with Interactive Rebase
To combine the last three commits:
git rebase -i HEAD~3
Git opens your editor with one line per commit, the oldest at the top:
pick a1b2c3d Add login form
pick e4f5a6b Fix typo in login form
pick f6e5d4c Style login button
Leave the first line as pick and change the words on the lines below it:
pick a1b2c3d Add login form
squash e4f5a6b Fix typo in login form
fixup f6e5d4c Style login button
Save and close the editor. Git combines the commits and, because of squash, opens the editor again with the messages together, so you can write the final message. If the editor is new to you, see how to save and quit in Vim.
squash or fixup?
| Word | Short | Commit message |
|---|---|---|
squash | s | Kept, for editing |
fixup | f | Discarded |
pick | p | Commit kept as is |
A squash or fixup line joins the commit above it. That is why the first line must stay pick.
Squash Everything Since main
To turn a whole feature branch into one commit, rebase it interactively onto main:
git rebase -i main
The list shows the commits of your branch that are not on main. Mark every line except the first as fixup or squash. If main has moved on, the branch is also moved onto its latest commit, which can bring conflicts to resolve.
The git reset --soft Shortcut
Without an editor list, move the branch back and commit again:
git reset --soft HEAD~3
git commit -m "Add login form"
--soft keeps all changes of the three commits staged, so the new commit contains exactly the same files. The modes are explained in git reset: soft, mixed, and hard.
Squash While Merging
To leave the feature branch as it is and put its work on main as one commit:
git switch main
git merge --squash feature
git commit -m "Add login form"
This does not rewrite anything, so it is safe on shared branches. Git does not record a merge, though, so git branch -d feature can later refuse and say the branch is not fully merged. Delete it with -D once you have checked that its work is on main; see how to delete a Git branch.
Push the Squashed Branch
If the old commits were pushed, a normal push is rejected. Use the safe form of a force push:
git push --force-with-lease origin feature
--force-with-lease refuses when someone else pushed to the branch since your last fetch, so you do not erase their work. Never use a plain --force for this, and never force-push main or another branch that others work on.
Stop or Undo a Rebase
- During the rebase:
git rebase --abortreturns everything to how it was before you started. - A conflict: fix the files,
git addthem, and rungit rebase --continue. - After it finished: the old commits are still in the reflog.
git reflog
git reset --hard feature@{1}
feature@{1} is where the branch pointed before its last change, which is the state before the rebase. Read git reflog to confirm before you reset, and check git status first, because --hard discards uncommitted edits.