"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.
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 aftergit 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 usegit 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 :
(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) :
To restore back your repo to how it was :
git reset
or the script above ...)stash@{xx}
you want to restore ingit stash list
git stash apply --index stash@{xx}