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 Overflow
🌐
Git
git-scm.com › book › en › v2 › Git-Basics-Undoing-Things
Git - Undoing Things
It tells you pretty explicitly how to discard the changes you’ve made. Let’s do what it says: $ git checkout -- CONTRIBUTING.md $ git status On branch master Changes to be committed: (use "git reset HEAD <file>..." to unstage) renamed: README.md -> README
Discussions

How do you discard changes in one file?
How do I do the equivalent of git reset foo/bar/ or git checkout foo/bar/ so I can throw away changes on that file/dir only? I've seen the d and D options but they all seem to apply to the whole directory, not just a path. ... If you want to discard just a specific file, move the cursor to the file. More on github.com
🌐 github.com
1
4
Git Discard changes has no impact
Wappler Version : 3.7.4 Operating ... Clicking to discard git changes should perform its action Actual behavior Nothing appears to change How to reproduce chrome_debug.log 4.zip (2.6 KB) Screen Recording 2021-02-05 at 1.46.56 PM Here is the diff for this file (nothing ... More on community.wappler.io
🌐 community.wappler.io
0
2
February 5, 2021
Discard all changes made to a file in a branch
You can use git checkout -- path/from/repo/root/to/yarn.lock . For example, if yarn.lock is in the root of your repo and you want to restore it to the commit beginning with 123abc, you can git checkout 123abc -- yarn.lock then when you commit the file, it should be in the same state it was as of commit 123abc. Note that the diff won't show the changes (when they hit "compare"), but the history will show whatever intermediate states the file went through unless you're doing something that rewrites the history, such as rebasing or squashing. More on reddit.com
🌐 r/git
27
8
May 9, 2021
How do I discard unstaged changes in Git? - Ask a Question - TestMu AI (formerly LambdaTest) Community
How do I discard unstaged changes in Git More on community.testmuai.com
🌐 community.testmuai.com
0
June 28, 2024
🌐
Reddit
reddit.com › r/git › reverting changes to a single file - why are so simple things so difficult?
r/git on Reddit: Reverting changes to a single file - why are so simple things so difficult?
March 17, 2023 -

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)

🌐
Git Tower
git-tower.com › learn › git faq › how to discard changes in git with git restore
How to Discard Changes in Git with git restore | Learn Version Control with Git
4 days ago - Discard uncommitted changes with git restore. Covers discarding working directory changes, unstaging files, and restoring from specific commits.
🌐
GitLab
docs.gitlab.com › topics › git › undo
Revert and undo changes | GitLab Docs
On branch main Your branch is up-to-date with 'origin/main'. Changes not staged for commit: (use "git add <file>..." to update what will be committed) (use "git checkout -- <file>..." to discard changes in working directory) modified: <file> no changes added to commit (use "git add" and/or ...
Find elsewhere
🌐
Wappler Community
community.wappler.io › wappler general › bugs
Git Discard changes has no impact - Bugs - Wappler Community
February 5, 2021 - Wappler Version : 3.7.4 Operating System : mac Server Model: node Database Type: Hosting Type: custom Expected behavior Clicking to discard git changes should perform its action Actual behavior Nothing appears to change How to reproduce chrome_debug.log 4.zip (2.6 KB) Screen Recording 2021-02-05 at 1.46.56 PM Here is the diff for this file (nothing changed):
🌐
Graphite
graphite.com › guides › git-discard-changes
How to discard changes in Git
Discard all changes in the working directory: If you want to discard changes from all files in the working directory: ... This will revert all of your tracked files back to the state they were in at the last commit. However, this will not affect "untracked" files, new files that have been created locally but not committed yet, or other files that are auto-generated and shouldn't be committed to the repository like build artifacts, compiled binaries, log files etc. Git 2.23 introduced a more straightforward command called git restore, designed to make it easier to work with changes:
🌐
DEV Community
dev.to › reiyszw › discard-file-changes-by-vscode-instead-of-git-stash-47h6
Discard File Changes by VSCode Instead of git stash - DEV Community
June 25, 2020 - Lately, I have found a much simple solution to discard file changes. Everyone should use that. ... That's it! If you have specific files to discard, You only need to select that file. I'm not familiar with this type of superpower of VSCode. If you know about VSCode tips to improve productivity, Please let me know. I would really appreciate it. Thanks! ... git restore --staged pathToFileOrFolder # first unstage file git checkout pathToFileOrFolder # then revert changes
🌐
Tempertemper
tempertemper.net › blog › git-restore-to-discard-changes
Git restore to discard changes – tempertemper
throw out all of your changes, regardless of filetype or directory using git restore . (actually, you can do that with git checkout --, but I like that the . follows the same pattern as git add) discard changes to staged files by using the --staged ...
🌐
Developer Community
developercommunity.visualstudio.com › content › problem › 221309 › git-undo-changes-on-files-that-differ-only-in-crlf.html
Git: "Undo Changes..." on files that differ only in CR/LF can't ...
March 22, 2018 - Skip to main content · Visual Studio · Guidelines Problems Suggestions Code of Conduct · Downloads · Visual Studio IDE Visual Studio Code Azure DevOps Team Foundation Server Accounts and Subscriptions · Subscriber Access · Microsoft Security Azure Dynamics 365 Microsoft 365 Microsoft ...
🌐
Tempertemper
tempertemper.net › blog › using-git-restore-to-discard-changes-within-a-file
Using Git restore to discard changes within a file – tempertemper
May 11, 2021 - Just like git add’s patch mode, we don’t have to use the full --patch flag; we get a handy -p shortcut. The following command would enter patch mode for every file we’ve edited since our last commit: ... Once we’re in patch mode, we just need to work our way through the changes by typing a letter from the multitude of options: Discard this hunk from worktree [y,n,q,a,d,g,/,j,J,k,K,s,e,?]?
🌐
TheServerSide
theserverside.com › blog › Coffee-Talk-Java-News-Stories-and-Opinions › remove-revert-discard-local-uncommitted-changes-Git-how-to
How to discard local changes in Git
The easiest way to restore your working directory and discard any local changes is to issue a git stash command. This not only discards all local changes, but it also stores a record of your changes for future retrieval with a pop or apply command. ...
🌐
Better Stack
betterstack.com › community › questions › git-undo-all-working-dir-changes-including-new-files
Git: Undo All Working Dir Changes Including New Files | Better Stack Community
Remove untracked files and directories with git clean -fd. Optionally, unstage changes with git reset. These commands will restore your working directory to the state of the last commit, discarding all modifications and new files.
🌐
Reddit
reddit.com › r/git › discard all changes made to a file in a branch
r/git on Reddit: Discard all changes made to a file in a branch
May 9, 2021 -

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.

🌐
Atlassian
atlassian.com › git › tutorials › resetting checking out and reverting
Resetting, Checking Out & Reverting | Atlassian Git Tutorial
December 16, 2025 - Note that this removes all of the subsequent changes to the file, whereas the git revert command undoes only the changes introduced by the specified commit. Like git reset, this is commonly used with HEAD as the commit reference.
🌐
Developer Community
developercommunity.visualstudio.com › content › problem › 681074 › git-undo-changes-does-not-remove-file-from-unstage.html
Git - Undo changes does not remove file from unstaged ...
August 7, 2019 - Skip to main content · Visual Studio · Guidelines Problems Suggestions Code of Conduct · Downloads · Visual Studio IDE Visual Studio Code Azure DevOps Team Foundation Server Accounts and Subscriptions · Subscriber Access · Microsoft Security Azure Dynamics 365 Microsoft 365 Microsoft ...
🌐
CloudBees
cloudbees.com › blog › git-undo-commit
How to Undo Changes in Git with Git Undo Commit
November 25, 2020 - Git stash lets you discard changes and save them for later reuse. Try Git checkout --<file> to discard uncommitted changes to a file.
🌐
Earth Data Science
earthdatascience.org › home
Undo Local Changes With Git | Earth Data Science - Earth Lab
March 30, 2021 - The output from git status also tells you that you can use git checkout -- <file> to discard changes to that file in your repo.