Interview Prep/Git & GitHub/Branching and Merging Workflows

A teammate branched from the wrong base, and their PR shows 40 unrelated commits. How do you fix it?

IntermediateScenarioBranching and Merging Workflows
Answer

Situation read → The branch was cut from another feature branch, so the PR drags that feature's commits along.

What I'd do →

  1. Identify the commits that really belong to the PR: git log --oneline origin/main..HEAD.
  2. Transplant only those onto the right base:
git rebase --onto origin/main <old-base-sha> my-branch
git push --force-with-lease
  1. Confirm the PR now shows only the intended commits.

Why → --onto replays commits in the range old-base..my-branch onto a new base, leaving the unwanted commits behind.

Interview tip → git rebase --onto is the answer interviewers hope to hear for "wrong base" questions.

All Git & GitHub questions Roadmap: Git & GitHub

More on Branching and Merging Workflows