You rebased and force-pushed a branch your teammate also had checked out. What does he do now?
AdvancedScenarioRebase, Cherry-pick and History Rewriting
Answer
Situation read → His local branch has the old commits, the remote has new ones. A normal git pull will merge both and duplicate commits.
What I'd do →
- If he has no local commits:
git fetch && git reset --hard origin/feature. - If he has local commits:
git rebase --onto origin/feature <old-remote-tip> feature, where the old tip comes fromgit reflog show origin/feature. - Or
git pull --rebase, which uses the fork-point to drop the duplicates automatically.
Why → Git needs to know which of his commits are already rewritten upstream. Fork-point detection uses the reflog.
Interview tip → Better still: announce before rewriting shared branches, or avoid doing it.
More on Rebase, Cherry-pick and History Rewriting
Your PR branch has ten commits like "wip", "fix typo", "oops". How do you clean it up before review?BeginnerYou want the database migration commit to come before the code that uses it.BeginnerA critical fix on `main` must go to the `release/4.1` branch only.BeginnerYou must backport a feature made of 6 commits, including one merge commit.IntermediateBranch B was built on top of branch A. A was squash-merged into `main`, and now B shows A's commits again.IntermediateDuring review you get ten small change requests on old commits. How do you apply them cleanly?Intermediate