192

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?

3
  • 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. Commented Jun 7, 2011 at 15:02
  • 16
    Interestingly, Git Community Book 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
    Commented Feb 9, 2016 at 5:15
  • 9
    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. Commented Mar 20, 2017 at 16:07

18 Answers 18

174

No. Those files are gone.

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

12
  • 56
    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
    Commented Oct 16, 2015 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
    Commented Mar 9, 2016 at 3:47
  • 5
    Great tip by @Usman. With Eclipse, I could get my file back with the "Restore from Local History..." option: i.imgur.com/XWNLOk5.gifv
    – brandizzi
    Commented Jun 10, 2016 at 13:28
  • 1
    Actually this is POSSIBLE but with hacky method. See this answer: stackoverflow.com/questions/6267180/can-i-undo-a-git-clean-fdx/… Commented Jun 28, 2017 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. Commented Oct 3, 2017 at 16:16
72
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)

1
  • 10
    To add to this, -n works on most git commands (and many other *nix commands). Commented Nov 9, 2013 at 20:57
61

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.

2
  • 5
    Ran the wrong command by mistake, and you've just saved me soooo much time 🙏. Commented Mar 13, 2023 at 12:39
  • 1
    You're the real hero Commented Nov 22, 2023 at 14:56
59

IntelliJ/Android Studio allows restoring files from local history.

3
  • 9
    PyCharm also supports this! Thank you for this answer. I'm so glad I read the rest of the answers after the top 3 were "no sorry you're out of luck".
    – Bryson
    Commented Mar 22, 2019 at 18:18
  • 6
    JetBrains (and you) saved my day!
    – ruohola
    Commented Nov 30, 2020 at 12:37
  • 1
    VSCode autosaves too. My bacon is saved!! stackoverflow.com/a/71657994/4898830 Commented Nov 19, 2022 at 20:10
25

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.

3
  • 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
    Commented Jun 7, 2011 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? github.com/kusma/git/tree/work/clean-backup
    – kusma
    Commented Feb 19, 2013 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. :) Commented Nov 9, 2013 at 15:53
25

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.

5
  • 2
    Create the file in same location with same file name. In IDE right click and click on "Show history". Copy paste the content from it. Excellent advice @Rajesh. Thanks.
    – deep
    Commented Feb 19, 2019 at 20:52
  • 1
    I'm saved. Thank you.
    – Scott
    Commented Jun 5, 2019 at 3:37
  • No need to copy the file contents, just right click the file, and click 'revert' to restore it back to that version (even if you deleted it)
    – n00b
    Commented Jun 21, 2019 at 18:52
  • This should be the best answer!
    – Louis L
    Commented Jan 25, 2021 at 5:41
  • The one in the answer should be the best answer. Saved me
    – Emjey
    Commented Feb 16, 2023 at 8:32
20

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.

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

1
  • 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
    Commented Nov 9, 2017 at 14:27
15

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.

1
  • 2
    This saved me! Thanks!! Commented Jun 3, 2021 at 1:39
11

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.

1
  • Along these lines, ext4magic is also very helpful.
    – Sagar
    Commented May 29, 2022 at 19:20
8

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

4
  • 2
    im using PyCharm and the local history saved me
    – psychok7
    Commented Jan 12, 2015 at 16:52
  • 1
    Same thing in IntelliJ IDEA!
    – arthurakay
    Commented May 28, 2015 at 19:45
  • RubyMine also has local history. saved me!
    – Usman
    Commented Oct 16, 2015 at 23:50
  • All IntelliJ browsers have a local history. Thankfully. I was using WebStorm and just about had an aneurysm before I found that. 3 days worth of 12 hour work days gone, and I seriously considered downing a bottle of hard liquor at my desk. Commented Mar 20, 2017 at 16:03
5

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

2
  • 1
    You saved my life... I didn't thought about it until i read your comment. Commented Dec 18, 2020 at 2:25
  • @WilliamRomero Some good often on the side, I’m glad to remind you.
    – Nullable
    Commented Dec 21, 2020 at 1:34
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

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
    Commented Aug 8, 2020 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/

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

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

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 :-/

-3

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

1
  • 9
    What do you mean? git clean does not move files to the recycle bin.
    – MEMark
    Commented Sep 14, 2018 at 7:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.