19

In git I am familiar with how to checkout individual files that have been deleted using the git checkout -- [<paths>...] syntax (which is recommended when you do git status.

To get all the files you could create a list and give the list as the argument to the above command.

However when you just want all the files that have been deleted (i.e. rm -rf in your cwd and then you want to restore all files) generating that list is inelegant.

How do you checkout all deleted files?

Trevor Boyd Smith
  • 18,164
  • 32
  • 127
  • 177
  • I added what I usually do to clear untrack files in my answer below. It is very handy most cases after a `git reset --hard` :) – Gayan Jan 11 '17 at 18:34

6 Answers6

24

Generating the list is not that hard:

git diff --no-renames --name-only --diff-filter=D

To make it suitable for git checkout, use -z and xargs -0:

git diff --no-renames --name-only --diff-filter=D -z |
    xargs -0 git checkout --

Note that using git checkout -f -- . is quite different from the above, as git checkout -f -- . will overwrite files that are modified but not yet added to the index, while the above will only extract, from the index, files that are still in the index but are no longer in the work-tree.

(If you have no such modified files, git checkout -f -- . will work, but then so will git checkout -- ..)

torek
  • 448,244
  • 59
  • 642
  • 775
  • 1
    To find deleted files, use filter=D, not filter=R – Oli Jul 26 '17 at 07:50
  • @Oli: oops, yes, yikes! – torek Jul 26 '17 at 12:36
  • So to checkout the deleted files: `git checkout $(git diff --no-renames --name-only --diff-filter=D)` – Klesun Oct 04 '18 at 13:05
  • 1
    @ArturKlesun: yes, provided the file names don't have white-space in them (if they do you need to do something fancier to keep the shell from treating `this file name has spaces` as naming five files named `this` and `file` and so on.) – torek Oct 04 '18 at 15:19
  • @torek, you could just start the answer with `git checkout -- .` (though I wonder if it's recursive) – Pacerier Apr 23 '20 at 16:23
  • @Pacerier: it is recursive, but the issue here is that the question asks to check out only *deleted* files. Running `git checkout -- .` will copy the index version of each file out, erasing all the work you did in any un-`git add`-ed files. (This is all a little bit clearer when using the new `git restore` command that first appeared in Git version 2.23. There is a "safe" `git checkout`, which is now available as `git switch`, and a "destroy all my work if I'm not careful" `git checkout`, which is now `git restore`.) – torek Apr 23 '20 at 18:54
11

when you just want all the files that have been deleted (i.e. rm -rf in your cwd and then you want to restore all files)

You want

git checkout-index -a
jthill
  • 55,082
  • 5
  • 77
  • 137
6

I usually do a

git checkout .

to checkout changes followed by

git clean -fd

This would remove untrack files. f for files and d for directories.

You could do also do a dry run prior to 2nd step by doing

git clean -fdn

This would list down files and directories to be deleted.

Please refer more info on undoing changes

Gayan
  • 3,614
  • 1
  • 27
  • 34
2

If the changes for delete files has not committed, you can use git checkout -- ..

If the changes for delete files has been committed, you can use git reset --hard HEAD~.

Marina Liu
  • 36,876
  • 5
  • 61
  • 74
2

I was able to do it with:

for f in $(git diff --no-renames --name-only --diff-filter=D);do git checkout -- $f ;done
dafnahaktana
  • 837
  • 7
  • 21
0

The command git checkout --force seems to do the trick. The man page for the --force option says "[The --force option] is used to throw away local changes".

From the git-checkout man page:

   -f, --force
       When switching branches, proceed even if the index or the
       working tree differs from HEAD. This is used to throw away 
       local changes.

       When checking out paths from the index, do not fail upon 
       unmerged entries; instead, unmerged entries are ignored.
Trevor Boyd Smith
  • 18,164
  • 32
  • 127
  • 177