Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I checkout a tree object in git?

Tags:

git

I have lost the commit object in a corrupt repository, but there are still some files and the tree object:

$ git fsck                                                
Checking object directories: 100% (256/256), done.
dangling blob 031be26142ed97da216fb7d79d16a0b0efdf0d71
dangling blob 4b2be7dfef082c2e247be52e6d78600af7b6dd40
dangling tree 4b825dc642cb6eb9a060e54bf8d69288fbee4904
dangling blob ccbb1056cb4e744f9a4b44a439fa036f6a3d7cbe
dangling blob 10bfbc3c1fa10e08cd6a783565f00e7324f61fe5
dangling blob 9b529957be714fef304c4e8161fe6cd138510e98
dangling blob dd5b54882d0b74db99c8a7fbba703d528dc559b9

Is there a way to check out that tree object?

I guess there could be a way to rebuild the commit object with a dummy commit string and check that out.

git cat-file -p tree-sha1
like image 750
Penz Avatar asked Dec 12 '22 13:12

Penz


1 Answers

If you git checkout 4b825dc642cb6eb9a060e54bf8d69288fbee4904 . (the dot for a path name is important, otherwise git will interpret it as a branch switch operation and refuse) it will update all of the files in both the working directory and the index to match the state of 4b825d... (that is, git diff will show nothing, git diff --staged will show a lot of changes, and git status will show a lot of M). You can inspect the state of the files that way.

If you have a guess at a relevant parent commit, you can

git checkout -b recover relevant_parent
git checkout 4b825dc642cb6eb9a060e54bf8d69288fbee4904 .
git commit -m "Recovered this thing that git fsck told me about"

to give it a commit and a branch to address it by.

like image 134
hobbs Avatar answered Dec 27 '22 22:12

hobbs