How to do a forced pull#

Maybe you are familiar with the command git push origin master -f, the command to forcefully overwrite new updates (if there are any) on the remote branch master, with your local changes.

Is there a pull equivalent to git push origin master -f? Something which will overwrite the latest changes on your local branch? Maybe git pull origin master -f?

Alas, there is no git pull origin master -f.

However, we can definitely do a "forced" pull. Here is how:

$ git fetch --all
$ git reset --hard origin/master

In this example, we are referring to the master branch, substitute it with the branch of your interest in your case.

This operation will overwrite all the tracked files (files you have git added ), whether they were committed or not. Untracked files will not be affected.

To those wondering, "why would anyone want to do a forced pull?"; there may be situations where your local commits are in conflict with the remote and you are in no mood to resolve them or your judgement says, "the changes on the remote are way better then yours". This operation is simply the git way of saying "YOU WIN!" to the remote branch.

Summary#

There is no single command to do a forced pull from the remote. However, using git fetch --all and git reset --hard origin/<branch>, we can overwrite the changes to the tracked files, effectively doing a "forced" pull.

Tweet this | Share on LinkedIn |