C.W.K.
Stream
Lesson 02 of 05 · published

Interactive Rebase: The Commit Editor

~26 min · rebase, interactive

Level 0Untracked Rookie
0 XP0/47 lessons0/14 achievements
0/100 XP to next level100 XP to go0% complete

Interactive rebase is the commit editor

git rebase -i <base> opens an editor with one line per commit between <base> and your current HEAD. You edit the lines to instruct Git: keep this commit, drop that one, reorder these two, squash these together, edit the message of that one, stop and let me amend that one. When you save and exit, Git replays the commits according to your script. The result: a different shape of history that ends at the same content (or a different content if you edited).

The verb at the start of each line is the instruction. pick keeps the commit as-is. reword keeps the changes but lets you rewrite the message. edit stops the rebase at that commit so you can git commit --amend with arbitrary changes, then git rebase --continue. squash folds the commit into the previous one, combining messages. fixup is squash but discards the fixup commit's message. drop deletes the commit entirely. Reordering lines reorders commits; deleting a line equals drop.

The most useful patterns: clean up a feature branch before merge with git rebase -i main — squash WIP fixes into their parent commits, drop the experimental detour, reword vague messages. Restructure ten commits into three logical units. Edit a months-old commit to drop a now-embarrassing log line: git rebase -i <hash>~1, mark the target with edit, fix it, git rebase --continue.

Two safety facts. Rebase rewrites history. The commit hashes change. If those commits were pushed and others used them, you have a coordination problem. Rebase is reversible. Until you push, git reset --hard ORIG_HEAD rewinds to before the rebase. Even after, the reflog has the pre-rebase tip for 90 days. Mistakes during interactive rebase are loud (Git stops on conflicts) and recoverable (reflog) — making it one of the safer "powerful" Git operations.

Code

A typical interactive rebase script·text
# git rebase -i main produces something like this in your editor:

pick    abc1234 Add OAuth2 GitHub login
fixup   def5678 fix config typo
reword  9876543 docs: add OAuth setup notes
edit    fedcba0 refactor: extract token storage
drop    1234abc experiment: log every request
pick    aabbcc1 Add logout button

# Verbs:
#   pick    - keep as is
#   reword  - keep changes, rewrite message
#   edit    - stop here so I can amend this commit
#   squash  - fold into previous commit, combine messages
#   fixup   - fold into previous, discard this message
#   drop    - delete this commit
Interactive rebase mid-flow·bash
git rebase -i main
# Edit the script as needed, save, exit.
# Git starts replaying. If you marked any "edit":

# Git stops at the edit commit. Make changes:
git add src/auth.js
git commit --amend
git rebase --continue

# If conflicts arise on a pick/squash:
# (resolve files, then)
git add resolved-file
git rebase --continue

# Bail out completely, restore pre-rebase state:
git rebase --abort

External links

Exercise

On a scratch branch with at least 5 commits, run git rebase -i HEAD~5. In the script, exercise three verbs: reword on one commit, fixup to fold a small commit into the previous one, and edit to amend a commit mid-rebase. Use git rebase --continue to drive through the edit step. Confirm with git log that the result is the shape you scripted.

Progress

Progress is local-only — sign in to sync across devices.
Spotted a bug or have feedback on this page?Report an Issue

Comments 0

🔔 Reply notifications (sign in)
Sign inPlease sign in to comment.

No comments yet — be the first.