Video in progress
A colleague rebased and force pushed a shared branch you are working on. Now when you `git pull`, every commit appears twice. How do you recover?
You are standing on the old commits, while the server has their rewritten copies. You need to move only your own new commits onto the new base: git rebase --onto, or git pull --rebase (which tries to work out the fork-point itself).
📖 Answer
The situation:
server (after the rebase): main---A'---B'---C'
yours: old---A---B---C---X---Y (X, Y are your commits)
A plain git pull (with merge) combines A-B-C and A'-B'-C', leaving duplicates in the history and sometimes conflicts. If you push such a merge, the whole benefit of the rebase is lost.
The right way is to move only X and Y:
git fetch origin
git rebase --onto origin/feature C feature
# new base old base (the last "foreign" commit you have)
In many cases the simpler one also works:
git pull --rebase
Git finds the fork-point from the history of origin/feature in the reflog, understands that A, B and C are already on the server (with different hashes but the same patch), and drops them.
To verify: git log --oneline origin/feature..feature should show only X and Y.
There is an organizational lesson too: tell the team before rebasing a shared branch, or do not do it at all. It is safer to update a branch several people work on with a merge.
Sign up to mark studied questions and save your progress.