You can use
git checkout -- file
You can do it without the -- (as suggested by nimrodm), but if the filename looks like a branch or tag (or other revision identifier), it may get confused, so using -- is best.
You can also check out a particular version of a file:
git checkout v1.2.3 -- file # tag v1.2.3
git checkout stable -- file # stable branch
git checkout origin/master -- file # upstream master
git checkout HEAD -- file # the version from the most recent commit
git checkout HEAD^ -- file # the version before the most recent commit
More details included based on comments
First check the the commits of the file
git log -- <filename>
Then you can run this
git checkout <sha-reference> -- filename
where the sha-reference is a reference to the sha of a commit, in any form (branch, tag, parent, etc.
Answer from Brian Campbell on Stack OverflowYou can use
git checkout -- file
You can do it without the -- (as suggested by nimrodm), but if the filename looks like a branch or tag (or other revision identifier), it may get confused, so using -- is best.
You can also check out a particular version of a file:
git checkout v1.2.3 -- file # tag v1.2.3
git checkout stable -- file # stable branch
git checkout origin/master -- file # upstream master
git checkout HEAD -- file # the version from the most recent commit
git checkout HEAD^ -- file # the version before the most recent commit
More details included based on comments
First check the the commits of the file
git log -- <filename>
Then you can run this
git checkout <sha-reference> -- filename
where the sha-reference is a reference to the sha of a commit, in any form (branch, tag, parent, etc.
Just use
git checkout filename
This will replace filename with the latest version from the current branch.
WARNING: your changes will be discarded — no backup is kept.
How do you discard changes in one file?
Git Discard changes has no impact
Discard all changes made to a file in a branch
How do I discard unstaged changes in Git? - Ask a Question - TestMu AI (formerly LambdaTest) Community
I want to revert changes done in a commit to a single file, but not all the other changes which were done on this commit. It seems I can only find the option the revert all the changes done by this commit to all the files. (trying both with ToirtoiseGit and GitKraken)
I have a given file, yarn.lock, and I modify that file and many others in a branch I am working on (partners). I commit my changes periodically and eventually decide I want to merge the branch into master. If I want to merge all the files from partners to master, except yarn.lock, how would I remove all changes that have been made to yarn.lock? I am using GitHub, so when I go to the branch and hit "compare", I don't want it to show that there is anything to compare for yarn.lock.