git pull after remote forced update

When collaborating in teams with Git, someone else may do a “force push” on a feature branch, that conflicts with revisions you’ve previously pulled. Here are a few simple scenarios to resolve this situation quickly. For simplicity, in this article we assume you’re working in a Git branch feat1.

First, make a copy of the entire top-level directory of the Git repo in case a mistake is made.

No new local work

If you haven’t done any work you want to save in feat1:

git checkout feat1

git fetch

git reset origin/feat1 --hard

That will erase any changes you made locally in feat, and now your local feat matches the Git repo.

update and preserve local work

If you’ve done work in feat1 that you want to preserve:

git checkout feat1

git fetch

git reset origin/feat1 --soft

The work you’ve done can be committed as usual after the reset.


Written by Michael Hirsch, Ph.D. //