简体   繁体   English

从 SourceTree 或 Git 恢复已删除的文件

[英]Restore Deleted Files from SourceTree or Git

I have recently started using SourceTree to work with Bitbucket.我最近开始使用 SourceTree 来处理 Bitbucket。 I was working on a project, and then I decided I needed to move it to a different folder (for various reasons).我正在处理一个项目,然后我决定需要将它移动到不同的文件夹(出于各种原因)。 So, I cut the files that I was working on in the repo, and then clicked the delete button in SourceTree, not realising that doing this would actually delete the files.所以,我剪切了我在 repo 中处理的文件,然后单击 SourceTree 中的删除按钮,没有意识到这样做实际上会删除文件。 I cut the files just in case, but didn't remember that pasting these files requires the original files to exist.我剪切了文件以防万一,但不记得粘贴这些文件需要原始文件存在。

Now, I only have the files left from repo that SourceTree did not delete.. these are the files in the objects directory, and I do not have the info or pack folders that are inside the objects directory.现在,我只有从回购该SourceTree没有删除留下的文件。这些都是在文件objects目录,我没有infopack其内部的文件夹中objects的目录。

Is there any way that I can restore the files?有什么办法可以恢复文件吗? I have taken a look at this question and this question , however I am new to git and cannot figure out how to actually do these.我已经看过这个问题这个问题,但是我是 git 新手,无法弄清楚如何实际执行这些操作。

Please help!请帮忙! How can I restore the files in the repo?如何恢复 repo 中的文件?

If that files were committed you can just make git checkout HEAD -f .如果提交了这些文件,您只需执行git checkout HEAD -f
If files were not committed they are not stored as objects.如果文件未提交,则它们不会存储为对象。

Here is a workaround in Sourcetree that worked for me:这是 Sourcetree 中对我有用的解决方法:

  1. Identify the commit where the file was deleted.标识删除文件的提交。

  2. Go to Tools-> Options-> Custom Actions.转到工具-> 选项-> 自定义操作。

  3. Create a new custom actions like this: custom action screen创建一个新的自定义操作,如下所示:自定义操作屏幕

    checkout $SHA~1 -- $FILE结帐 $SHA~1 -- $FILE

  4. Go to the commit where the file was deleted (found in step 1), rick click on the file and select custom action-> your just defined custom action: Restoring the file转到删除文件的提交(在步骤 1 中找到),rick 单击文件并选择自定义操作-> 您刚刚定义的自定义操作:恢复文件

  5. Unfortunately Sourcetree show an error and doesn't do the job.不幸的是,Sourcetree 显示错误并且无法完成工作。 Error in Sourcetree GUI Sourcetree GUI 中的错误

  6. I have the "Show Full Output" option always enabled so I have to close the window manually, this is useful when you need to create a merge request (the link will be there you'd just copy it, without this the window closes automatically and you'll miss the change to copy it).我始终启用“显示完整输出”选项,因此我必须手动关闭窗口,这在您需要创建合并请求时很有用(链接将在那里,您只需复制它,没有此窗口会自动关闭你会错过复制它的更改)。 If you cannot see that window it's probably because you have that feature disabled (that's the default)如果您看不到那个窗口,那可能是因为您禁用了该功能(这是默认设置)

  7. From the window copy the command.从窗口复制命令。

  8. Open a git terminal (from sourcetree top right) Git terminal launcher on Sourcetree在 Sourcetree 上打开一个 git 终端(从 sourcetree 右上角) Git 终端启动器

  9. Paste the command (shift + insert) and press enter.粘贴命令(shift + insert)并按回车键。 Git Console Git 控制台

  10. Voilá!瞧! your file is restored.您的文件已恢复。

In the last image you can see (failed) attempts to restore from the commit the file was deleted, but you actually need to restore from the previous commit, that's the reason the first two attempts failed and the later succeeded (that's the reason for the ~1 at the end of the SHA).在最后一张图片中,您可以看到(失败)尝试从文件被删除的提交中恢复,但实际上您需要从前一次提交中恢复,这就是前两次尝试失败而后一次成功的原因(这就是~1在 SHA 的末尾)。

Hope it helps, happy coding.希望它有帮助,快乐编码。

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

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