270

I'm trying to undo local changes to a specific file. Nothing has been committed.

When I want to revert all changes, I can perform git revert --reset HEAD. However, in this case, I don't want to revert all changes to all files.

Its not clear or obvious to me how to revert just a file, even after reading the git-revert(3) man page:

NAME
       git-revert - Revert some existing commits

SYNOPSIS
       git revert [--[no-]edit] [-n] [-m parent-number] [-s] <commit>...
       git revert --continue
       git revert --quit
       git revert --abort
...

This is similar to How to revert a specific file in a old commit on git, but no commits have been performed. And unlike OP, who wants to go back to an arbitrary commit, I just want the file to return to MASTER's copy of it.

Under SVN, I would just delete the file in question and then perform a svn update.

How do I revert changes to a single file?

Community
  • 1
  • 1
jww
  • 83,594
  • 69
  • 338
  • 732

1 Answers1

487

You don't want git revert. That undoes a previous commit. You want git checkout to get git's version of the file from master.

git checkout -- filename.txt

In general, when you want to perform a git operation on a single file, use -- filename.



2020 Update

Git introduced a new command git restore in version 2.23.0. Therefore, if you have git version 2.23.0+, you can simply git restore filename.txt - which does the same thing as git checkout -- filename.txt. The docs for this command do note that it is currently experimental.

mkasberg
  • 12,144
  • 2
  • 38
  • 42
  • 2
    I did not checkout. I'm using the default work space provided by git. That is, I performed a `git clone` and then went to work. – jww Jul 08 '15 at 00:51
  • 8
    Doesn't matter. That command will still work. You implicitly checkout whichever branch you cloned. Also, run `git status` There should be a one-liner explaining how to revert unstaged changes. `(use "git checkout -- ..." to discard changes in working directory)` – mkasberg Jul 08 '15 at 01:03
  • 3
    `git checkout -- "my file name with spaces.txt"` - Use double quotes enclosing your file name in case it has spaces. – RBT Apr 07 '17 at 06:44
  • 5
    This didn't work for me. Still says "error: Your local changes to the following files would be overwritten by merge" – Coxy May 29 '17 at 04:17
  • @Coxy Can't help without a lot more info. Perhaps open a new question about why `git checkout -- filename` isn't working for you. Include a reference to what you tried from this question. Also include the output of `git status` and the other commands that you're running. – mkasberg May 30 '17 at 17:30
  • This can be confusing for users coming from Mercurial because the mercurial command is `hg revert ...`. Although there is a `git revert` command, it performs an entirely different task from `hg revert`. – mkasberg Jan 03 '18 at 20:32
  • 2
    Why is `--` needed ? I did `git checkout file.txt` and achieved the same thing – Ciprian Tomoiagă Jan 17 '18 at 15:15
  • 4
    @CiprianTomoiaga it is not strictly required. It is used for [Argument Disambiguation](https://git-scm.com/docs/git-checkout#_argument_disambiguation) (to resolve conflicts between branch names and file names). If you provide it, the arguments after it will always be interpreted as filenames, which is desired here. – mkasberg Jan 17 '18 at 15:23
  • 1
    Please make sure that you have space after -- . `git checkout -- (space) filename` – Sibeesh Venu Apr 08 '18 at 15:24
  • Upvoted, because I'm not a fan of the suggested duplicate solution. The suggested solution has 13 answers, overwhelming for someone unfamiliar with git. Basically the reason is the use of `checkout` which is seriously counter intuitive. – Yimin Rong Jul 07 '19 at 13:54
  • `git checkout` cycles through changed files with tab key whereas `git restore` does not. Or am I missing something? – Julen May 19 '21 at 15:37
  • `git checkout` or `git restore` does not work if the local change is to remove the file. In that situation, the only thing I've found which works is to `git reset` the file and _then_ `git restore` the file... – David Given May 21 '21 at 20:57