运行git重置后丢失的文件 - 在初始提交之前固定
因此,我做了一些非常愚蠢的事情:我启动了一个项目,做了很多工作,然后我初始化了一个git回购。我运行git添加
以添加所有项目文件,但我想排除一个文件,因此我迅速运行git reset -hard
在制作初始初始文件之前提交。
现在,所有文件都从存储库中消失了,我不知道如何还原它们,因此任何帮助都将不胜感激!
So, I did something very stupid: i started a project, did quite a lot of work on it, and then I initialized a git repo. I ran git add
to add all of the project files, but i wanted to exclude one file, so I quickly ran git reset --hard
before making an initial commit.
Now all the files have disappeared from the repo and I have no clue how to restore them, so any help will be appreciated!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您正在运行
git add
,则将斑点添加到存储库中并存储在此处,即使它们已从工作树中删除。您可以通过运行git fsck找到所有这些斑点 - 缠绕
,就像这样:然后,您可以将每个悬挂的斑点拿走并进行检查:
一旦您知道文件应该在哪里,您可以将该命令的输出重定向到适当的文件。用所有悬空的斑点来做到这一点,您可以恢复工作的树。
要进行将来的参考,
git重置 - hard
完全吹走了工作树的内容,删除了所有更改。如果您第一次添加了一个文件,并稍后决定您不希望拥有的文件,则可以简单地运行git rm - cached文件
拆下它。请注意,默认情况下,git状态
为您提供了此提示。If you've run
git add
, the blobs have been added to the repository and are stored there, even though they've been deleted from the working tree. You can find all of these blobs by runninggit fsck --dangling
, like so:Then, you can take each of the dangling blobs and inspect them:
Once you know where the file is supposed to be, you can redirect the output of that command to an appropriate file. Do that with all the dangling blobs, and you can recover your working tree.
For future reference,
git reset --hard
completely blows away the contents of your working tree, removing all changes. If you've added a file for the first time and decide later that you'd rather not have, you can simply rungit rm --cached FILE
to unstage it. Note that by default,git status
provides this hint for you to help you out.