🔀 Git & GitHub Intermediate

What is interactive rebase and how is it used for cleaning up commits?

Why Interviewers Ask This

This tests whether you can apply Git & GitHub knowledge to real-world scenarios. Interviewers are looking for clarity of thought and evidence that you've encountered this in production code.

Answer

Interactive rebase (git rebase -i) lets you edit, reorder, combine, or drop commits in the current branch before merging. It's the standard way to clean up "work in progress" commits before a PR. Usage: git rebase -i HEAD~5 — interactive editor showing last 5 commits. The editor lists commits oldest-first (opposite of git log). Commands for each commit: pick — use as-is; r/reword — keep commit but edit message; e/edit — pause to amend the commit; s/squash — melt into previous commit, combine messages; f/fixup — like squash but discard this commit's message; d/drop — delete commit entirely; reorder — drag lines to change commit order. Common workflow: develop with many small commits, then before PR: git rebase -i origin/main → squash WIP commits → reword messages → result: 2-3 clean, meaningful commits. Example: squash "Add login form", "fix typo", "fix again", "add tests" into one clean "Add login feature with tests" commit. After interactive rebase: git push --force-with-lease (since history was rewritten). Use only on private/unshared branches.

Pro Tip

Back up your answer with a specific project or situation. Saying 'In my last Git & GitHub project, I used this when...' immediately makes your answer more credible and memorable.