Git – How to Force Pull and Overwrite Local Files

git

"Git Pull Force", "git reset branch to origin" or in other words, to pull a remote branch to overwrite a local branch, seems to be wildly searched feature with an increasing interest despite few local declines.

And it absolutely makes sense with growing teams and ever increasing number of developers.

shown is chart from Google Trends with increasing interest

Currently, the shortest working solution is quite verbose and requires knowledge of the branch

git reset --hard origin/<branch_name>

edit: There is a more convenient variant git reset --hard @{u}.
Please give credit where it is due comment. More shortcuts here.

which is unfortunate, as typing the following is so much faster

git pull

which, however, brings its own challenges. Diverging histories, merge conflicts, etc…


We do have shorthands such as this

git push origin HEAD -u --force

which pushes a local branch <branch_name> to an origin, overwrites a remote branch with same name <branch_name> and sets it as it's own upstream branch.

However, there is no such --force/reset alternative to git pull.


What would be the best way to have this feature added to git?


How do I force "git pull" to overwrite local files? 6.6m views
Reset local repository branch to be just like remote repository HEAD 4.7m views
How do I force git pull to overwrite everything on every pull? 370k views
Resolve conflicts using remote changes when pulling from Git remote 240k views
How to force update when doing git pull? 90k views
Force GIT Pull without commiting
Force a pull with git
git force pull with implicit rebase
Clean up a fork and restart it from the upstream
Force git to update my local repo when pulling
Reset all branches of a local repo to be the same as remote
Github – Discard all changes

Best Answer

I would underline that, while you certainly mention a need that arises daily, git commands that forcibly delete your work without warning are also a source of questions, with much more damageable consequences (look for "I lost my work after git reset --hard / git checkout ., can I get it back ?" questions).

With your pull -f example, this would be amplified by the fact that, when you inetract with a remote, you don't know what you are going to get from the remote.

Based on my humble experience, I stronly suggest to take the habbit of not using git pull and only use git fetch.

Then inspect the differences with origin/branchname, and then choose whether you want to reset or rebase or ...


It would be nice to have a command to say "move to that commit and discard all changes" in one go, I'll repeat here what I suggested in a comment to @VonC's answer :

#!/bin/bash

target=$1
if [ -z "$target" ]; then
    target=HEAD
fi

set -e  # avoid going forward if one command fails ...

git stash
git restore -SW -s "$target" -- .
git reset "$target"

(I don't have a good alias name for that : git goto ?)

It would give a reasonably safe alternative to git reset ; the main caveat being : a file on disk, not tracked in the starting commit but tracked in $target will be overwritten without being saved.

You would need a more convoluted variant of git stash to save those files too.


[update] I think I found a script that manages to stash away the impacted files (and only those) :

#!/bin/bash

target=$1
if [ -z "$target" ]; then
    target=HEAD
fi

set -e  # avoid going forward if one command fails ...

list_impacted_files () {
   local target=$1

   # tracked files in the working tree that have a diff
   git diff --no-renames --name-only HEAD

   # untracked files that will be clobbered when restoring $target :
   #   * files that are present in $target but not in HEAD
   #   * and that currently exist on disk (use 'ls' to keep only those)
   # add '|| true' to ignore error code returned by 'ls' on non existing files
   git diff --no-renames --name-only --diff-filter=A HEAD "$target" |\
     xargs -r ls 2> /dev/null || true 
}

# list files as described above, and feed this list to `git stash -u`
# 'xargs -r <cmd>' avoids running <cmd> at all if stdin is empty
# in our case: don't run 'git stash -u' if no files are to be stashed ...
list_impacted_files "$target" | xargs -r git stash -u --

git restore -SW -s "$target" -- .
git reset "$target"

To restore back your repo to how it was :

  • revert to the initial commit (using git reset or the script above ...)
  • spot the stash@{xx} you want to restore in git stash list
  • run git stash apply --index stash@{xx}
Related Question