繁体   English   中英

从 .git/index 恢复 GIT 文件

[英]Restore GIT files from .git/index

我有一个 .git/index 文件,我想恢复一个文件。 如果我使用命令 git status 我看到这个:

└─$ git status
On branch master

No commits yet

Changes to be committed:
  (use "git rm --cached <file>..." to unstage)
        new file:   app.py
        new file:   exploit.py
        new file:   flag.txt
        new file:   templates/index.html

Changes not staged for commit:
  (use "git add/rm <file>..." to update what will be committed)
  (use "git restore <file>..." to discard changes in working directory)
        deleted:    app.py
        deleted:    exploit.py
        deleted:    flag.txt
        deleted:    templates/index.html

我需要阅读 flag.txt 文件的内容。 我怎样才能做到这一点?

根据我对 Git 的理解,您可以使用git restore flag.txt将更改回滚到最近的提交。

我的猜测——这只是一个猜测——是您正在一个 Git 存储库中工作,该存储库存储在某种云同步工作区(Google Drive、iCloud 等)中。 根据您在此评论中的内容:

[ git restore flag.txt产生错误消息:]

 error: unable to read sha1 file of flag.txt (c4bef48579d85bb52df9f0bf5a0873dad3d632e3)

您的索引文件是完整的,但您的 Git 存储库本身不是,已被其他一些软件损坏 - 可能是云同步器,尽管您自己也可能删除了.git目录的部分内容(我们无法确定,因为您这样做了不要向我们展示您以前的行为)。

不幸的是,这意味着您无法取回您的文件,或者至少不能从 Git 取回。 您的git status文本:

 └─$ git status On branch master No commits yet

显示您在一个空的 repository中,其中当前的分支名称master是一个不存在的分支。

同时:

 Changes to be committed: (use "git rm --cached <file>..." to unstage) new file: app.py new file: exploit.py new file: flag.txt new file: templates/index.html

Git 的索引(也称为暂存区)表明有四个新文件准备好提交。 这些文件具有名称app.pyexploit.py等——以及(未显示)内部 object hash ID。 不幸的是,您收到的错误消息显示 flag.txt 的flag.txt ID 即c4bef48579d85bb52df9f0bf5a0873dad3d632e3实际上并不存在于 Git 数据库中,或者已损坏。

同时:

 Changes not staged for commit: (use "git add/rm <file>..." to update what will be committed) (use "git restore <file>..." to discard changes in working directory) deleted: app.py deleted: exploit.py deleted: flag.txt deleted: templates/index.html

这表明这些文件的工作树副本也不再存在。

因此,我们可以得出以下结论:

  • 这些不再作为普通文件存在。 如果可能的话,您将不得不使用一些非 Git 文件恢复软件将它们作为普通文件恢复。

  • 这些确实以 Git 内部对象的形式存在,例如c4bef48579d85bb52df9f0bf5a0873dad3d632e3 for flag.txt .git/index文件仍然完好无损,仍然记录路径名flag.txt和内部 Git object hash ID。

  • 但是 Git 内部 object c4bef48579d85bb52df9f0bf5a0873dad3d632e3不见了:有些东西将其删除。 所以 Git 无法读取 object 来获取文件的内容。

暂无
暂无

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

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