Squashing Commits with Rebase
This article belongs to the Git Basics to Advanced course, which is currently being revisited with updated architectural patterns and content. Content may be refined shortly.
Table of Contents
9 sections
- 1. Interactive Squash Explorer
- 2. Why Squash Commits?
- 3. How to Squash Commits
- Step 1: Identify Your Target Range
- Step 2: Configure the Interactive TODO Sheet
- Step 3: Edit the Combined Commit Message
- 4. Pro Tip: The Automated --autosquash Workflow
- 5. Pushing Squashed Commits to Remote
- 6. Local Squash vs GitHub “Squash and Merge”
When developing a feature, you frequently produce several small, checkpoint-style commits (e.g., “typo fix”, “wip auth”, “linting”). Sharing this granular trail in production history can obscure the purpose of the overall change.
Git allows you to squash multiple intermediate commits into one atomic, descriptive commit using interactive rebase (git rebase -i).
1. Interactive Squash Explorer
Step through the visualization below to see how Git transforms noisy incremental WIP commits into a single unified commit:
Squashing Commits with Interactive Rebase
Step 1 of 3: Messy Incremental WIP Commitspick; subsequent commits marked with squash are melded into it with a unified message.
$ git log -n 3 --oneline Feature branch has 3 noisy commits: "wip auth", "fix typo in token", and "forgot semicolon".
2. Why Squash Commits?
- Atomic History: Every commit on your main branch represents a fully tested, functional change.
- Simplified Rollbacks: Reverting a feature requires undoing a single commit rather than navigating five fragments.
- Clear Code Reviews: Pull requests are easier to inspect and bisect when changes are grouped logically.
3. How to Squash Commits
Step 1: Identify Your Target Range
To squash the last $N$ commits, specify HEAD~N:
git rebase -i HEAD~3
Alternatively, to rebase from the oldest root commit:
git rebase -i $(git rev-list --max-parents=0 HEAD) HEAD
Step 2: Configure the Interactive TODO Sheet
Git launches an editable file listing your commits in chronological order (oldest at the top):
pick f2b2b2 wip auth
squash f3c3c3 fix typo in token
squash f4d4d4 forgot semicolon
pick(orp): Keep the commit as is.squash(ors): Melds the commit into the previous one and prompts you to combine the commit messages.fixup(orf): Melds the commit into the previous one but automatically discards its commit message (ideal for typo and linting fixes).drop(ord): Deletes the commit from history entirely.

Step 3: Edit the Combined Commit Message
After editing and saving the TODO sheet, Git combines the diffs. If you used squash, Git opens your editor to write a clean, unified commit message:
# This is a combination of 3 commits.
# The first commit's message is:
feat(auth): implement JWT authentication tokens
Save and close the editor, and Git finalizes the rebase.
4. Pro Tip: The Automated --autosquash Workflow
Instead of manually editing commit hashes and changing pick to fixup, Git provides a streamlined automation workflow:
# 1. You discover a bug or typo related to a previous commit (e.g., e4f5a6b)
# 2. Stage your fix
git add src/auth/token.js
# 3. Create an automated fixup commit linked directly to that target commit:
git commit --fixup e4f5a6b
# Git creates a commit with message "fixup! feat(auth): implement JWT"
# 4. Run interactive rebase with --autosquash enabled:
git rebase -i --autosquash origin/main
Git automatically positions the fixup commit directly underneath e4f5a6b in the TODO list and marks it with fixup without requiring manual reordering!
5. Pushing Squashed Commits to Remote
Because squashing combines commits and alters the historical SHA hashes of your branch, use --force-with-lease when updating your remote feature branch:
git push --force-with-lease origin feature-auth
6. Local Squash vs GitHub “Squash and Merge”
| Feature | Local Interactive Rebase (git rebase -i) | GitHub PR “Squash and Merge” |
|---|---|---|
| Where it occurs | Locally on your development branch before opening or updating a PR. | On GitHub’s web interface when closing a pull request into main. |
| Granularity | Highly flexible: you can squash 10 commits down to 2 or 3 atomic sub-features. | All commits in the PR are compressed into exactly 1 commit on main. |
| Local branch state | Your local branch history is clean before pushing. | Your local feature branch remains unchanged; only main receives the squashed commit. |