简体   繁体   English

是否仍然可以在 git 中恢复已删除的未跟踪文件?

[英]Is it still possible to restore deleted untracked files in git?

let's say yesterday I did some changes on my master branch, and I forgot to add, commit them.假设昨天我在我的 master 分支上做了一些更改,但我忘了添加、提交它们。 and in the morning i did早上我做了

git reset --hard

is it possible to restore deleted files in this situation?在这种情况下是否可以恢复已删除的文件?

Some better IDEs keep track of your files as a local history. 一些更好的IDE会将您的文件跟踪为本地历史记录。 If you removed files externally (say, git reset ) you should be able to click in your IDE on parent directory and choose "Compare with local history". 如果您从外部删除了文件(例如, git reset ),您应该可以在父目录中的IDE中单击并选择“与本地历史记录比较”。

I used this feature successfully in PHPStorm IDE when my untracked files got wiped out by some utility... 当我的未跟踪文件被一些实用程序消灭时,我在PHPStorm IDE中成功使用了此功能...

不...................不是。

git reset --hard is a very dangerous command, so be careful when you use it next time :) git reset --hard是一个非常危险的命令,所以下次使用时要小心:)

If you do not have any commit for those files, it seems you have no chance restore them. 如果您没有对这些文件进行任何提交,那么您似乎无法恢复它们。

Otherwise, reflog command can help you. 否则,reflog命令可以帮到你。

( Note : The steps seem to have changed a bit, since the accepted answer was posted. But the idea remains the same. ) 注意由于接受的答案已经公布,所以步骤似乎有所改变。但这个想法仍然是一样的。

To add to the accepted answer, I was able to recover my deleted files using Webstorm 2016. 为了添加到已接受的答案,我能够使用Webstorm 2016恢复已删除的文件。

  1. First, I lost my file doing a forced clean using git git clean -f . 首先,我使用git git clean -f丢失了我的文件强制清理。
  2. To recover it, I went in Webstorm and did the following steps: 为了恢复它,我进入了Webstorm并执行了以下步骤:

    • Go to Project tab for your project, right click to get to 'Show History' under 'Local History'. 转到项目的“项目”选项卡,右键单击“本地历史记录”下的“显示历史记录”。 ( As shown in this picture. ) 如图所示。

    • Once 'Show History' is clicked, one can see the pop up with 'External Change -> Revert' in it. 单击“显示历史记录”后,可以看到弹出“外部更改 - >还原”。 Click to revert the desired files on right. 单击以还原右侧所需的文件。 ( As shown in the attached picture. ) 如附图所示。

    • After the above 2 steps, you should see your file come back in 'Project' tab. 完成上述两个步骤后,您应该会在“项目”标签中看到您的文件。 'Local History' pop up will show something like in this attached picture. 弹出的“本地历史记录”将显示此附图中的内容。 Reverted to External Change You will get an option to get it add to Git. 恢复为外部更改您将获得一个选项,以将其添加到Git。

    • To untrack it, just do a git reset HEAD <filename> from the command line. 要解决它,只需从命令行执行git reset HEAD <filename>

git reset --hard won't remove untracked files. git reset --hard不会删除未跟踪的文件。 git clean will, however. 但是, git clean会。

However if you git add the files, don't git commit them and then run git reset --hard those files will be lost. 但是如果你git add文件,不要git commit它们然后运行git reset --hard那些文件丢失。

But it's ok, as they can be recovered, as shown in this answer . 但是没关系,因为它们可以恢复,如本答案所示。 (Well, until the next git gc ). (好吧,直到下一个git gc )。

git fsck may be able to help you. git fsck也许可以帮到你。

If you have not done garbage collection yet it might be able to help you. 如果你还没有完成垃圾收集,它可能会帮助你。 Once you have the blob id you can say: 一旦你有blob id你可以说:

git show {blobId}

In Intellij how to recover the untracked revert changes 在Intellij中如何恢复未跟踪的恢复更改

Step 1: Select the Folder , Right click on it 第1步:选择文件夹,右键单击它

Setp 2: Go to Local History, and then go to Show history 第2步:转到“本地历史记录”,然后转到“显示历史记录”

在此输入图像描述

Step 3: Select the untracked files that your revert accidentally. 第3步:选择意外恢复的未跟踪文件。 then select those file in right side panel and revert the changes, you will get the files in reverted format. 然后在右侧面板中选择这些文件并还原更改,您将获得恢复格式的文件。

在此输入图像描述

You can't access to previous version of untracked deleted files from git, because, of coruse, they do not exist. 您无法从git访问以前版本的未跟踪已删除文件,因为在coruse中它们不存在。 I would recover them from some backup (maybe there are hidden backup files left by the ide/editor?), or, in alternative, I would avoid working on that filesystem too much and start searching for recovery tools. 我会从一些备份中恢复它们(也许是ide / editor留下了隐藏的备份文件?),或者,作为替代方案,我会避免过多地使用该文件系统并开始搜索恢复工具。

If file retrieval tools like "recuva" didn't work, do the following:如果“recuva”等文件检索工具不起作用,请执行以下操作:

  1. If you have build the solution at least once then there will be DLLs (In .net intermediate language files are called DLLs) stored in your Debug/Release or some other folder.如果您至少构建了一次解决方案,那么将在您的 Debug/Release 或其他文件夹中存储 DLL(在 .net 中间语言文件中称为 DLL)。
  2. Then try disassembling the DLLs using tools like DotPeek etc.然后尝试使用 DotPeek 等工具反汇编 DLL。

This worked for me.这对我有用。 I was able to retrieve the content.我能够检索内容。

If you're on Windows, go to the folder in which you kept these files > Right click > properties > "Previous revisions" and double click on a recent revision.如果您使用的是 Windows,请转到您保存这些文件的文件夹 > 右键单击 > 属性 >“以前的修订版”,然后双击最近的修订版。 It'll show how all of these files were at that time mentioned there.它将显示当时所有这些文件是如何被提及的。 Open the files you want and if it's the one you want copy the contents and paste in your current file.打开你想要的文件,如果它是你想要的,复制内容并粘贴到你当前的文件中。

The Local History method or simply clicking ctrl+z (undo) in the editor you wrote it in, as mentioned in the other answers is the best answer.如其他答案中所述, Local History方法或在您编写它的编辑器中简单地单击ctrl+z (撤消)是最佳答案。 But this method came in handy when I didn't actually code in an IDE that supports local history.但是当我实际上没有在支持本地历史记录的 IDE 中编写代码时,这种方法就派上用场了。

For VScode, the Below steps worked for me to restore deleted untracked file对于 VScode,以下步骤对我恢复已删除的未跟踪文件有效

  1. CTRL + SHIFT + P CTRL + SHIFT + P
  2. LOOK FOR: "Local History: Find Entry to Restore"查找:“本地历史:查找要还原的条目”
  3. SEARCH FOR DELETED FILE搜索已删除的文件
  4. SELECT THE LAST SAVED FILE TO OPEN选择上次保存的文件打开

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

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