Home > Back-end >  Restore GIT files from .git/index
Restore GIT files from .git/index

Time:11-27

I have a .git/index file and I want to recover a file. If I use the command git status I see this:

└─$ 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

I need to read the content of flag.txt file. How can I do this?

CodePudding user response:

From my understanding of Git, you can use git restore flag.txt to rollback changes to it to the most recent commit.

CodePudding user response:

My guess—it's just a guess—is that you are working in a Git repository that you've stored in some sort of cloud-synchronized workspace (Google Drive, iCloud, etc). Based on what you put in this comment:

[git restore flag.txt produces an error message:]

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

your index file is intact but your Git repository itself is not, having been damaged by some other software—probably said cloud-syncer, although it's also possible that you yourself removed parts of the .git directory (we can't tell since you did not show us your previous actions).

Unfortunately, this means you cannot get your file back, or at least, not from Git. Your git status text:

└─$ git status
On branch master

No commits yet

shows that you are in an empty repository, in which the current branch name, master, is for a branch that does not exist.

Meanwhile:

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's index (also called the staging area) indicates that there are four new files prepared for committing. These files have name—app.py, exploit.py, and so on—and (not shown) internal object hash IDs. Unfortunately the error message you got shows that the hash ID for flag.txt, namely c4bef48579d85bb52df9f0bf5a0873dad3d632e3, doesn't actually exist in the Git database, or has been damaged.

At the same time:

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

This indicates that the working tree copies of these files no longer exist either.

We can therefore conclude the following:

  • These no longer exist as ordinary files. You'll have to use some non-Git file recovery software to get them back as ordinary files, if that's possible at all.

  • These did exist as Git internal objects at one point, such as c4bef48579d85bb52df9f0bf5a0873dad3d632e3 for flag.txt. The .git/index file is still intact and still records the path name flag.txt and the internal Git object hash ID.

  • But the Git internal object c4bef48579d85bb52df9f0bf5a0873dad3d632e3 is missing: something removed it. So Git can't read the object to get the file's content.

  •  Tags:  
  • git
  • Related