开发者

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

开发者 https://www.devze.com 2023-03-12 01:28 出处:网络
I was following the instructions on making github pages, and forgot to move down into my git sub directory. As a result, I just nuke开发者_运维知识库d an entire directory of documents with git clean -

I was following the instructions on making github pages, and forgot to move down into my git sub directory. As a result, I just nuke开发者_运维知识库d an entire directory of documents with git clean -fdx.

Is there any way I can undo this terrible mistake?


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)


IntelliJ/Android Studio allows restoring files from local history.


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.


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.


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:

  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.


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.


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.


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.


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


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


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


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号