Interactive rebase
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.
Starting an interactive rebase
git rebase -i main # every commit on this branch that isn't on main
git rebase -i HEAD~4 # or: the last four commitsGit opens your editor with a todo list:
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:
pick e4b71f2 Add signup form
fixup 1a2b3c4 fix typo
reword 5d6e7f8 wip
fixup 3f2e1d0 Add email validation
drop 9a8b7c6 debug loggingResult: 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:
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 --continueFixup commits and autosquash
You often know at commit time which earlier commit a fix belongs to. Say so:
git commit --fixup e4b71f2 # creates "fixup! Add signup form"Later, --autosquash arranges the todo list for you — fixup commits move under their targets automatically:
git rebase -i --autosquash mainSet 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 --abortreturns to exactly where you started. - You finished and don’t like the result:
git reset --hard ORIG_HEAD, or find the pre-rebase state ingit reflog. git statusalways 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 -ibefore asking anyone to review.
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.
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 resultmkdir 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
git log --oneline main..HEAD
# 2. Rebase interactively onto main and rewrite the todo list.
# By hand: run `git rebase -i main` and edit the list in your editor.
# Here the list is rewritten with sed so the script runs unattended:
GIT_SEQUENCE_EDITOR='sed -i.bak -e "s/^pick \([0-9a-f]*\) fix typo/fixup \1 fix typo/" \
-e "s/^pick \([0-9a-f]*\) debug logging/drop \1 debug logging/" \
-e "s/^pick \([0-9a-f]*\) wip/reword \1 wip/" \
-e "s/^pick \([0-9a-f]*\) Add email validation/fixup \1 Add email validation/"' \
GIT_EDITOR='sed -i.bak "1s/.*/Validate email addresses/"' \
git rebase -i main
# 3. Check the result
git log --oneline main..HEAD # Validate email addresses / Add signup form
ls # no debug.jsSummary
git rebase -i <base>opens a todo list of your commits, oldest first, that you edit to rewrite history.pickkeeps,rewordedits the message,editpauses,squash/fixupcombine,dropremoves; reorder by moving lines.git commit --fixup <commit>plusgit rebase -i --autosquashfolds 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 --abortor the reflog undo it.