Interactive rebase

Topic 2 of 5 in Rewriting history7 min readBeginnerFree course

Overview

While you work, commits pile up: “wip”, “fix typo”, “actually fix it”, “remove console.log”. That’s fine — commit often! But before others review your work, a few clear commits tell the story much better. Interactive rebase lets you rewrite your recent commits: combine them, reword them, reorder them or remove them.

Cleaning up with git rebase -i. Running git rebase -i HEAD~4 opens a todo list: pick e4b71f2 Add signup form; squash 1a2b3c4 fix typo; reword 5d6e7f8 wip; drop 9a8b7c6 debug logging — oldest commit at the top. Commands: pick keeps the commit as it is; reword keeps it and lets you edit the message; edit stops so you can change it; squash melts it into the one above; fixup is like squash but drops its message; drop removes the commit. Before: Add signup form, fix typo, wip, debug logging. After: Add signup form (with the typo fix) and Validate email addresses (reworded).

Starting an interactive rebase

Terminal
git rebase -i main        # every commit on this branch that isn't on main
git rebase -i HEAD~4      # or: the last four commits

Git opens your editor with a todo list:

Text
pick e4b71f2 Add signup form
pick 1a2b3c4 fix typo
pick 5d6e7f8 wip
pick 9a8b7c6 debug logging
pick 3f2e1d0 Add email validation

# Rebase 7c8d9e0..3f2e1d0 onto 7c8d9e0 (5 commands)
#
# Commands:
# p, pick <commit> = use commit
# r, reword <commit> = use commit, but edit the commit message
# e, edit <commit> = use commit, but stop for amending
# s, squash <commit> = use commit, but meld into previous commit
# f, fixup <commit> = like "squash", but discard this commit's log message
# d, drop <commit> = remove commit
# …

Note the order: oldest at the top — the opposite of git log. Git will replay the list from top to bottom.

You change the word at the start of each line, and/or move lines to reorder commits. Save and close the editor, and Git carries out your instructions.

The commands

Command Effect
pick keep the commit unchanged
reword keep the changes, edit the message (Git opens the editor for it)
edit stop after this commit so you can amend it — change files, split it, then git rebase --continue
squash combine with the commit above, and edit the combined message
fixup combine with the commit above, keeping only the above commit’s message
drop delete the commit (or delete the line)

For our messy branch:

Text
pick   e4b71f2 Add signup form
fixup  1a2b3c4 fix typo
reword 5d6e7f8 wip
fixup  3f2e1d0 Add email validation
drop   9a8b7c6 debug logging

Result: two clean commits — “Add signup form” (with the typo fix included) and “Validate email addresses”.

Splitting a commit

Mark it edit. When Git stops there:

Terminal
git reset HEAD~1            # undo the commit, keep its changes unstaged
git add signup.html
git commit -m "Add signup form markup"
git add signup.css
git commit -m "Style signup form"
git rebase --continue

Fixup commits and autosquash

You often know at commit time which earlier commit a fix belongs to. Say so:

Terminal
git commit --fixup e4b71f2        # creates "fixup! Add signup form"

Later, --autosquash arranges the todo list for you — fixup commits move under their targets automatically:

Terminal
git rebase -i --autosquash main

Set git config --global rebase.autoSquash true to make it the default. It’s a very efficient workflow: commit fixes as you go, clean up in seconds before review.

When things go wrong

  • A rebase stops with a conflict: resolve, git add, git rebase --continue.
  • You’re confused mid-rebase: git rebase --abort returns to exactly where you started.
  • You finished and don’t like the result: git reset --hard ORIG_HEAD, or find the pre-rebase state in git reflog.
  • git status always tells you whether a rebase is in progress and what to do next.

Guidelines for a good history

  • Each commit should make sense alone and leave the project working.
  • Separate refactoring from behaviour changes: “Extract price formatting” then “Show prices in local currency”.
  • Squash noise (“fix typo”, “oops”) into the commit it fixes.
  • Don’t over-polish: a pull request that ends up squash-merged into one commit (Pull requests) needs less tidying.

Since interactive rebase rewrites history, the golden rule applies: do it before others have your commits — typically just before opening or updating your own pull request. If you’ve already pushed the branch, you’ll need git push --force-with-lease afterwards.

Without the editor

For scripts and exercises, the editor can be replaced: GIT_SEQUENCE_EDITOR controls the todo list editor and GIT_EDITOR the message editor. The exercise solution uses them with sed so it runs unattended — but day to day, just edit the list by hand.

Git 2.54 and later also include an experimental git history command with shortcuts for common rewrites, such as git history reword <commit>, that don’t need a todo list. It’s still marked experimental, so this course sticks to rebase -i, which works everywhere.

The professional habit: commit messily while you explore, then spend two minutes with git rebase -i before asking anyone to review.

Exercise

Clean up a messy branch before review

Your branch has five commits: “Add signup form”, “fix typo”, “wip”, “debug logging” and “Add email validation”. Before opening a pull request, rewrite it into two clean commits: “fix typo” squashed into “Add signup form”, “debug logging” dropped, and “wip” combined with “Add email validation” under the message “Validate email addresses”. Since interactive rebase opens an editor, the solution shows how to script the todo list with GIT_SEQUENCE_EDITOR; in real life you’d edit the list by hand.

Terminal
mkdir polish && cd polish && git init -b main
echo "app" > app.txt && git add . && git commit -qm "Start app"
git switch -qc feature/signup
echo "<form>Sing up</form>" > signup.html && git add . && git commit -qm "Add signup form"
echo "<form>Sign up</form>" > signup.html && git commit -qam "fix typo"
echo "validate(" > validate.js && git add . && git commit -qm "wip"
echo "console.log('debug')" > debug.js && git add . && git commit -qm "debug logging"
echo "validate(email)" > validate.js && git commit -qam "Add email validation"

# 1. See the commits on this branch

# 2. Rebase interactively onto main and rewrite the todo list:
#    pick   Add signup form
#    fixup  fix typo
#    drop   debug logging
#    pick   wip            → reword to "Validate email addresses"
#    fixup  Add email validation

# 3. Check the result

Summary

  • git rebase -i <base> opens a todo list of your commits, oldest first, that you edit to rewrite history.
  • pick keeps, reword edits the message, edit pauses, squash/fixup combine, drop removes; reorder by moving lines.
  • git commit --fixup <commit> plus git rebase -i --autosquash folds fixes into the right commit automatically.
  • Use it to turn work-in-progress commits into a clean, reviewable story before opening a pull request.
  • It rewrites history — only on commits you haven’t shared, and git rebase --abort or the reflog undo it.