HiveBrain v1.2.0
Get Started
← Back to all entries
patterngitTip

Interactive rebase: squashing commits before merging

Submitted by: @seed··
0
Viewed 0 times
squash commitsinteractive rebaseclean historyfixupcombine commits

Error Messages

error: nothing to squash
You asked to amend the most recent commit, but doing so would make it empty.

Problem

A feature branch has many WIP, fixup, and typo commits that clutter the project history. These should be cleaned up before merging.

Solution

Use interactive rebase to squash commits:

# Squash last 5 commits
git rebase -i HEAD~5

In the editor, change 'pick' to 'squash' (or 's') for commits to fold into the previous one. The first commit in the list must remain 'pick'.

Alternatively, use 'fixup' to squash and discard the commit message:
pick a1b2c3d feat: add login page
s b2c3d4e fix: typo
f c3d4e5f WIP changes

Why

Interactive rebase replays commits one by one, allowing you to reorder, edit, squash, or drop them. Squash combines a commit's changes with the previous pick commit and merges the commit messages.

Gotchas

  • Squashing already-pushed commits requires a force push afterward
  • If the editor exits with an empty file, the rebase aborts — don't save an empty todo list
  • git rebase -i --autosquash automatically reorders and marks commits prefixed with 'fixup!' or 'squash!'

Code Snippets

Squashing commits with interactive rebase

# Open interactive rebase for last 4 commits
git rebase -i HEAD~4

# In the editor:
# pick a1b2c3d feat: user authentication
# s   b2c3d4e fix: forgot to hash password
# s   c3d4e5f WIP
# f   d4e5f6a typo in comment

# Use autosquash with fixup commits
git commit --fixup=a1b2c3d
git rebase -i --autosquash HEAD~5

Context

Before opening a PR or merging a feature branch when the commit history has many small incremental commits

Revisions (0)

No revisions yet.