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 (or s) 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

  1. In the graph, select the commits with Ctrl + click or Shift + click.
  2. Right-click → "Squash the N selected commits…".
  3. 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.

See also