Fixing a mistake in an earlier commit is done with an interactive rebase, which lets you pause at that specific commit, amend it, and then replay the remaining commits on top of the correction.
Key Points: • git rebase -i HEAD~n opens an editable list of the last n commits in your default text editor. • Changing pick to edit next to the problematic commit tells Git to stop there when the rebase runs. • After Git stops at that commit, you make the needed corrections and run git commit --amend to update it in place. • git rebase --continue replays the remaining commits on top of the corrected one, completing the rebase. • Because this rewrites history, it should only be done on commits that haven't been pushed and shared, or with team coordination if they have.
Example: A developer notices commit three of the last five had a typo in a variable name; they run git rebase -i HEAD~5, mark that commit edit, fix the typo, run git commit --amend, then git rebase --continue to reapply the later commits cleanly.
Code Example:
git rebase -i HEAD~5
# mark the target commit as "edit", save and close
# make the fix
git add .
git commit --amend
git rebase --continueInterview Tip: A concise interview answer is:
"I'd run an interactive rebase with git rebase -i, mark the problem commit as edit, fix it, and amend it with git commit --amend, then continue the rebase to replay the rest on top. It's a clean way to fix history, but only safe on commits that haven't been shared yet."