Squashing Commits with Rebase

🔁
Course Under Active Revision Re-visiting

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.

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 Commits
1 / 3
Interactive Rebase Todo Sheet (git rebase -i HEAD~3) Auto-generated editor
pick f2b2b2 wip auth
pick f3c3c3 fix typo in token
pick f4d4d4 forgot semicolon
💡 Result: The first commit is kept with pick; 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 (or p): Keep the commit as is.
  • squash (or s): Melds the commit into the previous one and prompts you to combine the commit messages.
  • fixup (or f): Melds the commit into the previous one but automatically discards its commit message (ideal for typo and linting fixes).
  • drop (or d): Deletes the commit from history entirely.

Untitled

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”

FeatureLocal Interactive Rebase (git rebase -i)GitHub PR “Squash and Merge”
Where it occursLocally on your development branch before opening or updating a PR.On GitHub’s web interface when closing a pull request into main.
GranularityHighly 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 stateYour local branch history is clean before pushing.Your local feature branch remains unchanged; only main receives the squashed commit.