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 →
- Identify the commits that really belong to the PR:
git log --oneline origin/main..HEAD. - Transplant only those onto the right base:
git rebase --onto origin/main <old-base-sha> my-branch
git push --force-with-lease
- 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.
More on Branching and Merging Workflows
You run `git pull` and Git stops with a merge conflict in `app.py`. What do you do?BeginnerYour feature branch is three weeks old and `main` has moved a lot. Merge `main` in, or rebase onto it?IntermediateThe team argues about fast-forward merges versus merge commits on `main`. How do you settle it?BeginnerBefore merging, you want to see exactly what the feature branch adds compared to `main`. Which commands?BeginnerHalfway through a merge you realise you pulled the wrong branch. How do you back out?BeginnerYou keep resolving the same conflicts again and again while rebasing a long-lived branch. How do you stop?Intermediate