我可以恢复已删除的文件(撤消`git clean -fdx`)吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6267180/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Can I restore deleted files (undo a `git clean -fdx`)?
提问by Eric
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?
我正在按照制作 github pages的说明进行操作,但忘记向下移动到我的 git 子目录。结果,我只是用 .nud 扩展了整个文档目录git clean -fdx
。
有什么办法可以消除这个可怕的错误吗?
回答by Mat
No. Those files are gone.
不,那些文件不见了。
(Just checked on Linux: git clean calls unlink()
, and does not backup up anything beforehand.)
(刚刚在 Linux 上检查过:git clean callsunlink()
,并且没有事先备份任何东西。)
回答by Peter Ajtai
回答by Y2i
IntelliJ/Android Studio allows restoring files from local history.
IntelliJ/Android Studio 允许从本地历史记录恢复文件。
回答by kusma
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.
不。“git clean -fdx”将删除 git 未从您的工作目录中跟踪的所有文件和目录。因为 Git 不跟踪这些文件,所以它不会有这些文件的任何备份。至少通常不会。
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.
如果您最近对这些文件之一执行了 'git add'(但中止了提交),则您有机会使用 'git fsck --lost-found' 找到它。值得一试,但不要抱太大希望。
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.
将来,您应该考虑多提交几次而不是很少提交。这样你至少会有一个本地备份,即使你最终没有将这些提交推送到远程。
回答by Marcello de Sales
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:
正如@kusma 上面提到的,如果你很幸运(如果你曾经做过“git add”),那么你可以使用以下内容来提取整个对象:
git fsck | awk '{print }' | 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.
这样,它将查找所有类型的块,收集整个差异并添加到一个文件中,您可以提取丢失的文件。就我而言,我丢失了整个 Java 类。
回答by Abhinav
If you are working on Eclipse, one of the possible solution is to restore from local history of Eclipse.
如果您正在使用 Eclipse,可能的解决方案之一是从 Eclipse 的本地历史记录中恢复。
回答by dougallj
I had this problem today.
我今天遇到了这个问题。
As others have said, git doesn't keep the files.
正如其他人所说,git 不保留文件。
The only way to undo this is with an undelete utility. I used "extundelete" and recovered everything, but your mileage/filesystem may vary.
撤消此操作的唯一方法是使用取消删除实用程序。我使用了“extundelete”并恢复了所有内容,但您的里程/文件系统可能会有所不同。
回答by Just Shadow
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.
其实是的!这个有可能!但不使用 git 命令。
我们所需要的只是一个像Recuva这样的文件恢复工具。
只需提及删除它的路径,然后让它恢复文件。
Also here are some useful notes for using that app:
这里还有一些使用该应用程序的有用注意事项:
- I'd highly recommend you to check this checkbox before restoring
files (when deleted files are appeared in recovery window):
Advanced Mode
->Options
->Actions
->Restore folder structure
With this all your files will be recovered by keeping folder structure so then it'll be much easier to just drag and drop that folder to path from where it was deleted. - Install the recovery tool on USB device is much preferable (because of the warning below)
- While mentioning where to restore the files, avoidchoosing the same drive from where the files where deleted (again because of below-mentioned warning), as currently recovered file might overwrite the other deleted file which is not recovered yet. Recovering files to any USB drive is always a good solution there.
- 我强烈建议您在恢复文件之前选中此复选框(当已删除的文件出现在恢复窗口中时):
Advanced Mode
->Options
->Actions
->Restore folder structure
有了这个,您的所有文件都将通过保持文件夹结构来恢复,这样会更容易只需将该文件夹拖放到删除它的路径即可。 - 在 USB 设备上安装恢复工具更可取(因为下面的警告)
- 在提到恢复文件的位置时,请避免从删除文件的位置选择相同的驱动器(再次因为下面提到的警告),因为当前恢复的文件可能会覆盖其他尚未恢复的已删除文件。将文件恢复到任何 USB 驱动器始终是一个很好的解决方案。
Warning!As soon as you lose (accidentally delete) any file, try not to touch (add/edit files) the drive from where it was deleted to increase possibility of successful recovery. Otherwise you might completely lose that file.
警告!一旦丢失(意外删除)任何文件,请尽量不要触摸(添加/编辑文件)删除文件的驱动器,以增加成功恢复的可能性。否则,您可能会完全丢失该文件。
Why it works:When you delete a file, actually it's not 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.
工作原理:当你删除一个文件时,实际上它并没有被删除。它只是标记为“已删除”,因此下次当您将文件添加/更新到同一驱动器时,这些旧文件将被新文件覆盖。但是在它们被覆盖之前,仍然可以读取(当然也可以恢复)这些文件。
回答by Rajesh Paul
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.
如果您使用任何 Jetbrains IDE,则可以选择查看文件的本地历史记录。如果您已经完成git clean
,您可以重新创建文件并检查文件的本地历史记录并从那里恢复它。
Worked for me for a single file. For a complete directory I don't have any idea how to do that.
对我来说有效了一个文件。对于完整的目录,我不知道该怎么做。
回答by motbus
I did with a code I was supposed to add to my repository and I cleaned with -dfx I could restore the files.
我使用了应该添加到我的存储库中的代码,并使用 -dfx 进行了清理,我可以恢复文件。
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.
我尝试使用 debugfs,从 inode、testdisk 和许多其他出现的工具中查看和链接,但没有人找到这个愚蠢的写作意外删除的目录。
Download extundeletefrom sourceforge.
You probably will need to install e2fslibs-dev package
Run
从 sourceforge下载extundelete。
您可能需要安装e2fslibs-dev 包
运行
$ ./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
它将为 src 文件夹内的 extundelete 生成二进制文件在我的情况下,我错误地删除了一个文件夹。让我们称之为 FOOBAR
I just ran:
我刚跑:
$ sudo ./extundelete --restore-directory <full path to FOOBAR > <the partition>
and it created a folder named RESTORED_FOLDERS
并创建了一个名为 RESTORED_FOLDERS 的文件夹