Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 363
user2003024 Avatar asked Jan 23 '13 08:01

user2003024


People also ask

How recover deleted files before commit?

If you have deleted the file and already committed the changes, you need to use the ` git checkout` command to restore the file. First, you need to find out the checksum of the commit that deleted the file, and then check out the file from the previous commit.

How do I undo a git delete?

You can restore a deleted file from a Git repository using the git checkout command. If you do not know when a file was last deleted, you can use git rev-list to find the checksum of the commit in which that file was deleted. Then, you can check out that commit.

What to do when git deleted files?

The files listed under "changes not staged for commit" can be recovered by discarding your changes since last commit. Although you can later recover those files by reverting back to a commit when these files existed.


1 Answers

(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.

like image 174
dyng Avatar answered Sep 28 '22 12:09

dyng