I accidentally erase files. Fortunately, I have added those to index. Please tell me how to recover those files to working tree, without commiting.
Asked
Active
Viewed 7,275 times
12
-
Note: in 2020: `git restore -- aFile`. See my [edited answer below](https://stackoverflow.com/a/9666522/6309). – VonC Oct 10 '20 at 10:45
1 Answers
17
2014: git checkout-index
will restore deleted files. It will NOT change the content of existing files.
(unless, as commented, using the -f/--force
option)
Charles mentions a simpler solution, which can also be used to restore the content of files which existing in the working copy to their last indexed state:
git checkout -- the_erased_file
But that can silently overwrite an existing file, so don't make mistake.
(git checkout-index
, by default, won't overwrite)
2020, Git 2.23+: use the git restore
command:
You can specify the source (index), default is the index and destination (working tree).
git restore -- aFile
That will restore the working tree from the index.
git restore

VonC
- 1,262,500
- 529
- 4,410
- 5,250
-
@CharlesBailey sure, but I am always a bit weary of `git checkout` for files. See second part of http://stackoverflow.com/a/2961348/6309 – VonC Mar 12 '12 at 11:58
-
Yes, `checkout` can be destructive but it is a user command whereas `checkout-index` is really plumbing. – CB Bailey Mar 12 '12 at 12:09
-
@CharlesBailey: plumbing, that is true, but in this case, plumbing seems preferable, no? See this debate: http://git.661346.n2.nabble.com/Best-way-to-check-for-a-dirty-working-tree-tp6465415p6472027.html – VonC Mar 12 '12 at 12:29
-
-
1@TomHale True, but in 2020, the actual command would be `git restore -- aFile`. See my edited answer. – VonC Oct 10 '20 at 10:44