Git Squash Commits
In this page:
What does Squashing Mean?
Squashing merges several small, often messy commits ("wip", "fix typo", "actually fix it") into one clean commit that represents the finished change as a single logical unit — useful when your in-progress commit history is more useful to you while working than it is to anyone reading the log afterward.
Example: What does Squashing Mean?
git log --oneline
# wip
# fix typo
# actually fix it
Using interactive rebase instructions
git rebase -i <base> opens an editor listing your commits; leaving the first as pick and changing every commit after it to squash (or s) tells Git to fold each of those into the one above it when the rebase runs.
Example: Using interactive rebase instructions
git rebase -i HEAD~3
# pick a1b2c3d Add login form
# squash e4f5g6h fix typo
# squash h7i8j9k actually fix it
Writing the Combined Commit Message
After the rebase runs, Git combines the messages from all the squashed commits and opens an editor for you to write one final message describing the change as a whole, discarding or keeping pieces of the originals as you choose.
Example: Writing the Combined Commit Message
git rebase -i HEAD~3
# combine messages into one final message describing the change
Squashing during a Merge
Many hosting platforms offer squash-merge as a button on a pull request, collapsing every commit on the branch into one commit on the target branch at merge time — this achieves the same clean-history result without you having to run an interactive rebase yourself.
Example: Squashing during a Merge
git merge --squash feature-x
git commit -m "Add feature x"
Pushing Squashed Commits
Squashing commits that were already pushed rewrites history, so a plain git push will be rejected — you need git push --force-with-lease to update the remote branch, and doing this on a branch anyone else has already pulled will make their local history diverge and cause them real pain.
Example: Pushing Squashed Commits
git push --force-with-lease
Chapter Quiz — Complete all 6 topics to unlock
0/6 topics done
Complete these topics first: