4

My git repo has become corrupted and unfortunately it only exists locally.

$ git fsck --full
Checking object directories: 100% (256/256), done.
broken link from    tree 54b4ff576b2e39831a298e58a38d91890f622b63
              to    tree d564d0bc3dd917926892c55e3706cc116d5b165e
missing tree d564d0bc3dd917926892c55e3706cc116d5b165e

I checked into what d564d0bc is, and it is my log/ folder in a rails project. This folder only contains *.log files (which are ignored) and a .gitkeep file.

I tried following the steps mentioned in this post, but I'm using GitHub for Windows and powershell is screaming at me over an empty pipe.

Any help is appreciated.

Update: I copied the project into linux so I wouldn't have to worry about powershell commands. I still haven't found a solution though.

Community
  • 1
  • 1
MikeKusold
  • 1,632
  • 15
  • 25

1 Answers1

3

If you are sure that missing tree contained only .gitkeep file and you have its content than it is possible to recreate missing tree.

All you need to know is a bunch of low-level git commands? Are you ready? Go! The first of all. You need to use git mktree.

This command reads from stdin data and creates a tree based on this information. It outputs the sha1 summ of newly created tree (Don't forget to check that it equals to d564d0bc3dd917926892c55e3706cc116d5b165e)

Next thing you need to know what the format of this input?! The format is the following

<mode> SP <type> SP <object> TAB <file>

where SP is a space, TAB is a tab.

  • is a file mode
  • is a git object type (blob for files, tree for folders)
  • is a sha1 hash of the object
  • is a file name

Let me show an example. To create the tree from this folder (the some_file file is empty)

drwxrwxr-x 2 aleksandr aleksandr 4096 2012-07-25 03:51 .
drwxrwxr-x 4 aleksandr aleksandr 4096 2012-07-25 03:54 ..
-rw-rw-r-- 1 aleksandr aleksandr    0 2012-07-25 03:51 some_file

you need to run

echo -e "100644 blob e69de29bb2d1d6434b8b29ae775ad8c2e48c5391\tsome_file" | git mktree

But how did you find out the desired sha1 hash?! git hash-object to the rescue. Just

git hash-object log/.gitkeep

to determine its hash. I think that all you need to know to repair your repository.

Oleksandr Pryimak
  • 1,561
  • 9
  • 11
  • Thank you so much. For anyone else that might have this problem, you need to `cd` into the folder, you can't just use a path. In my example I had to go into my `log` folder. The final command I used is: `echo -e "100644 blob e69de29bb2d1d6434b8b29ae775ad8c2e48c5391\t.gitkeep" | git mktree` – MikeKusold Jul 25 '12 at 14:03