簡體   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