Can I restore deleted files (undo a `git clean -fdx`)?

爱⌒轻易说出口 提交于 2019-11-26 16:24:25
Mat

No. Those files are gone.

(Just checked on Linux: git clean calls unlink(), and does not backup up anything beforehand.)

git clean -fdxn

Will do a dry run, and show you what files would be deleted if you ran

git clean -fdx

(of course this is only helpful if you know this ahead of time, but for next time)

No. "git clean -fdx" will delete all files and directories that git does not track from your working-directory. Because Git does not track these files, it won't have any backups of these files. At least not usually.

If you have done a 'git add' on one of these files relatively recently (but aborted the commit), there is a chance you can find it with 'git fsck --lost-found'. It's worth a try, but don't get your hopes up too much.

In the future you should consider rather committing a few times too often than a few times too seldom. That way you'll at least have a local backup, even if you don't end up pushing these commits to a remote.

IntelliJ/Android Studio allows restoring files from local history.

As @kusma mentioned above, if you are lucky (if you ever did "git add"), then you can use the following to extract the entire object:

git fsck | awk '{print $3}' | xargs git show | tee searchresults.log

That way, it will look for all the types of chunks, collect the entire diffs and add to a file that you can extract the lost file. In my case, I had lost an entire Java class.

If you are working on Eclipse, one of the possible solution is to restore from local history of Eclipse.

I had this problem today.

As others have said, git doesn't keep the files.

The only way to undo this is with an undelete utility. I used "extundelete" and recovered everything, but your mileage/filesystem may vary.

Actually Yes! This is possible! But without using git commands.
All we need is just a file recovery tool like Recuva.
Just mention the path from where it was deleted, and let it recover the files.

Also here are some useful notes for using that app:

  1. I'd highly recommend you to check this checkbox before restoring files (when deleted files are appeared in recovery window):
    Advanced Mode->Options->Actions->Restore folder structure
    With this all your files will be recovered by keeping folder structure so then it'll be much easier to just drag and drop that folder to path from where it was deleted.
  2. Install the recovery tool on USB device is much preferable (because of the warning below)
  3. While mentioning where to restore the files, avoid choosing the same drive from where the files where deleted (again because of below-mentioned warning), as currently recovered file might overwrite the other deleted file which is not recovered yet. Recovering files to any USB drive is always a good solution there.

Warning! As soon as you lose (accidentally delete) any file, try not to touch (add/edit files) the drive from where it was deleted to increase possibility of successful recovery. Otherwise you might completely lose that file.

Why it works: When you delete a file, actually it's not deleted. It's just marked as "deleted" so next time when you add/update files to the same drive, those old files are overwritten with the new ones. But until they are overwritten it's still possible to read (and of-course recover) the files.

If you are using any Jetbrains IDE there's an option to see local history of a file. In case you have done git clean, you can recreate the file and check the local history of the file and restore it from there.

Worked for me for a single file. For a complete directory I don't have any idea how to do that.

If you are using git with MSBuild I created a target that copies all the files and then does git clean -xdf. This way you can easily restore the file if you realize that you deleted something you did not want to delete. Take a look here: http://blog.3d-logic.com/2012/11/04/safe-git-clean-with-msbuild/

Fortunately, I made this mistake while using Windows 7. Went to recycle bin, highlighted all deleted files and clicked "Restore". Done.

motbus

I did with a code I was supposed to add to my repository and I cleaned with -dfx I could restore the files.

I tried to use debugfs, look and link from inodes, testdisk, and many other tools that appears and none have found the directory this stupid one writing has erased by accident.

Download extundelete from sourceforge.
You probably will need to install e2fslibs-dev package
Run

$ ./configure 
$ make 

It will generate the binary for extundelete inside src folder In my case, I have wrongly erased a folder. let's call it FOOBAR

I just ran:

$  sudo ./extundelete --restore-directory <full path to FOOBAR > <the partition> 


and it created a folder named RESTORED_FOLDERS

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!