Home > Mobile >  Git Pull Force to overwrite local files
Git Pull Force to overwrite local files

Time:05-11

"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.

enter image description here

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

git reset --hard origin/<branch_name>

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

CodePudding user response:

You need at least three commands:

  • a fetch, to update the remote tracking branches
  • a reset to the upstream branch, using the @{u} or @{upstream} suffix
  • a clean to make sure no extra files are left

That is:

git fetch
git reset --hard @{u}
git clean -nd

(replace in git clean the -nd option by -fd to actually remove the files: I always prefer to first preview what a git clean will do, before actually deleting anything)

You would have to group those as an alias or a script (git-pullreset), that you can then call.


LeGEC suggests in the comments:

git stash
git restore -SW -s @{u}
git reset @{u}

That would:

  • leave completely untracked files (e.g: files not tracked in HEAD nor in @{u}) untouched, and
  • would give a way to still have something to revert to "how it was before".

CodePudding user response:

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.

  •  Tags:  
  • git
  • Related