Undo & History

How to Squash Multiple Commits into One with Git Rebase

Combine multiple messy "wip" or "fix typo" commits into a single clean, atomic commit.

Instant 1-Line Solution
Reversible (History Change)Intermediate
git rebase -i HEAD~N
When to use this scenario:You have 5 commits on your feature branch like "fix typo", "oops", "wip test" and want to clean up your PR history before merging.

Step-by-Step Execution Guide

1Start interactive rebase for last N commits
git rebase -i HEAD~3

Opens your editor displaying the last 3 commits with "pick" next to each.

2Change "pick" to "squash" or "s"
# pick a1b2c3d First commit
# s d4e5f6a Second commit
# s g7h8i9j Third commit

Keep the first commit as "pick", and change subsequent commits to "squash" (or "s") to merge them into the first.

3Save and write a consolidated commit message
:wq

Git prompts you to enter a single unified commit message for all squashed changes.

Critical Pitfalls & Precautions

  • β€’Do not squash commits that have already been merged into a shared production branch.
Frequently Asked Questions

Squash Commits (Rebase) - Frequently Asked Questions

Common questions about safe rollback, remote history implications, and troubleshooting.

Run "git rebase --abort". Your repository will return to the exact state it was in before starting the rebase.