Git损坏的repo:如何从干净的存储库中选择Git对象

Git损坏的repo:如何从干净的存储库中选择Git对象,git,repository,corruption,Git,Repository,Corruption,这是关于Git回购协议的众多问题之一,Git回购协议已被破坏,正是一个出错的松散对象: $ git gc Counting objects: 3299, done. error: inflate: data stream error (unknown compression method) error: unable to unpack 831a5d31af4a0af2f5a367689bee27a44efc22c9 header Delta compression using up to 6

这是关于Git回购协议的众多问题之一,Git回购协议已被破坏,正是一个出错的松散对象:

$ git gc
Counting objects: 3299, done.
error: inflate: data stream error (unknown compression method)
error: unable to unpack 831a5d31af4a0af2f5a367689bee27a44efc22c9 header
Delta compression using up to 6 threads.
Compressing objects: 100% (3283/3283), done.
error: inflate: data stream error (unknown compression method)
fatal: loose object 831a5d31af4a0af2f5a367689bee27a44efc22c9 (stored in .git/objects/83/1a5d31af4a0af2f5a367689bee27a44efc22c9) is corrupt
error: failed to run repack
按照关于主题(、、或)的现有答案,我已经从损坏的存储库中删除了对象
831a5d31


在我的例子中,我有一个存储库的克隆,它似乎保存了我丢失的对象,但在
objects/83/1a5d31af4a0af2f5a367689bee27a44efc22c9
中没有文件。如何修复我的存储库?

如果文件存在于其他位置

clean存储库的对象已被重新打包,这就是它不再作为文件存在的原因

要恢复它,首先将它保存为文件,从clean存储库中,使用

git show 831a5d31af4a0af2f5a367689bee27a44efc22c9 > 831a5-file
在损坏的repo中移动
831a5文件
,然后运行

git hash-object -w 831a5-file
确保输出中给出的SHA1是
831a5d31af4a0af2f5a367689bee27a44efc22c9

这将存储对象,并且存储库是固定的

如果文件在其他地方不存在


如果文件在其他地方不存在,即在提交后但在您能够推送之前发生损坏,则有一种方法可以恢复您的回购并重新提交更改。请参阅相关问题。

这适用于blob对象;有没有办法导入/导出树对象?回答我自己的问题,对树等对象执行此操作:
git cat file tree>temp
git hash object-t tree-w temp
谢谢,您保存了我的repo!请注意,如果您的干净回购来自不同的操作系统,那么如果不同操作系统的行尾不相同,SHA1可能不匹配。不过,解决方案仍然有效。这对我来说不起作用,但复制了由
git fsck
指定的丢失的
对象
文件