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

GitGit Clean

Git Problem Overview


I was following the instructions on making github pages, and forgot to move down into my git sub directory. As a result, I just nuked an entire directory of documents with git clean -fdx.
Is there any way I can undo this terrible mistake?

Git Solutions


Solution 1 - Git

No. Those files are gone.

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

Solution 2 - Git

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)

Solution 3 - Git

IntelliJ/Android Studio allows restoring files from local history.

Solution 4 - Git

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.

Solution 5 - Git

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.

Solution 6 - Git

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.

Solution 7 - Git

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 the deleted files appear in the recovery window):
    Advanced Mode->Options->Actions->Restore folder structure
    With this, all your files will be recovered by keeping the folder structure, so then it'll be much easier to just drag and drop that folder to the path from where it was deleted.
  2. Install the recovery tool on a 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 were deleted (again because of the below-mentioned warning), as currently, the recovered file might overwrite the other deleted files which are not recovered yet. Recovering files to any USB drive is always a good solution there.

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


Why it works
When you delete a file, actually it's not really 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.

Solution 8 - Git

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.

Solution 9 - Git

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

Solution 10 - Git

If you're using IntelliJ IDEA or Android Studio it can be easily done. Just create files with same names you deleted, and click on the VCS -> Local History -> Show History for that file. Unfortunately it doesn't work with media files such as JPEG or PNG images. Also, at the moment you can recover your deleted files only one-by-one. However, this option saved my life many times, so thankfully it's there in place.

Solution 11 - Git

If you are using a mac and backed up with a Time Machine, then you can restore

Solution 12 - Git

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

Solution 13 - Git

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/

Solution 14 - Git

No, but maybe you have a backup and don't even know about it.

It depends on how long those files were laying around on your machine. If those files are older than a day, there are good changes your backup has it.

On a Linux Mint, you might have a Timeshift backup tool installed. And if you are lucky enough, your code folder is included in daily backups.

On a MacOS you might have a luck with the Time Machine.

If your laptop/PC is in a company fleet, there are chances that they have a backup as well.

If machine is in the cloud, most likely there is a backup or a hdd snapshot.

In any case it is always a good idea to double check with git clean -n beforehand

Solution 15 - Git

If you havent pushed your changes (git push) you can also bring back the information from your server which is not updated yet. I used filezilla to bring back the files i deleted and copied those into my local files. Its not the best way do it, but it works and avoids using console

Solution 16 - Git

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionEricView Question on Stackoverflow
Solution 1 - GitMatView Answer on Stackoverflow
Solution 2 - GitPeter AjtaiView Answer on Stackoverflow
Solution 3 - GitY2iView Answer on Stackoverflow
Solution 4 - GitkusmaView Answer on Stackoverflow
Solution 5 - GitMarcello de SalesView Answer on Stackoverflow
Solution 6 - GitRajesh PaulView Answer on Stackoverflow
Solution 7 - GitJust ShadowView Answer on Stackoverflow
Solution 8 - GitdougalljView Answer on Stackoverflow
Solution 9 - GitKumarView Answer on Stackoverflow
Solution 10 - GitSergei EmelianovView Answer on Stackoverflow
Solution 11 - GitNullableView Answer on Stackoverflow
Solution 12 - GitmotbusView Answer on Stackoverflow
Solution 13 - GitPawelView Answer on Stackoverflow
Solution 14 - GitAlexander ParamonovView Answer on Stackoverflow
Solution 15 - GitMbotetView Answer on Stackoverflow
Solution 16 - GitMatt CashattView Answer on Stackoverflow