简体   繁体   English

如何在提交任何内容之前恢复意外删除的git存储库

[英]How to recover git repository accidentally deleted before committing anything

I decided to try GitHub for the first time and already managed to delete all my files in my most important project while playing around. 我决定第一次尝试GitHub,并且在玩游戏时已经设法删除了我最重要的项目中的所有文件。 Here is my console history: 这是我的控制台历史:

git init
git remote add origin https://github.com/...
git add .
git config --global user.email (...)
git config --global user.name (...)
git reset --hard   // amazing move by me

I searched for many ways to recover this. 我搜索了很多方法来恢复它。 One recommended was: git fsck --lost-found but it only returns: 一个推荐是: git fsck --lost-found但它只返回:

missing tree 4b825dc642cb6eb9a060e54bf8d69288fbee4904
dangling tree 3178c349021f733a9fa7fa0fabd2ac34f8841bdd

and no blobs. 没有斑点。 So is there no hope to recover it? 所以没有希望恢复吗?

That's odd, because missing tree and dangling tree cannot occur unless there are or were commits made. 这很奇怪,因为除非有或没有提交,否则不能发生missing treedangling tree It's git write-tree that builds tree objects from the index (or git hash-object -w -t tree but this is hard to use). 它是git write-tree ,它从索引(或git hash-object -w -t tree构建树对象,但这很难使用。 (Well, the missing tree is the empty tree —I thought that hash ID sounded familiar!—so that's a bit less odd.) (好吧,丢失的树是空树 - 我认为哈希ID听起来很熟悉! - 这有点奇怪。)

Still, however you got to this point, the dangling tree object is probably what has your blob hash IDs. 尽管如此,你仍然到了这一点, 悬挂的树对象可能是你的blob哈希ID。 Use git show or git ls-tree -r on it to get file names and blob hash IDs, then use git show or git cat-file -p on each blob ID to get the file's contents, and store those contents under the name you find in the tree. 在它上面使用git showgit ls-tree -r来获取文件名和blob哈希ID,然后在每个blob ID上使用git showgit cat-file -p来获取文件的内容,并以你的名字存储这些内容在树上找到。

Or, you can use eftshift0's trick : turn the dangling tree into the tree of a commit. 或者,您可以使用eftshift0的技巧 :将悬空树转换为提交树。 That's even better / more convenient. 那更好/更方便。 (eftshift0 should turn this into an answer, which you should accept :-) ) (eftshift0应该把它变成一个答案,你应该接受:-))

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

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