Squashing Commits¶
Squashing refers to combining multiple commits into one.
Squashing is a good way to clean up commit history, reduce noise in PRs, fixing
commit mistakes, etc..
There is no actual git squash
command.
You'd typically do this with git rebase -i
or git merge --squash
.
When to squash:
- Working on a feature branch before merging to
main
. - To clean up commit history after experimenting.
- When submitting changes for review.
- Teams often require squashing before merging.
- When a single commit makes more sense than multiple small commits.
Table of Contents¶
Squash Commits from the CLI¶
There are two main ways to do this.
You can either use an interactive rebase (git rebase -i HEAD~n
), or squash during a
merge (git merge --squash some-branch
).
Using Interactive Rebase¶
E.g., to squash the last 3 commits:
This will open an interactive editor with a list of commits.
To squash these, change pick
to squash
(or just s
) for all but the first commit
Squash During a Merge¶
To merge a branch into main
while squashing:
git switch main
git merge --squash other-branch
git commit -m "Merged other-branch with a single commit"
Squashing Best Practices¶
- Use before pushing (or be ready to force push).
- Use when merging feature branches to keep history clean.
- Don't squash commits in a shared branch unless it's necessary (it rewrites history).
- Don't use
git rebase -i
if you're unsure. Backup or create a branch first.