Interview Prep/Git & GitHub/Rebase, Cherry-pick and History Rewriting

A commit from three days ago should never have been made. How do you remove it from your private branch?

IntermediateScenarioRebase, Cherry-pick and History Rewriting
Answer

Situation read → The commit is in the middle of history and unshared.

What I'd do →

  1. git rebase -i <bad-sha>^.
  2. Change pick to drop on that line (or delete it).
  3. Resolve conflicts from later commits that depended on it.
  4. git push --force-with-lease.

Why → Rebase replays everything except the dropped commit. If the branch is shared, use git revert instead.

Interview tip → Ask whether anyone else has the branch before dropping anything.

All Git & GitHub questions Roadmap: Git & GitHub

More on Rebase, Cherry-pick and History Rewriting