Question:
If another developer made a change to git
history and then did git push -f
, git pull
doesn't work for me.
How can I bring the changes from the remote?
Answer:
Translated from git pull after forced update
The first step is to receive all the commits that are in the remote, for that I do
git fetch
I assume we are talking about master
. If not, replace master
with the name of the branch you are working with.
Reset
To point the local branches to the same commit as the remote one, you have to use git reset
.
If you don't mind losing your local changes:
git reset origin/master --hard
This will undo all your changes and bring the same thing that is on the remote. From now on, git push
and git pull
will work as usual.
If you want to keep your changes local:
Instead of a hard reset, do a soft reset
git reset origin/master --soft
You can apply your local commits on top of what's on the remote using git rebase
git rebase -i origin/master
This will execute a rebase
in interactive mode, where you can choose how to apply your local commits that are not on the remote above the current HEAD
.
If the changes in the history deleted a commit that you have local, they will appear as commits pending to apply again. If you don't want them to apply, you will have to erase them as part of the rebase.
Use git command --help
for more details and examples of how to apply any of these commands.