Git Reset Single File to Origin Version
Sometimes you have a file in your local git clone that you want to simply reset to whatever is at origin
.
You might want to do this if the origin
copy is pristine and your local copy is messed up. Even if you did a git reset --hard
, it would not be fixed, because even your committed version at HEAD
is still not what you want.
If the messed-up file in question was package-lock.json
(which it sometimes is!), you can set the contents of the file to whatever the contents are at origin/HEAD
by running:
git reset origin/main package-lock.json
This also presumes the branch name is main
.
This will change the file contents. Then because the contents will presumably differ with what's committed locally, you'll need to run a git commit
to make it permanent.