You committed to the wrong branch — how do you fix it?
Take a moment.
Say your answer out loud or write it down first — retrieval is what makes it stick. Reveal when you're ready.
Short answer
If unpushed: cherry-pick the commit onto the right branch, then remove it from the wrong one (reset). If pushed: same cherry-pick, but use revert (or coordinate before any history rewrite) on the shared branch.
Detailed explanation
The mechanics: switch to the correct branch, git cherry-pick <sha> copies the commit there with a new SHA; returning to the wrong branch, remove the original — git reset --hard HEAD~1 if it was the tip and unpushed, or interactive rebase to drop mid-history commits. Pushed commits change the calculus: rewriting shared history strands teammates, so prefer revert on the public branch (creating an explicit undo) while still cherry-picking forward to where the work belongs. Variations: committed everything to main instead of a feature? git branch feature creates the branch pointer at current position *before* resetting main back — no cherry-pick needed for the whole-batch case. Uncommitted changes headed to the wrong place? stash or git switch -c directly carrying them over. Prevention habits: status checks before committing, branch naming conventions, and IDE branch indicators.
Example
# Committed feature work on main by mistake (unpushed):
git branch feature/login # save position first!
git reset --hard origin/main # rewind main
git checkout feature/login # work continues correctly
# Or single commit already further along:
git checkout feature/login
git cherry-pick abc1234
git checkout main && git reset --hard HEAD~1Interview tip
Split the answer by pushed-vs-unpushed immediately, then show the whole-batch branch-then-reset trick — interviewers rarely expect candidates to know it.
Common mistakes
- Hard-resetting shared branches without coordination.
- Cherry-picking without cleaning up the original commit.