Git interactive rebase — squashing and editing commits
Fast recall
Quick revision
Core answer
git rebase -i to open an editor listing every commit since the branch diverged. Mark commits with squash or fixup to fold them into a predecessor, reword to change a message, or drop to remove a commit entirely. The result is a polished, reviewable commit sequence before the branch is merged.Interview-ready guide
Interview answer
Interactive rebase is a history cleanup tool used immediately before opening or updating a PR — during development you commit freely, but the commits reviewers see should tell a coherent story about what the feature does.
The typical workflow
git rebase -i origin/mainopens the interactive editor listing every commit since the branch diverged; commits are shown oldest-first, opposite ofgit log- Mark well-scoped commits as
pickand everything else asfixup— fixup squashes the commit into the previous pick without preserving its message;squashlets you combine and edit the message - The goal: three to five logical commits where each represents one complete idea — the domain model, the service layer, the controller — so reviewers can step through the feature's evolution rather than a stream of "WIP" and "fix null check"
Safety practices
- Always use
--force-with-leaseinstead of--forcewhen pushing after a rebase — it checks that no one pushed to the remote branch while you were rebasing; bare--forcesilently overwrites any commits they added - Never interactive-rebase a branch that others have checked out — replaying commits produces new SHA1 hashes, making the remote branch incompatible with any local checkout based on the original SHAs
- Set
rebase.autoSquash trueglobally — thengit commit --fixupcreates a commit that interactive rebase automatically places and squashes below the referenced commit
Advanced: splitting a commit with `edit`
- Mark a commit as
editto pause the rebase at that point; rungit reset HEAD^to unstage everything, then recommit in separate logical units - Useful when a rename and a behavior change were accidentally committed together — splitting them makes
git bisectpinpoint bugs more precisely and makesgit blamemore informative
Git interactive rebase — squashing and editing commits
Direct Answer
Use git rebase -i <base> to open an editor listing every commit since the branch diverged. Mark commits with squash or fixup to fold them into a predecessor, reword to change a message, or drop to remove a commit entirely. The result is a polished, reviewable commit sequence before the branch is merged.
Check your understanding
Answer these without looking back at the article.
- What is the difference between `squash` and `fixup` in interactive rebase?
- How would you split a single commit into two separate commits?
- When would you use `git commit --fixup` and `git rebase --autosquash` together?
Related interview prep categories
1Looking for a structured roadmap?
Every module on this page is also part of the full Java backend interview track.