175

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?

Just Shadow
  • 10,860
  • 6
  • 57
  • 75
Eric
  • 95,302
  • 53
  • 242
  • 374
  • 2
    I don't know for sure, but I don't think it's possible with git. However an undelete utility for your filesystem might do the trick. – static_rtti Jun 07 '11 at 15:02
  • 15
    Interestingly, [Git Community Book](http://schacon.github.io/gitbook/5_creating_new_empty_branches.html) also suggests `git clean -fdx` without any warning that operation is so dangerous. I wonder how many people irrevocably destroyed their files with this seemingly innocent suggestion. – catpnosis Feb 09 '16 at 05:15
  • 8
    Be sure to check if your IDE has a "Local History" like Eclipse or IntelliJ IDEs do. That can save you a lot of heartache over inadvertent `git clean` calls. Also, try to get out of the habit of cleaning without checking. Create a Powershell or Bash script that can be called that will run `git clean -fdxn` with a confirm prompt before you actually run the _incredibly_ destructive clean. – Josh Burgess Mar 20 '17 at 16:07

18 Answers18

167

No. Those files are gone.

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

Cristian Ciupitu
  • 20,270
  • 7
  • 50
  • 76
Mat
  • 202,337
  • 40
  • 393
  • 406
  • 53
    If you are using an advanced IDE like RubyMine, Eclipse etc, Chances are it would have a local history of deleted files and you might be able to recover them. – Usman Oct 16 '15 at 23:52
  • 1
    @Usman in my case `git clean` had accidentally deleted shelved changes inside the `.idea` hidden folder and Local History saved me! – emkman Mar 09 '16 at 03:47
  • 5
    Great tip by @Usman. With Eclipse, I could get my file back with the "Restore from Local History..." option: http://i.imgur.com/XWNLOk5.gifv – brandizzi Jun 10 '16 at 13:28
  • 1
    Actually this is POSSIBLE but with hacky method. See this answer: https://stackoverflow.com/questions/6267180/can-i-undo-a-git-clean-fdx/42861993#42861993 – Just Shadow Jun 28 '17 at 12:16
  • 11
    In PyCharm you also can restore them. Right click in the folder that had them, click in Local history, find the deletion and click revert. – Juanjo Conti Oct 03 '17 at 16:16
  • On macOS, Time Machine can help you – gmjonker Jul 23 '18 at 13:21
  • oh @#$% #$% ^&*(# – Urasquirrel Aug 22 '19 at 21:30
  • I thought I was safe doing a `git stash` prior to git clean -fxd. Apparently not! My untracked files were still deleted. A warning to anybody else using this dangerous AF command, if you're not paying attention when using it you really do risk severe data loss. – Jacob Hatwell Sep 24 '20 at 11:11
  • even VS Code has “Local History: Find Entry to Restore“ – valbaca Sep 07 '22 at 17:12
69
git clean -nfdx

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)

Peter Ajtai
  • 56,972
  • 13
  • 121
  • 140
55

IntelliJ/Android Studio allows restoring files from local history.

Y2i
  • 3,748
  • 2
  • 28
  • 32
23

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.

kusma
  • 6,516
  • 2
  • 22
  • 26
  • The real problem was the directory was not supposed to be a repository as well. Looking back at the log, turns out I forgot to cd to the new repo directory after `mkdir`-ing it – Eric Jun 07 '11 at 15:07
  • 2
    Shortly after reading this question, I wrote a patch to Git that adds a configuration variable to back-up each deleted file. Perhaps it could be useful for others? https://github.com/kusma/git/tree/work/clean-backup – kusma Feb 19 '13 at 15:37
  • 3
    I was lucky today!!! Thanks to "git fsck --lost-found". I used the following to get ALL the changes: git fsck | awk '{print $3}' | xargs git show | tee searchresults.log As I wanted the entire file that I had added and then removed, I could get it from the log. :) – Marcello DeSales Nov 09 '13 at 15:53
22

VS Code also keeps a history you can restore from.

Execute “Local History: Find Entry to Restore” (can cmd+shift+p and search “Restore”)

Enter your filename and pick which version you want.

valbaca
  • 625
  • 6
  • 12
21

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.

Rajesh Paul
  • 6,793
  • 6
  • 40
  • 57
17

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.

Marcello DeSales
  • 21,361
  • 14
  • 77
  • 80
16

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.

Just Shadow
  • 10,860
  • 6
  • 57
  • 75
  • 1
    Thanks you saved my life. I don't know if it the correct answer beacuse I could not recover everything but I was lucky as I got the most critical files I needed. – XzaR Nov 09 '17 at 14:27
14

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.

Sergey Emeliyanov
  • 5,158
  • 6
  • 29
  • 52
10

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.

dougallj
  • 409
  • 2
  • 4
8

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

Kumar
  • 1,536
  • 2
  • 23
  • 33
5

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

Nullable
  • 761
  • 5
  • 17
2

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

Vega
  • 27,856
  • 27
  • 95
  • 103
motbus
  • 21
  • 1
  • Wow! though this didnt recover the file I wanted, it is something I will make a note of - will definitely come in handy! – Ani Aug 08 '20 at 19:22
1

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/

Pawel
  • 31,342
  • 4
  • 73
  • 104
1

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

Alexander Paramonov
  • 1,441
  • 14
  • 24
0

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

Mbotet
  • 170
  • 2
  • 17
0

In usual case, Git cannot restore those files. The command clean removes only those files that are not tracked. Git keeps no information about untracked files. You'll only hope is to try some non-Git-related tool for restoring files.

However, in rare cases it may happen that you've stashed these files before they were cleaned. Even if you've dropped this stash in the meantime, it may still exists in the repository database. Git usually waits some time before GCing old revisions. In such case you may try to recover the stash: How do I recover a dropped stash in Git?

That's all the methods I could think of :-/

Piotr Siupa
  • 3,929
  • 2
  • 29
  • 65
-3

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

Matt Cashatt
  • 23,490
  • 28
  • 78
  • 111