Recover staged but not committed files after 'git rm -rf'
Asked Answered
G

2

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?

Grani answered 23/1, 2013 at 8:7 Comment(2)
If you do not get them back by using any "undelete" tool, they are gone.Longsome
https://mcmap.net/q/22361/-recover-from-losing-uncommitted-changes-by-quot-git-reset-hard-quot/… this may be usefulGraeco
F
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.

Fist answered 23/1, 2013 at 8:38 Comment(6)
+1: Wow, indeed. Strange that the files are there but are not accessible in any way using the normal git commands.Hu
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?Grani
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.Fist
It possible to do it automaticly! Here is my shit-code on PHP )) codepad.org/khIjLFnIGrani
@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.Fist
Thank you so much, I thought I'd lost all my files :)Modred
P
-2

have you tried :

git reset --hard

?

Hope that works :)

Percolator answered 23/1, 2013 at 8:12 Comment(5)
After a git rm -rf, you can even safely do a hard reset.Sanity
-1: That would only work if rm -rf would have been used. See my answer for an explanation.Hu
This command changed the code to the last commit and remove all the modified files.Jehoshaphat
You really should delete this answer. It doesn't help at all.Hu
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!Roughdry

© 2022 - 2024 — McMap. All rights reserved.