简体   繁体   English

我可以恢复已删除的文件(撤消 `git clean -fdx`)吗?

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

I was following the instructions on making github pages , and forgot to move down into my git sub directory.我按照有关制作 github 页面的说明进行操作,却忘记向下移动到我的 git 子目录中。 As a result, I just nuked an entire directory of documents with git clean -fdx .结果,我只是用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.) (刚刚检查了 Linux: git clean 调用unlink() ,并且没有事先备份任何内容。)

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 . IntelliJ/Android Studio 允许从本地历史恢复文件

No. "git clean -fdx" will delete all files and directories that git does not track from your working-directory.不会。“git clean -fdx”将删除 git 未从您的工作目录中跟踪的所有文件和目录。 Because Git does not track these files, it won't have any backups of these files.由于 Git 不跟踪这些文件,因此不会对这些文件进行任何备份。 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'.如果您最近对其中一个文件执行了“git add”(但中止了提交),则有可能使用“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.如果您使用的是任何 Jetbrains IDE,则可以选择查看文件的本地历史记录。 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.如果您已经完成git clean ,您可以重新创建文件并检查文件的本地历史记录并从那里恢复。

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:正如上面提到的@kusma,如果你很幸运(如果你曾经做过“git add”),那么你可以使用以下命令来提取整个 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.就我而言,我丢失了整个 Java class。

Actually Yes .其实的。 This is possible!这个有可能! But without using git commands.但不使用 git 命令。
All we need is just a file recovery tool like Recuva .我们所需要的只是一个像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 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)在 USB 设备上安装恢复工具更可取(因为下面的警告)
  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.将文件恢复到任何 USB 驱动器始终是一个很好的解决方案。

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.但在它们被覆盖之前,仍然可以读取(当然也可以恢复)文件。

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”并恢复了所有内容,但您的里程/文件系统可能会有所不同。

If you're using IntelliJ IDEA or Android Studio it can be easily done.如果您使用IntelliJ IDEAAndroid Studio ,则可以轻松完成。 Just create files with same names you deleted, and click on the VCS -> Local History -> Show History for that file.只需创建与您删除的名称相同的文件,然后单击该文件的VCS -> Local History -> Show History Unfortunately it doesn't work with media files such as JPEG or PNG images.不幸的是,它不适用于 JPEG 或 PNG 图像等媒体文件。 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.然而,这个选项多次救了我的命,所以谢天谢地,它就在那里。

If you are working on Eclipse, one of the possible solution is to restore from local history of Eclipse.如果您正在处理 Eclipse,则可能的解决方案之一是从 Eclipse 的本地历史记录中恢复。

If you are using a mac and backed up with a Time Machine , then you can restore如果您使用的是 Mac 并使用Time Machine备份,则可以恢复

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 extundelete from sourceforge.从 sourceforge 下载extundelete
You probably will need to install e2fslibs-dev package您可能需要安装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.它将在 src 文件夹中生成用于 extundelete 的二进制文件在我的情况下,我错误地删除了一个文件夹。 let's call it FOOBAR让我们称之为 FOOBAR

I just ran:我刚跑:

$  sudo ./extundelete --restore-directory <full path to FOOBAR > <the partition> 


and it created a folder named RESTORED_FOLDERS它创建了一个名为 RESTORED_FOLDERS 的文件夹

If you are using git with MSBuild I created a target that copies all the files and then does git clean -xdf .如果您将 git 与 MSBuild 一起使用,我创建了一个复制所有文件的目标,然后执行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/看看这里: 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.在 Linux Mint 上,您可能安装了Timeshift备份工具。 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 .在 MacOS 上,您可能会对Time Machine感到幸运。

If your laptop/PC is in a company fleet , there are chances that they have a backup as well.如果您的笔记本电脑/PC 在company fleet中,那么他们也有可能拥有备份。

If machine is in the cloud , most likely there is a backup or a hdd snapshot .如果机器在cloud中,很可能有备份或硬盘snapshot

In any case it is always a good idea to double check with git clean -n beforehand无论如何,最好事先仔细检查git clean -n

If you havent pushed your changes (git push) you can also bring back the information from your server which is not updated yet.如果您还没有推送您的更改(git push),您还可以从您的服务器中取回尚未更新的信息。 I used filezilla to bring back the files i deleted and copied those into my local files.我使用 filezilla 来恢复我删除的文件并将它们复制到我的本地文件中。 Its not the best way do it, but it works and avoids using console它不是最好的方法,但它可以工作并避免使用控制台

VS Code also keeps a history you can restore from. VS Code 还保留您可以从中恢复的历史记录。

Execute “Local History: Find Entry to Restore” (can cmd+shift+p and search “ Restore ”)执行“本地历史:查找要还原的条目” (可以cmd+shift+p搜索“还原”)

Enter your filename and pick which version you want.输入您的文件名并选择您想要的版本。

In usual case, Git cannot restore those files.通常情况下,Git 无法恢复这些文件。 The command clean removes only those files that are not tracked.命令clean仅删除那些未跟踪的文件。 Git keeps no information about untracked files. Git 不保留有关未跟踪文件的信息。 You'll only hope is to try some non-Git-related tool for restoring files.您只希望尝试一些与 Git 无关的工具来恢复文件。

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. Git 通常在 GCing 旧版本之前等待一段时间。 In such case you may try to recover the stash: How do I recover a dropped stash in Git?在这种情况下,您可以尝试恢复存储:如何恢复 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".幸运的是,我在使用 Windows 时犯了这个错误 7. 进入回收站,突出显示所有已删除的文件并单击“恢复”。 Done.完毕。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM