What is git rebase -i, and what can you do with it?
Interactive rebase opens a list of your recent commits so you can reorder, reword, squash, edit, or drop them before they are replayed. It is the standard way to tidy a messy branch before opening a pull request.
Explain it simply
Before submitting a rough-work notebook, you tear out the scribbled pages, merge two pages into one and fix headings. Interactive rebase is that editing pass over your commits.
git rebase -i HEAD~4 opens an editor listing the last four commits, oldest first. Each line starts with a command: pick keeps it, reword changes the message, squash melds it into the previous commit, fixup does the same but discards its message, edit pauses so you can change the content, drop deletes it, and moving lines reorders them. Save and Git replays the commits, creating new hashes.
Example
pick 1a2b3c4 Add login form
fixup 5d6e7f8 typo in login form
reword 9a0b1c2 wip stuff
drop 3d4e5f6 debug logging
# git rebase -i HEAD~4What to say in the interview
"git rebase -i lets me rewrite the last few commits: pick, reword, squash, fixup, edit, drop, or reorder them. I use it to clean up a branch before a pull request. Since it creates new commits, I only do it on branches nobody else is building on."
Common mistakes
- Running it on commits already shared with teammates.