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?
18 Answers
No. Those files are gone.
(Just checked on Linux: git clean calls unlink()
, and does not backup up anything beforehand.)
-
56If 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.– UsmanCommented 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!– emkmanCommented Mar 9, 2016 at 3:47 -
5Great tip by @Usman. With Eclipse, I could get my file back with the "Restore from Local History..." option: i.imgur.com/XWNLOk5.gifv Commented Jun 10, 2016 at 13:28
-
1Actually 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
-
11In 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
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)
-
10To add to this,
-n
works on most git commands (and many other *nix commands). Commented Nov 9, 2013 at 20:57
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.
-
5Ran the wrong command by mistake, and you've just saved me soooo much time 🙏. Commented Mar 13, 2023 at 12:39
-
1
IntelliJ/Android Studio allows restoring files from local history.
-
9PyCharm 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".– BrysonCommented Mar 22, 2019 at 18:18
-
6
-
1VSCode autosaves too. My bacon is saved!! stackoverflow.com/a/71657994/4898830 Commented Nov 19, 2022 at 20:10
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.
-
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– EricCommented Jun 7, 2011 at 15:07 -
2Shortly 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– kusmaCommented Feb 19, 2013 at 15:37
-
3I 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
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.
-
2Create 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.– deepCommented Feb 19, 2019 at 20:52
-
1
-
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)– n00bCommented Jun 21, 2019 at 18:52
-
-
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.
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:
- 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. - Install the recovery tool on a USB device is much preferable (because of the warning below)
- 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.
-
1Thanks 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.– XzaRCommented Nov 9, 2017 at 14:27
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.
-
2
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.
If you are working on Eclipse, one of the possible solution is to restore from local history of Eclipse.
-
2
-
1
-
-
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
If you are using a mac and backed up with a Time Machine
, then you can restore
-
1You 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.– NullableCommented Dec 21, 2020 at 1:34
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
-
Wow! though this didnt recover the file I wanted, it is something I will make a note of - will definitely come in handy!– AniCommented Aug 8, 2020 at 19:22
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/
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
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
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 :-/
Fortunately, I made this mistake while using Windows 7. Went to recycle bin, highlighted all deleted files and clicked "Restore". Done.
-
9What do you mean?
git clean
does not move files to the recycle bin.– MEMarkCommented Sep 14, 2018 at 7:30
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.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 rungit clean -fdxn
with a confirm prompt before you actually run the incredibly destructive clean.