Git Cheat Sheet · Rewrite history
Group commits
git rebase -i (squash)
Squashing commits merges several small commits into a single, more readable one. The classic method is the interactive rebase: you keep the first commit as pick and set the following ones to squash or fixup.
What it does
Merges several commits into one, in place of the oldest.
A use case
Three small tweak commits really form a single change.
Warning: Rewrites the branch history.
On the command line
git rebase -i HEAD~3- Then
squash(ors) on the commits to merge into the previous one. git reset --soft HEAD~3 && git commit- Alternative: undoes the 3 commits while keeping everything staged, then commits again.
git merge --squash <branch>- Integrates a whole branch as a single commit.
In Merathon
- In the graph, select the commits with Ctrl + click or Shift + click.
- Right-click → "Squash the N selected commits…".
- Adjust the message of the resulting commit then confirm.
Frequently asked questions
What is the difference between squash and fixup?
squash combines the messages of the merged commits; fixup keeps only the first one's message.
Should I squash before a pull request?
It depends on the team. Many forges offer "Squash and merge", which does it at integration time.