What is interactive rebase and what can you do with it?
Learn what git rebase -i does — reorder, squash, edit, reword, and drop commits to clean up your history before sharing, with examples and best practices.
Expected Interview Answer
Interactive rebase (git rebase -i) lets you rewrite a series of commits by editing a to-do list, so you can reorder, edit, combine, split, drop, or reword commits before they are shared.
You run git rebase -i <base> and Git opens an editor listing every commit from base to HEAD with a command keyword you can change (pick, reword, edit, squash, fixup, drop, or reorder lines). Git then replays the commits according to your instructions, creating brand-new commits with new hashes. It is the standard way to clean up a messy local branch into a tidy, reviewable history before pushing or opening a pull request.
- Squash noisy WIP commits into meaningful ones
- Reword unclear commit messages
- Reorder commits into a logical sequence
- Split a large commit into smaller focused ones
- Drop commits that should never have been made
- Produce a clean, reviewable history before sharing
AI Mentor Explanation
Interactive rebase is like a coach reviewing raw net-session footage before the highlights reel airs. Each delivery is a commit on a to-do list, and the coach can keep a good ball, re-label a mislabelled one, merge two similar drills into a single clip, drop a wild wide, or swap the order so the montage tells a clean story from warm-up to match-winning shot.
Step-by-Step Explanation
Step 1
Pick a base
Run git rebase -i <base>, e.g. git rebase -i HEAD~5 to edit the last five commits, or git rebase -i main.
Step 2
Edit the to-do list
Git opens an editor listing commits oldest-first with 'pick' keywords; change keywords or reorder lines to describe what you want.
Step 3
Choose actions
Use reword to change a message, squash/fixup to combine, edit to stop and amend, drop or delete a line to remove, and reorder lines to reorder.
Step 4
Let Git replay
Save and close; Git replays each commit per your instructions, pausing when a step needs input or a conflict arises.
Step 5
Resolve and continue
Fix any conflicts, git add the files, then git rebase --continue; use git rebase --abort to bail out safely.
What Interviewer Expects
- Knowing rebase -i rewrites commits and changes their hashes
- Familiarity with pick, reword, edit, squash, fixup, drop keywords
- Understanding you only rebase unshared/local history
- How to resolve conflicts and use --continue / --abort
- Awareness of the reflog as a safety net
Common Mistakes
- Interactive-rebasing commits already pushed to a shared branch
- Confusing squash (keeps both messages) with fixup (discards the second)
- Forgetting that the to-do list is ordered oldest-to-newest
- Force-pushing over teammates' work without --force-with-lease
- Not knowing --abort restores the original state
Best Answer (HR Friendly)
“Interactive rebase is a Git tool that lets you tidy up a batch of your recent commits before sharing them. You get an editable list where you can reorder them, merge small ones together, fix messages, or remove mistakes, so the final history is clean and easy to review.”
Code Example
# Open the editor for the last 3 commits
git rebase -i HEAD~3
# In the editor, change the keywords, e.g.:
# pick a1b2c3d Add login form
# squash e4f5g6h Fix typo in login form
# fixup i7j8k9l WIP debug log
# Save and close; edit the combined message when prompted.
# If a conflict appears while replaying:
git status
# resolve files, then:
git add .
git rebase --continue
# Changed your mind? Restore the original state:
git rebase --abortFollow-up Questions
- What is the difference between squash and fixup?
- Why should you avoid rebasing commits that are already pushed?
- How does the reflog help you recover from a bad rebase?
- How do you split one commit into two during a rebase?
- What does --force-with-lease do and why prefer it over --force?
MCQ Practice
1. In an interactive rebase to-do list, which keyword combines a commit into the previous one but discards its message?
fixup merges the commit into the preceding one and drops its message, whereas squash keeps both messages for you to combine.
2. After interactive rebase rewrites commits, what happens to their commit hashes?
Rebase creates brand-new commit objects, so every rewritten commit gets a new SHA-1 hash.
3. Which command safely restores your branch if a rebase goes wrong mid-way?
git rebase --abort stops the rebase and returns the branch to its exact pre-rebase state.
Flash Cards
What does git rebase -i do? — Opens an editable to-do list of commits so you can reorder, edit, squash, fixup, reword, or drop them before they are shared.
squash vs fixup — Both merge a commit into the previous one; squash keeps both messages, fixup discards the merged commit's message.
Golden rule of rebase — Never rebase commits that have already been pushed to a shared branch — it rewrites history others may depend on.
How to recover a bad rebase — Use git rebase --abort during the rebase, or git reflog afterward to find and reset to the original HEAD.