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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9750049/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 13:13:49  来源:igfitidea点击:

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

gitgit-reset

提问by Said Kaldybaev

let's say yesterday I did some changes on my master branch, and I forgot to add, commit them. and in the morning i did

假设昨天我在我的主分支上做了一些更改,但我忘了添加、提交它们。早上我做了

git reset --hard

is it possible to restore deleted files in this situation ?

在这种情况下是否可以恢复已删除的文件?

回答by Jovan Perovic

Some better IDEs keep track of your files as a local history. 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".

一些更好的 IDE 将您的文件作为本地历史记录进行跟踪。如果您从外部删除了文件(例如,git reset),您应该能够在父目录中单击 IDE 并选择“与本地历史比较”。

I used this feature successfully in PHPStormIDE when my untracked files got wiped out by some utility...

PHPStorm当我的未跟踪文件被某些实用程序清除时,我在IDE 中成功使用了此功能...

回答by Tim

git reset --hardis 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 命令可以帮助您。

回答by Amit

(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.
  2. To recover it, I went in Webstorm and did the following steps:

    • 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 ChangeYou will get an option to get it add to Git.

    • To untrack it, just do a git reset HEAD <filename>from the command line.

  1. 首先,我使用 git 强制清理丢失了我的文件git clean -f
  2. 为了恢复它,我进入了 Webstorm 并执行了以下步骤:

    • 转到项目的项目选项卡,右键单击以进入“本地历史”下的“显示历史”。(如图所示。

    • 单击“显示历史记录”后,可以看到其中包含“外部更改-> 还原”的弹出窗口。单击以还原右侧的所需文件。(如附图所示。

    • 完成上述 2 个步骤后,您应该会在“项目”选项卡中看到您的文件。“本地历史”弹出窗口将显示类似于此附加图片的内容。恢复到外部更改您将获得一个选项以将其添加到 Git。

    • 要取消跟踪,只需git reset HEAD <filename>从命令行执行 a 。

回答by Pod

git reset --hardwon't remove untracked files. git cleanwill, however.

git reset --hard不会删除未跟踪的文件。git clean会,但是。

However if you git addthe files, don't git committhem and then run git reset --hardthose files willbe 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)。

回答by Arpan Saini

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

Setp 2:转到本地历史记录,然后转到显示历史记录

enter image description here

在此处输入图片说明

Step 3:Select the untracked files that your revert accidentally. then select those file in right side panel and revert the changes, you will get the files in reverted format.

第 3 步:选择您意外还原的未跟踪文件。然后在右侧面板中选择这些文件并还原更改,您将获得还原格式的文件。

enter image description here

在此处输入图片说明

回答by Riccardo T.

You can't access to previous version of untracked deleted files from git, because, of coruse, they do not exist. 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.

您无法从 git 访问以前版本的未跟踪已删除文件,因为,当然,它们不存在。我会从一些备份中恢复它们(也许ide/编辑器留下了隐藏的备份文件?),或者,我会避免在该文件系统上工作太多并开始搜索恢复工具。

回答by Dhirendra Rathod

git fsckmay 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}

git show {blobId}