Back to Resources Resource

Git Interactive Rebase - Master Your Commit History

Clean up messy commits, squash WIP changes, reorder commits, and craft a perfect git history before pushing. Essential for professional workflows.

Your commit history looks like a crime scene. "WIP", "fix", "fix again", "actually fix it this time" - we've all been there. Git Interactive Rebase lets you rewrite history before pushing, transforming chaos into clean, reviewable commits.

🎯

Rewrite History Safely

Squash, reorder, edit, and drop commits before pushing. Never pollute main with "WIP" commits again.

Core Concept

Interactive rebase lets you modify commits in your branch before sharing them. Think of it as editing a draft before publishing:

BEFORE: Messy History main WIP stuff fix fix typo add tests oops forgot HEAD git rebase -i HEAD~6 AFTER: Clean History main feat: add user auth test: auth coverage HEAD 5 commits squashed into 2!

Key Techniques

📦

Squash Commits

Combine multiple commits into one logical change

🔀

Reorder

Move commits to group related changes together

✏️

Edit Messages

Reword commit messages for clarity

✂️

Split Commits

Break large commits into smaller, focused ones

Practical Examples

Start an interactive rebase for the last 5 commits:

# Start interactive rebase for last 5 commits
git rebase -i HEAD~5

# Or rebase onto a specific branch
git rebase -i main

This opens your editor with a list of commits. Each line starts with a command:

pick a1b2c3d Add user authentication
pick e4f5g6h WIP: login form
pick i7j8k9l Fix typo in login
pick m1n2o3p Add password validation
pick q4r5s6t Fix tests

# Commands:
# p, pick   = use commit as-is
# r, reword = use commit, but edit the message
# e, edit   = use commit, but stop for amending
# s, squash = use commit, but meld into previous commit
# f, fixup  = like squash, but discard this commit's message
# d, drop   = remove commit entirely

To squash the WIP commits into the main feature:

pick a1b2c3d Add user authentication
squash e4f5g6h WIP: login form
fixup i7j8k9l Fix typo in login
pick m1n2o3p Add password validation
fixup q4r5s6t Fix tests

Reorder commits by moving lines:

# Move tests commit to be right after the feature
pick a1b2c3d Add user authentication
pick q4r5s6t Add tests for authentication
pick m1n2o3p Add password validation

Why This Matters

  • Cleaner Pull Requests - Reviewers see logical, focused commits instead of "fix" noise
  • Better Git Blame - Each line traces back to a meaningful commit message
  • Easier Reverts - Atomic commits can be reverted without side effects
  • Professional History - Shows attention to detail and craft in your work
  • Bisect-Friendly - Clean history makes git bisect actually useful

Cheatsheet

Command Short Description
pick p Use commit as-is
reword r Edit commit message only
edit e Stop to amend commit (add/remove files)
squash s Meld into previous, combine messages
fixup f Meld into previous, discard message
drop d Remove commit entirely

Pro Tips

⚠️

Never Rebase Pushed Commits

Only rebase commits that haven't been pushed. Rebasing shared history causes merge conflicts for teammates. Use git log origin/main..HEAD to see what's safe to rebase.

🛡️

Create a Backup Branch

Before rebasing, create a backup: git branch backup-feature. If something goes wrong, reset with git reset --hard backup-feature.

🔄

Abort If Things Go Wrong

In the middle of a messy rebase? Run git rebase --abort to return to the state before you started. No harm done.

🎯

Use Autosquash for Fixups

Commit with git commit --fixup=<hash>, then git rebase -i --autosquash automatically orders fixup commits correctly.

📝

Split Large Commits

Use edit to pause at a commit, then git reset HEAD~1 to unstage changes. Add files in groups with separate commits, then git rebase --continue.

Getting Started

  1. 1

    Check Your Unpushed Commits

    Run git log origin/main..HEAD --oneline to see commits safe to rebase

  2. 2

    Create a Backup

    Run git branch backup-$(git branch --show-current) for safety

  3. 3

    Start Interactive Rebase

    Run git rebase -i HEAD~N where N is the number of commits to edit

  4. 4

    Edit and Save

    Change pick to squash, reword, etc. Save and close editor

  5. 5

    Verify and Push

    Check history with git log --oneline, then push your clean commits

Master Your Git History

Stop shipping "WIP" commits. Use interactive rebase to craft professional, reviewable commit histories.

Read Git Documentation

Explore More Resources