16

On my local machine I removed files from folder.

git init
git add --all

then I wrote (don't ask me, why! :) )

git rm -rf

I don't commit, yet. Now I have empty folders in my project. In .git folder has objects with 53 Mb of files.

How can I recover my files? I've tried programs like Drill Disc and Stellar, but not found my files. And I can't rollback from GIT.

How can I recover the lost files?

Cœur
  • 37,241
  • 25
  • 195
  • 267
user2003024
  • 191
  • 1
  • 8

2 Answers2

13

(update) Use git fsck instead, it is a builtin command for retrieving files you have once added to git repository.

git fsck --lost-found --unreachable

after the command processing, retrieved files will be placed at .git/lost-found/other, with file name of a sha256 hash. Although the original name is still lost, the content will be back.


You can find your files in your .git/objects directory.

Suppose there is a .git/objects/2f/ae996f8f8f9298b41ba4fdbac3d62014f9579e object, you can execute

git cat-file -p 2fae996

to get the content of your lost file.

But I'm sorry, I have no idea about reconstructing your directory or doing this automatically.

dyng
  • 2,854
  • 21
  • 24
  • 1
    +1: Wow, indeed. Strange that the files are there but are not accessible in any way using the normal git commands. – Daniel Hilgarth Jan 23 '13 at 08:57
  • Im not worry about directories, but I need files. "git cat-file" - this like "cat" command in *nix? How can I save file in other directory? – user2003024 Jan 23 '13 at 08:58
  • you can save it by redirection just like an usual unix command. `git cat-file object > /path/to/where/you/want/save/it`, but I think it is not a really good idea because there is no information about file names. – dyng Jan 23 '13 at 09:20
  • 1
    It possible to do it automaticly! Here is my shit-code on PHP )) http://codepad.org/khIjLFnI – user2003024 Jan 23 '13 at 12:05
  • @DanielHilgarth `git show 2fae996` can do it too (how shame I don't know it until today), `show` is a versatile upper interface for many lower command. – dyng Jan 24 '13 at 03:27
  • Thank you so much, I thought I'd lost all my files :) – Henry Aug 25 '21 at 20:44
-2

have you tried :

git reset --hard

?

Hope that works :)

Flavius
  • 13,566
  • 13
  • 80
  • 126
Udan
  • 5,429
  • 2
  • 28
  • 34
  • After a `git rm -rf`, you can even safely do a hard reset. – Flavius Jan 23 '13 at 08:15
  • 1
    -1: That would only work if `rm -rf` would have been used. See [my answer](http://stackoverflow.com/a/14475121/572644) for an explanation. – Daniel Hilgarth Jan 23 '13 at 08:17
  • 1
    This command changed the code to the last commit and remove all the modified files. – Bin Wang Jan 23 '13 at 08:20
  • 1
    You really should delete this answer. It doesn't help *at all*. – Daniel Hilgarth Jan 23 '13 at 08:59
  • I did almost the exact same thing as the person who asked the question. The only difference is that I was a lot further along on a project. This answer resolved the issue for me. I lost all of the work I'd done since my last commit, and I learned my lesson about trying to use shortcuts when I need to stage deleted files, but at least I didn't lose the whole project. Thanks! – Taylor714 Nov 30 '14 at 17:48