在 git 中恢复悬空的 blob

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9560184/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 13:07:03  来源:igfitidea点击:

Recover dangling blobs in git

git

提问by Zack Parsons

I did a "git rm -rf ." (trying to purge the cache of files I had removed after doing "git add .") without thinking git would physically delete the files. I don't have an initial commit/branch yet.

我做了一个“git rm -rf”。(试图清除我在执行“git add”后删除的文件的缓存。)而不考虑 git 会物理删除文件。我还没有初始提交/分支。

$ git init
$ git add .

I remember to add my ".gitignore". Then, out of being lazy and also not bothering to look up the proper command I did:

我记得添加我的“.gitignore”。然后,出于懒惰并且也懒得查找我所做的正确命令:

$ git rm -rf .

Now every file that git was tracking is gone. Oops.

现在 git 跟踪的每个文件都消失了。哎呀。

How do I recover the files using the dangling blobs

如何使用悬空 blob 恢复文件

$ git fsck
notice: HEAD points to an unborn branch (master)
notice: No default references
dangling blob 45cb2316b079df7898a28bab1389c87d37de4da5
dangling blob 06b9d0f91bb0643a54ec027efc0efd6645d95326
dangling blob c6c828230bc129da40928d55297577421c6f2e79
dangling blob 087b296f5ae80151fb0d6150927ebe8049ed7706
dangling blob c957743cb7ea533ce772d178394ce9f656b17b7c
dangling blob 0ea745612b105394f5cd5c0119a829de98a0a954
dangling blob 8fb1fa03c12cd56d4e2284008e92a3666bc60b93
dangling blob cf49a6cf77ac792b108577c01ccf88cb2c28228c
dangling blob 93817d9eeefea6ea894544e78b0e0970aa5adc46
dangling blob 94a9ed024d3859793618152ea559a168bbcbb5e2
dangling blob 574b7130959f2278c88946cf385fc49adcdf28c2
dangling blob 582f5bceb8b35c01fd7442678a3cd7e1088b7957
dangling blob d9fb7ca5775745b4332f630400d52c979aab35cd
dangling blob 2087b182bf8b4b8d5ed8fe45a50c7661bda25feb
dangling blob 6109baf32d04410c84d860501057a7f55a13f9dd
dangling blob 22cc2ac7585b1d47bccf2ecb7bf57e16c2142bb6
dangling blob 63b329443cc27273fad14c1e41de5bb9bf1bdd03
dangling blob a2296d429715c9b6d730fdcc972eefdf8273d2e2
dangling blob e41aedd7b2dbdee8b965a30d40ed0c9275194984
dangling blob 6dc7b06e8f03cda72cf223b050d07ccd2b850fc8
dangling blob ee52d86d94a40e7092dc34d1b2760244ec7dccaf
dangling blob 2ffa08c086d3702563d498c9069a345940b3a348
dangling blob 6fafa07526ad288ff2f6e26810ed9818eb18aabb
dangling blob b1cdf17b5bb40b4839cfc80f7e91bbcf7b94f798
dangling blob 353db77b88c248c752cdbd914787b9ebdf2d700f
dangling blob f61d3492ce0d0e396fd322114a5e3475886de1cc
dangling blob 7756a8713095390f5b106b81ae7f7247f762970b
dangling blob bc995b7f9f6806dd5678227579a515bb5768d3a0
dangling blob fce4a77b63b25abbc010859b4037589983820329
dangling blob 3e3335f8cd27168d4fe81f61421d647f2905b7b0
dangling blob 7f56efed4f8706e5b79408afbde197964d824eab

Looking around I could only find tutorials on recovering dangling commits

环顾四周,我只能找到有关恢复悬空提交的教程

回答by Vi.

You can use git show fce4a77b63b25abbc010859b4037589983820329to see the content (or git show fce4a > somefileto dump it to a file).

您可以使用git show fce4a77b63b25abbc010859b4037589983820329查看内容(或git show fce4a > somefile将其转储到文件)。

File names are lost (unless there are also dangling trees or other sources of information like command history in .bash_history).

文件名丢失(除非还有悬空树或其他信息源,如 中的命令历史记录.bash_history)。

If you can see (with git ls-tree) your root tree, you can create a commit with it using git commit-treecommand or some git checkout e48751c3b37a9cab692133202bbb933241f73f69 -- .to retrieve files.

如果您可以看到(使用git ls-tree)您的根树,您可以使用git commit-tree命令或 somegit checkout e48751c3b37a9cab692133202bbb933241f73f69 -- .来创建一个提交来检索文件。

回答by Vi.

I've recovered them in the past using the fsck command with the lost and found option:

我过去曾使用 fsck 命令和 lost and found 选项恢复它们:

git fsck --lost-found

git fsck --lost-found

Running that then saves the dangling blobs to the following path

运行然后将悬空的 blob 保存到以下路径

.git/lost-found/other

.git/lost-found/other

回答by Ricardo Souza

See here: http://schacon.github.com/git/user-manual.html#dangling-objects

见这里:http: //schacon.github.com/git/user-manual.html#dangling-objects

For commits, you can just use:

    $ gitk <dangling-commit-sha-goes-here> --not --all

This asks for all the history reachable from the given commit but not from any branch, tag, or other reference. If you decide it's something you want, you can always create a new reference to it, e.g.,

    $ git branch recovered-branch <dangling-commit-sha-goes-here>

For blobs and trees, you can't do the same, but you can still examine them. You can just do

    $ git show <dangling-blob/tree-sha-goes-here>

Usually, dangling blobs and trees aren't very interesting. They're almost always the result of either being a half-way mergebase (the blob will often even have the conflict markers from a merge in it, if you have had conflicting merges that you fixed up by hand), or simply because you interrupted a "git fetch" with ^C or something like that, leaving some of the new objects in the object database, but just dangling and useless.

对于提交,您可以使用:

    $ gitk <dangling-commit-sha-goes-here> --not --all

这要求所有可从给定提交访问的历史记录,而不是从任何分支、标签或其他引用。如果你决定它是你想要的东西,你总是可以创建一个新的引用,例如,

    $ git branch recovered-branch <dangling-commit-sha-goes-here>

对于斑点和树,您不能这样做,但您仍然可以检查它们。你可以做

    $ git show <dangling-blob/tree-sha-goes-here>

通常,悬垂的斑点和树木并不是很有趣。它们几乎总是要么是中途合并库的结果(如果您有手动修复的冲突合并,blob 通常甚至会包含来自合并的冲突标记),或者仅仅是因为您打断了带有 ^C 或类似内容的“git fetch”,在对象数据库中留下一些新对象,但只是悬空而无用。