简体   繁体   English

在 Git 中恢复悬空的 blob

[英]Recover dangling blobs in Git

I did a "git rm -rf ."我做了一个“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. (试图清除我在执行“git add”后删除的文件的缓存。)而不考虑 git 会物理删除文件。 I don't have an initial commit/branch yet.我还没有初始提交/分支。

$ git init
$ git add .

I remember to add my ".gitignore".我记得添加我的“.gitignore”。 Then, out of being lazy and also not bothering to look up the proper command I did:然后,出于懒惰并且也懒得查找我所做的正确命令:

$ git rm -rf .

Now every file that git was tracking is gone.现在 git 跟踪的每个文件都消失了。 Oops.哎呀。

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.环顾四周,我只能找到有关恢复悬空提交的教程。

You can use git show fce4a77b63b25abbc010859b4037589983820329 to see the content (or git show fce4a > somefile to 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-tree command or some git checkout e48751c3b37a9cab692133202bbb933241f73f69 -- .如果你可以看到(使用git ls-tree )你的根树,你可以使用git commit-tree命令或一些git checkout e48751c3b37a9cab692133202bbb933241f73f69 -- .创建一个提交git checkout e48751c3b37a9cab692133202bbb933241f73f69 -- . to retrieve files.检索文件。

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

Running that then saves the dangling blobs to the following path运行然后将悬空的 blob 保存到以下路径

.git/lost-found/other

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, eg,如果你决定它是你想要的东西,你总是可以创建一个新的引用,例如,

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

Here's a script to convert the blobs to readable, .txt files.这是将 blob 转换为可读的 .txt 文件的脚本。 Use the other answers (eg, user909746's answer ) to create and navigate to the lost-found folder.使用其他答案(例如, user909746 的答案)创建并导航到 lost-found 文件夹。

From within the lost-found folder, run:从 lost-found 文件夹中,运行:

for FILE in *; do git show $FILE > "$FILE.txt"; done

This will create a .txt copy of every blob and add it to the current directory, which is the lost-found folder.这将创建每个 blob 的 .txt 副本并将其添加到当前目录,即 lost-found 文件夹。 You can now filter using your file explorer of choice for .txt files and browse them all to find the files you need.您现在可以使用您选择的文件资源管理器过滤 .txt 文件并浏览它们以找到您需要的文件。

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

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