993

I deleted some files.

I did NOT commit yet.

I want to reset my workspace to recover the files.

I did a git checkout ..

But the deleted files are still missing.

And git status shows:

# On branch master
# Changes to be committed:
#   (use "git reset HEAD <file>..." to unstage)
#
#   deleted:    cc.properties
#   deleted:    store/README
#   deleted:    store/cc.properties
#

Why doesn't git checkout . reset the workspace to HEAD?

LostMyGlasses
  • 3,074
  • 20
  • 28
Homan
  • 25,618
  • 22
  • 70
  • 107
  • 31
    if you hadn't staged your changes after the delete, `git checkout .` would have worked fine. – faizal Nov 23 '14 at 10:55
  • 13
    @faizal and you will lose your changes if you do that. – Vasiliy Yorkin Dec 15 '15 at 10:56
  • 2
    Just press Ctrl-J in git gui on the deleted item. – ajeh Sep 27 '17 at 15:34
  • git checkout -- cc.properties store/README store/cc.properties – Vinod Pasi Feb 22 '20 at 01:02
  • See this answer: https://www.quora.com/How-can-I-recover-a-file-I-deleted-in-my-local-repo-from-the-remote-repo-in-Git – live-love Apr 17 '20 at 15:49
  • @VasiliyYorkin You can commit the changes first before doing `git checkout .`. Then you won't loose them. – pogosama Jul 01 '21 at 07:51
  • DANGER DANGER Will Robinson - if you do "git checkout ." as I did after 2 hard days of work in tired attempt to get back a locally deleted file, you will lose ALL locally modified tracked files. That happened to me. There is no recovery via git. If you are lucky you might have some other versioning system or your application backup file. – tobi delbruck Oct 28 '22 at 13:05
  • `git checkout .` restored files deleted by `rm`. But in any case: 1. You can make a copy of entire directory to prevent losing any data. 2. You can make dummy repo in a new directory and check how commands actually behave if different scenarios. Something like `mkdir 42 && cd 42 && git init && echo 42 > 42 && git add 42 && rm 42` – quant2016 Apr 27 '23 at 10:38
  • Check the Recycle Bin in your system. Pretty sure the files are there. – carloswm85 Apr 28 '23 at 23:17
  • How about git restore filepath where filepath is the path of the file you want to recover – Reza May 09 '23 at 11:47
  • With the current version of git, the `git restore *` does the trick in one shot. `git status` also hints about that. – Michal Jul 30 '23 at 18:23

29 Answers29

889

The output tells you what you need to do. git reset HEAD cc.properties etc.

This will unstage the rm operation. After that, running a git status again will tell you that you need to do a git checkout -- cc.properties to get the file back.

Update: I have this in my config file

$ git config alias.unstage
reset HEAD

which I usually use to unstage stuff.

Alessandro Jacopson
  • 18,047
  • 15
  • 98
  • 153
Noufal Ibrahim
  • 71,383
  • 13
  • 135
  • 169
  • 6
    How do you do this for multiple deleted files? Running git reset HEAD <> multiple times would be cumbersome, any efficient way to get it done? – SubSul May 10 '16 at 06:14
  • 96
    `git reset HEAD \*` and then `git checkout -- .` – Noufal Ibrahim May 10 '16 at 07:51
  • 4
    but I have modified files. – Jiang YD Dec 06 '16 at 03:22
  • @RauliRajande It's probable that your situation is different from the one described in the original question. – Noufal Ibrahim Feb 15 '18 at 05:18
  • 2
    `rm -r ./engines` - oops. Now `git reset engines; git checkout engines`. – Kris Jul 26 '18 at 13:26
  • @Kris is `./engines` a repo or a directory within a repo, because a directory doesn't seem to work like that with git version 2.21.0? `git checkout -- dirname` => `error: pathspec 'dirname' did not match any file(s) known to git`. (Same thing without the `--` argument divider.) [Replace dirname with a real directory, obviously.] – simpleuser Oct 24 '19 at 21:27
  • @simpleuser It would have been a directory within a repo. – Kris Oct 28 '19 at 17:52
  • This can be set using: `git config --global alias.unstage "reset HEAD"` – alper Jan 01 '21 at 15:36
  • Thanks for the comment, what does `--` in `git checkout -- cc.properties` do? – zyy Feb 01 '21 at 21:37
  • 2
    @zyy The `--` is to indicate files. e.g. If you have a branch called `foo`. `git co foo` will checkout to the branch. However, `git co -- foo` will checkout the file named `foo`. – Noufal Ibrahim Feb 02 '21 at 04:36
  • Again this operation can wipe out all your locally deleted files if you give the path "." – tobi delbruck Oct 28 '22 at 13:06
250

You've staged the deletion so you need to do:

git checkout HEAD cc.properties store/README store/cc.properties

git checkout . only checks out from the index where the deletion has already been staged.

CB Bailey
  • 755,051
  • 104
  • 632
  • 656
230

Just do git checkout path/to/file-I-want-to-bring-back.txt

seddonym
  • 16,304
  • 6
  • 66
  • 71
209

To recover all unstaged deletions at once, automatically, without specifying each single path:

git ls-files -z -d | xargs -0 git checkout --

To recover all staged deletions at once, automatically, without specifying each single path:

git status | grep 'deleted:' | awk '{print $2}' | xargs git checkout --
PaoloC
  • 3,817
  • 1
  • 23
  • 27
  • 13
    I accidentally deleted over 500 files and this worked a treat because it also kept all of my valid changes (the first line is what I used). Thanks. – Guy Lowe Jun 15 '15 at 06:59
  • 1
    Accidentally deleted all contents of a repo right after a successful build. The first command saved my bacon. – MonaLisaOverdrive Jun 27 '15 at 21:49
  • saved me some time but for some reason it did not recover some files, will use it again for sure – M_K Jul 01 '16 at 09:54
  • 2
    Before this would work for me, I had to run `git status --long | grep 'deleted:' | awk '{print $2}' | xargs git reset HEAD --`. – Ian Dunn Jan 20 '17 at 23:48
  • 1
    Very useful, wanted to keep untracked files but get rid of deleted and modified, just changed -d to -m for handling the modified. – RaisinBranCrunch Aug 18 '17 at 20:46
  • 6
    Note this doesn't work if you have spaces in your file names/paths. I think ``git ls-files -d | sed -e "s/\(.*\)/'\1'/" | xargs git checkout --`` will work. – parsley72 Nov 01 '17 at 21:14
  • 1
    Both ls-files and xargs support 0-termination, so just use `git ls-files -z -d | xargs -0 git checkout --` - no need to get sed involved, and no problems with spaces in file names. – RichardC Mar 05 '20 at 18:56
  • 1
    I wasn't able to use `git checkout` because I messed up something, I ended up using `git status --untracked-files=no . | grep deleted: | sed -r 's/\s+deleted:\s+//' | sed -e "s/\(.*\)/'\1'/" | xargs git restore --staged`, not pretty but worked – CervEd Mar 06 '21 at 15:29
93

Since you're doing a git checkout ., it looks like you are trying to restore your branch back to the last commit state.

You can achieve this with a git reset HEAD --hard

Warning

Doing this may remove all your latest modifications and unstage your modifications, e.g., you can lose work. It may be what you want, but check out the docs to make sure.

Rap
  • 6,851
  • 3
  • 50
  • 88
Paul T
  • 1,455
  • 11
  • 15
  • 43
    Woww!! Careful with this!!!! You might be right, but someone could be confused and blow up their whole code. It'd be cool if you add a bigger warning. – santiagobasulto Jan 04 '13 at 00:18
  • 3
    This is exactly what I needed. Doesn't blow up your whole code - simply brings you back to your most recent commit. – Andrew Hendrie Feb 11 '15 at 04:22
  • 3
    I ended up with hundreds of missing files at one point. This is the only practical way to fix the problem. Thanks! – Jonathan Benn Aug 05 '15 at 14:56
88

if you used

git rm filename

to delete a file then

git checkout path/to/filename

doesn't work, so in that case

git checkout HEAD^ path/to/filename

should work

Aurangzeb
  • 1,537
  • 13
  • 9
33

Here are different cases as a reference to help others:

If the deletion has not been committed, the command below will restore the deleted file in the working tree.

$ git checkout -- <file>

You can get a list of all the deleted files in the working tree using the command below.

$ git ls-files --deleted

If the deletion has been committed, find the commit where it happened, then recover the file from this commit.

#find the commit hash where it had this file deleted
$ git rev-list -n 1 HEAD -- <file>

It should give you something like c46e81aa403ecb8a0f7a323a358068345, now use this commit hash with the parent operator (^) like so:

$ git checkout <commit>^ -- <file>

Example:

$ git checkout c46e81aa403ecb8a0f7a323a358068345^ -- <file> 

In case you are looking for the path of the file to recover, the following command will display a summary of all deleted files.

$ git log --diff-filter=D --summary

If you want to just display the list of files:

git log --diff-filter=D --summary | grep "delete mode"
Abdull
  • 26,371
  • 26
  • 130
  • 172
Muhammad Soliman
  • 21,644
  • 6
  • 109
  • 75
  • I had a case where `git rev-list -n 1 HEAD -- ` didn't work, maybe because `` only ever existed on a feature branch. Adding `--all` helped: `git rev-list --all -n 1 HEAD -- ` – Abdull Oct 19 '22 at 10:46
31

Here is the command that helped me on my mac. I tried a few of the other solutions but they did not work for me.

Git version on OSX Mavericks

mac-pro:main chris$ git version
git version 1.8.5.2 (Apple Git-48)

Command

git checkout HEAD -- path/to/file/file.cc
Chris Hinshaw
  • 6,967
  • 2
  • 39
  • 65
27

If you want to restore all of the files at once

Remember to use the period because it tells git to grab all of the files.

This command will reset the head and unstage all of the changes:

$ git reset HEAD . 

Then run this to restore all of the files:

$ git checkout .

Then doing a git status, you'll get:

$ git status
On branch master
Your branch is up-to-date with 'origin/master'.
Casper Wilkes
  • 899
  • 11
  • 14
  • 3
    This is kinda the simplest solution and works for bunch of files (lets say you have deleted multiple files / folders). good job dude+ – Gkiokan Sep 14 '17 at 08:04
19
git checkout HEAD -- client/src/pp_web/index.cljs
Dustin Getz
  • 21,282
  • 15
  • 82
  • 131
19

Use git ls-files to checkout deleted(-d) or modified(-m) files.

git checkout $(git ls-files -d)

see How can I restore only the modified files on a git checkout?

rickfoosusa
  • 1,061
  • 19
  • 26
8

Do you can want see this

that goes for cases where you used

git checkout -- .

before you commit something.

You may also want to get rid of created files that have not yet been created. And you do not want them. With :

git reset -- .
Community
  • 1
  • 1
  • You did not report fully the answer you copied. In fact `git checkout -- .` does not help to recover deleted files and is equivalent to what the asker tried: `git checkout .`. The part which can work is the one you didn't copy: `git checkout `. – Jean Paul Dec 15 '18 at 11:00
6

If you have not committed any changes all you have to do is stash those changes and you will be back to the last working commit.

git stash
git stash clear
git clean 
Rick
  • 12,606
  • 2
  • 43
  • 41
6

Found this post while looking for answers on how to un-delete a file that was deleted in my working directory after a merge from another's branch. No commit was yet made after the merge. Since it was a merge in progress, i could not just add it back using:

$ git reset <commitid#-where-file.cpp-existed> file.cpp

I had to do another step in addition to the reset to bring the file back:

$ git checkout -- file.cpp
user2453404
  • 63
  • 1
  • 6
4

if you are looking for a deleted directory.

 git checkout ./pathToDir/*
PPB
  • 2,937
  • 3
  • 17
  • 12
4

Newer git (mine is 2.27.0) is more friendly and the actual commands are shown during "git status". For example, if you deleted the file tf.c, then

$ git status
...
Changes not staged for commit:
  (use "git add/rm <file>..." to update what will be committed)
  (use "git restore <file>..." to discard changes in working directory)
      deleted:    tf.c

You would use "git restore tf.c" to get it back, as it saz. No more search!

4

This was the easiest way for me:

git checkout HEAD~1 path/to/myfile.rb

I found it here.


Another way that also worked for me:

git reset HEAD path/to/myfile.rb
git restore path/to/myfile.rb
stevec
  • 41,291
  • 27
  • 223
  • 311
2

For me what worked was git checkout {SHA1 of commit with version to restore} "{path to file to restore}"

For example git checkout 5a6b3179e58edff9c90326b9a04284b02fd67bd0 "src-ui/views/includes/radar.pug"

(executed in the branch that we want the file to go into)

After that command executes, the restored file will exist in the original location (which will need to be comited)

Dinis Cruz
  • 4,161
  • 2
  • 31
  • 49
  • and / or simply a ```git checkout master path/to/the/file.bin``` so that you just undelete that file without loosing any other change you might have done. PS: this should be the accepted answer... – Edoardo Aug 19 '16 at 10:40
2

1.Find that particular commit to which you want to revert using:

   git log
This command will give you a list of commits done by you .

2.Revert to that commit using :

    git revert <commit id> 

Now you local branch would have all files in particular

Ashutosh Shukla
  • 557
  • 5
  • 9
2

I happened to move (instead of copy) some json files from one folder to another within the same repository. Then I renamed those files and changed some contents in the new location. However I quickly learned that I have not copied and totally deleted the files from previous location.

Easy Solution:

git reset HEAD <oldfilepath_which_was_moved>
git restore <oldfilepath_which_was_moved>

Did this for all the files and they are back.

You can also include multiple files separated by space.

git reset HEAD file_1_path file_2_path file_3_path

Easy fix, btw this will not change / delete the new files.

Karan Sharma
  • 2,353
  • 4
  • 28
  • 46
2

Situation: One deleted a file but didn’t commit

If one deleted a file, and immediately realized it was a mistake? This one is easy, just do:

git checkout HEAD <filename>

If it is a folder, just do:

git checkout HEAD <foldername>/

Reference: https://www.git-tower.com/learn/git/faq/restoring-deleted-files

Francis Bacon
  • 4,080
  • 1
  • 37
  • 48
1

If you have installed ToroiseGIT then just select "Revert..." menu item for parent folder popup-menu.

Vasyl Shyrochuk
  • 460
  • 3
  • 11
1

CAUTION: commit any work you wish to retain first.

You may reset your workspace (and recover the deleted files)

git checkout ./*
k0pernikus
  • 60,309
  • 67
  • 216
  • 347
Henrique Florencio
  • 3,440
  • 1
  • 18
  • 19
  • 2
    FYI...this command deleted all my working files and didn't recover the deleted file..beware – hendr1x Sep 13 '18 at 19:22
  • That is why you use this command to RESET your workspace. I thought that would be self explanatory. – Henrique Florencio Sep 21 '18 at 19:21
  • 1
    That command does not work because if the file is deleted, it will not be caught by `./*`. – Jean Paul Dec 15 '18 at 10:37
  • @JeanPaul maybe I'm misunderstanding but it put my workspace in the original state (deleted file now present). – Marc Feb 01 '19 at 10:57
  • @Marc It could work but only if there is no visible file in the directory, because otherwise `./*` will be expanded by bash to match those files before being sent to git. – Jean Paul Feb 02 '19 at 08:36
  • @Marc It could also work if the deleted files are in subdirectories which them are not deleted, but clearly, that command is not the most straightforward. – Jean Paul Feb 02 '19 at 08:46
1

For anyone who get unknown revision or path not in the working tree when running git checkout

run git reset fileToRestore

then git checkout fileToRestore

Your file will be restored

adwardwo1f
  • 817
  • 6
  • 18
1

You can use git-restore to get the files back in one go.

If you have not committed your deletions like this:

➜  learning git:(daily) ✗ git status
Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
    deleted:    feelings/articles/2022/1/assets/20220109_093659_image.png
    deleted:    feelings/articles/2022/1/assets/20220109_094525_image.png
    deleted:    feelings/articles/2022/1/assets/20220109_100231_image.png
    deleted:    feelings/articles/2022/1/assets/20220109_100251_image.png
    deleted:    feelings/articles/2022/1/assets/20220109_100321_image.png
    deleted:    feelings/articles/2022/1/assets/20220109_101009_image.png

you can use something like this to recover your files:

git restore --worktree --staged feelings/articles/2022/1/assets

-W, --worktree, -S, --staged
Specify the restore location. If neither option is specified, by default the working tree is restored. Specifying --staged will only restore the index. Specifying both restores both.

hustnzj
  • 525
  • 6
  • 13
0

I had the same problem however none of the above solutions worked for me. What I ended up doing was:
- create an empty file with the same name
- compare this file with its local history
- copy history across to empty file.

theEUG
  • 399
  • 5
  • 18
0

One solution without any risks is to go to your repository page (on github etc.) and download the deleted file by hand.

alercelik
  • 615
  • 7
  • 11
0

git reset HEAD --hard command will merge pull request from the originator branch ,so you will find the deleted file

Biddut
  • 418
  • 1
  • 6
  • 17
-1

I had the same problem and none of the answers here I tried worked for me either. I am using Intellij and I had checked out a new branch git checkout -b minimalExample to create a "minimal example" on the new branch of some issue by deleting a bunch of files and modifying a bunch of others in the project. Unfortunately, even though I didn't commit any of the changes on the new "minimal example" branch, when I checked out my "original" branch again all of the changes and deletions from the "minimal example" branch had happened in the "original" branch too (or so it appeared). According to git status the deleted files were just gone from both branches.

Fortunately, even though Intellij had warned me "deleting these files may not be fully recoverable", I was able to restore them (on the minimal example branch from which they had actually been deleted) by right-clicking on the project and selecting Local History > Show History (and then Restore on the most recent history item I wanted). After Intellij restored the files in the "minimal example" branch, I pushed the branch to origin. Then I switched back to my "original" local branch and ran git pull origin minimalExample to get them back in the "original" branch too.

geneSummons
  • 907
  • 5
  • 15