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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25527812/
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-19 10:20:23  来源:igfitidea点击:

Git corrupted repo: how to pick a git object from a clean repository

gitrepositorycorruption

提问by CharlesB

This is one of the numerous questions regarding a Git repo that is corrupted, precisely a loose object that went wrong:

这是关于损坏的 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

Following existing answers on the subject (How do I deal with corrupted git object files?, How to fix corrupted git repository?, or What can I do with Git corruption due to a missing object?) I already removed the object 831a5d31from the corrupted repository.

遵循有关该主题的现有答案(如何处理损坏的 git 对象文件?如何修复损坏的 git 存储库?由于缺少对象而导致 Git 损坏怎么办?)我已经831a5d31从损坏的存储库中删除了对象.

In my case I have a clone of the repository, that seems to hold the object I'm missing, but there's no file at objects/83/1a5d31af4a0af2f5a367689bee27a44efc22c9. How can I do to fix my repository?

就我而言,我有一个存储库的克隆,它似乎保存了我丢失的对象,但objects/83/1a5d31af4a0af2f5a367689bee27a44efc22c9. 我该如何修复我的存储库?

回答by CharlesB

If the file exists elsewhere

如果文件存在于其他地方

The object of the clean repository has been repacked, that's why it doesn't exist anymore as a file.

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

To restore it, first save it as a file, from the clean repository, with

要恢复它,首先将它保存为一个文件,从干净的存储库,用

git show 831a5d31af4a0af2f5a367689bee27a44efc22c9 > 831a5-file

Move 831a5-filein the corrupted repo, and run

移入831a5-file损坏的存储库,然后运行

git hash-object -w 831a5-file

Make sure that the SHA1 given in the output is 831a5d31af4a0af2f5a367689bee27a44efc22c9

确保输出中给出的 SHA1 是 831a5d31af4a0af2f5a367689bee27a44efc22c9

This will store the object, and the repository is fixed!

这将存储对象,并且存储库已修复!

If the file does not exist elsewhere

如果该文件在别处不存在

If the file doesn't exist elsewhere, i.e. the corruption occurred after commit but before you were able to push, there is a way to restore your repo and and re-commit the changes. See this answerto a related question.

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