Lesson Completion
Back to course

Interactive Rebase (Conceptual)

Beginner
10 minutes4.8Git

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.):

ActionShortWhat It Does
pickpKeep the commit as-is
rewordrKeep the commit but change its message
editePause to modify the commit's content
squashsMerge into the previous commit (combine messages)
fixupfMerge into the previous commit (discard this message)
dropdDelete the commit entirely
execxRun 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

flowchart TD START["git rebase -i HEAD~4"] --> EDITOR["📝 Editor Opens"] EDITOR --> PICK["pick: Keep as-is"] EDITOR --> SQUASH["squash: Combine commits"] EDITOR --> REWORD["reword: Change message"] EDITOR --> DROP["drop: Delete commit"] PICK --> RESULT["✅ Clean History"] SQUASH --> RESULT REWORD --> RESULT DROP --> RESULT style START fill:#0f3460,stroke:#53d8fb,color:#53d8fb style EDITOR fill:#1a1a2e,stroke:#ffd700,color:#ffd700 style RESULT fill:#1b2d1b,stroke:#53d8fb,color:#53d8fb

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 bisect effective.
  • Self-discipline: Cleaning up commits before merging is a hallmark of experienced developers.

Code

bash
# ─── 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 --abort

Key Takeaways

  • git rebase -i HEAD~N opens 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-lease when 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 squash and fixup in interactive rebase? A: Both merge a commit into the previous one. squash combines both commit messages (letting you edit the result). fixup discards the current commit's message and keeps only the previous commit's message. Use fixup for 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 as pick, change the remaining 4 to squash (or fixup), save, and edit the combined commit message. Alternatively, git reset --soft HEAD~5 followed by git commit achieves the same result.

Topics Covered

Git HistoryGit Rebase

Tags

#git#rebase#interactive#history

Last Updated

2026-02-13