运行git重置后丢失的文件 - 在初始提交之前固定

发布于 2025-02-11 04:29:21 字数 214 浏览 0 评论 0原文

因此,我做了一些非常愚蠢的事情:我启动了一个项目,做了很多工作,然后我初始化了一个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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

分开我的手 2025-02-18 04:29:21

如果您正在运行git add,则将斑点添加到存储库中并存储在此处,即使它们已从工作树中删除。您可以通过运行git fsck找到所有这些斑点 - 缠绕,就像这样:

$ git fsck --dangling
Checking object directories: 100% (256/256), done.
notice: HEAD points to an unborn branch (dev)
notice: No default references
dangling blob 257cc5642cb1a054f08cc83f2d943e56fd3ebe99
missing tree 4b825dc642cb6eb9a060e54bf8d69288fbee4904

然后,您可以将每个悬挂的斑点拿走并进行检查:

$ git cat-file blob 257cc5642cb1a054f08cc83f2d943e56fd3ebe99
foo

一旦您知道文件应该在哪里,您可以将该命令的输出重定向到适当的文件。用所有悬空的斑点来做到这一点,您可以恢复工作的树。

要进行将来的参考,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 running git fsck --dangling, like so:

$ git fsck --dangling
Checking object directories: 100% (256/256), done.
notice: HEAD points to an unborn branch (dev)
notice: No default references
dangling blob 257cc5642cb1a054f08cc83f2d943e56fd3ebe99
missing tree 4b825dc642cb6eb9a060e54bf8d69288fbee4904

Then, you can take each of the dangling blobs and inspect them:

$ git cat-file blob 257cc5642cb1a054f08cc83f2d943e56fd3ebe99
foo

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 run git rm --cached FILE to unstage it. Note that by default, git status provides this hint for you to help you out.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文