是否仍然可以在 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
Is it still possible to restore deleted untracked files in git?
提问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 PHPStorm
IDE when my untracked files got wiped out by some utility...
PHPStorm
当我的未跟踪文件被某些实用程序清除时,我在IDE 中成功使用了此功能...
回答by Tim
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 命令可以帮助您。
回答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 恢复我删除的文件。
- First, I lost my file doing a forced clean using git
git clean -f
. 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.
回答by Pod
git reset --hard
won't remove untracked files. git clean
will, however.
git reset --hard
不会删除未跟踪的文件。git clean
会,但是。
However if you git add
the files, don't git commit
them and then run git reset --hard
those 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:转到本地历史记录,然后转到显示历史记录
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 步:选择您意外还原的未跟踪文件。然后在右侧面板中选择这些文件并还原更改,您将获得还原格式的文件。
回答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/编辑器留下了隐藏的备份文件?),或者,我会避免在该文件系统上工作太多并开始搜索恢复工具。