The Hook (The "Byte-Sized" Intro)
Your feature has 8 commits: "WIP", "fix typo", "oops wrong file", "actually fix", "WIP 2", "formatting", "add tests", "final cleanup". You want to merge, but that log is embarrassing. Interactive rebase (git rebase -i) is your editor: squash the WIPs into one, reword the message, drop the useless commits, and reorder the rest. Turn a messy draft into a clean story.
📖 What is Interactive Rebase?
git rebase -i opens a text editor showing your recent commits, letting you pick actions for each one. You can reorder commits, squash them together, edit their content, reword messages, or drop them entirely.
Conceptual Clarity
The actions (pick, squash, etc.):
| Action | Short | What It Does |
|---|---|---|
pick | p | Keep the commit as-is |
reword | r | Keep the commit but change its message |
edit | e | Pause to modify the commit's content |
squash | s | Merge into the previous commit (combine messages) |
fixup | f | Merge into the previous commit (discard this message) |
drop | d | Delete the commit entirely |
exec | x | Run a shell command between commits |
What the editor looks like:
pick a1b2c3d Add user model
pick b2c3d4e Fix typo in model
pick c3d4e5f Add user tests
pick d4e5f6g WIP: formatting
# Rebase abc123..d4e5f6g onto abc123
# Commands: p, r, e, s, f, d, x
Real-Life Analogy
Interactive rebase is like editing a video timeline. You can rearrange clips (reorder), merge short clips into one (squash), trim unwanted footage (drop), add a voiceover (reword), and fine-tune a scene (edit). The raw footage becomes a polished final cut.
Visual Architecture
Why It Matters
- Professional history: Turn messy development commits into clean, logical ones.
- PR quality: Reviewers love a focused commit per logical change.
- Bisect-friendly: Each commit does one thing, making
git bisecteffective. - Self-discipline: Cleaning up commits before merging is a hallmark of experienced developers.
Code
# ─── Edit the last 4 commits ───
git rebase -i HEAD~4
# Editor opens with:
# pick a1b2c3d Add user model
# pick b2c3d4e Fix typo in model
# pick c3d4e5f Add user tests
# pick d4e5f6g WIP formatting
# ─── Squash the typo fix into the model commit ───
# Change to:
# pick a1b2c3d Add user model
# fixup b2c3d4e Fix typo in model ← squashed into above
# pick c3d4e5f Add user tests
# drop d4e5f6g WIP formatting ← deleted
# Save and close the editor → Git replays the commits
# ─── Reword a commit ───
# Change "pick" to "reword" → save → new editor opens for message
# ─── Reorder commits ───
# Just move lines up/down in the editor
# ─── After interactive rebase, force-push ───
git push --force-with-lease
# ─── Abort if things go wrong ───
git rebase --abortKey Takeaways
git rebase -i HEAD~Nopens an editor to manipulate the last N commits.- squash/fixup combine commits; reword changes messages; drop removes commits.
- Reorder commits by rearranging lines in the editor.
- Always use
--force-with-leasewhen pushing after interactive rebase.
Interview Prep
-
Q: What is interactive rebase used for? A: To clean up commit history before merging. You can squash multiple commits into one, reword messages, reorder commits, drop unnecessary commits, and edit commit content — all in a single operation.
-
Q: What is the difference between
squashandfixupin interactive rebase? A: Both merge a commit into the previous one.squashcombines both commit messages (letting you edit the result).fixupdiscards the current commit's message and keeps only the previous commit's message. Usefixupfor WIP/typo commits. -
Q: How would you combine the last 5 commits into a single commit? A: Run
git rebase -i HEAD~5, keep the first commit aspick, change the remaining 4 tosquash(orfixup), save, and edit the combined commit message. Alternatively,git reset --soft HEAD~5followed bygit commitachieves the same result.