Git 存储库损坏(不正确的标头检查;松散对象已损坏)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23725925/
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
Git repository corrupt (incorrect header check; loose object is corrupt)
提问by trentcl
I experienced a power failure yesterday evening while writing a commit message. When I booted the machine back up I couldn't complete the commit. I ran git reset
, added back the changed files, and tried again, and got this:
昨天晚上我在写提交消息时遇到了电源故障。当我重新启动机器时,我无法完成提交。我跑了git reset
,添加回更改过的文件,然后再试一次,得到了这个:
% git commit
error: inflate: data stream error (incorrect header check)
error: unable to unpack a94406345ac44982b00cf57b4b9660a35436637f header
fatal: a94406345ac44982b00cf57b4b9660a35436637f is not a valid object
git fsck
reveals the following:
git fsck
揭示了以下内容:
% git fsck --full
Checking object directories: 100% (256/256), done.
error: inflate: data stream error (incorrect header check)
error: unable to unpack 4346883490a0990e68db0187241abc1642765a73 header
error: inflate: data stream error (incorrect header check)
fatal: loose object 4346883490a0990e68db0187241abc1642765a73 (stored in .git/objects/43/46883490a0990e68db0187241abc1642765a73) is corrupt
I notice the messages are complaining about different objects.
我注意到消息抱怨不同的对象。
I searched SO and the Web and tried a few different things but to no avail.
我搜索了 SO 和 Web 并尝试了一些不同的方法,但无济于事。
- I don't have a recent backup copy.
- Cloning the repository into another directory doesn't help; the new repository exhibits the exact same problems.
git stash
gives the same message asgit commit
. All the other git commands seem to work normally.
- 我没有最近的备份副本。
- 将存储库克隆到另一个目录没有帮助;新的存储库表现出完全相同的问题。
git stash
给出与 相同的消息git commit
。所有其他 git 命令似乎都正常工作。
How can I tell what is wrong and fix it?
我怎样才能知道出了什么问题并修复它?
Edit:git log
output as suggested (just the first few lines):
编辑:git log
按建议输出(仅前几行):
% git log --oneline --decorate --all |head -n 8
253b086 (HEAD, new_tokenize) Normalized tokenizer interface slightly
0f2425a (master) Added procs to eval layer
a4d4c22 Added procedures as a type
d1e15ad (tag: v0.10) Added `if' form with tail call semantics
f94a992 (tag: v0.9) Completed environments
031116e Fixed bug where # on a line by itself caused segfault
3d8b09f Added environments, define and set!
01cc624 Put symbol table implementation into types.c
This is a small personal project; I usually just work in (master) but I was doing an experiment at the time (new_tokenize). 253b086 was the last successful commit before the power failure.
这是一个小型的个人项目;我通常只在(master)工作,但我当时正在做一个实验(new_tokenize)。253b086 是断电前最后一次成功提交。
回答by trentcl
It appears that git created files in .git/objects for the new commit, but didn't successfully write to them. I solved it by deleting them one at a time and re-running git fsck --full
to find the next one. I started with the one originally reported by git fsck
:
似乎 git 在 .git/objects 中为新提交创建了文件,但没有成功写入它们。我通过一次删除它们并重新运行git fsck --full
以找到下一个来解决它。我从最初报道的那个开始git fsck
:
% rm -f .git/objects/43/46883490a0990e68db0187241abc1642765a73
% git fsck --full
Checking object directories: 100% (256/256), done.
error: inflate: data stream error (incorrect header check)
error: unable to unpack 86e7247af5865e857a3b61eed99986e2d9538df1 header
error: inflate: data stream error (incorrect header check)
fatal: loose object 86e7247af5865e857a3b61eed99986e2d9538df1 (stored in .git/objects/86/e7247af5865e857a3b61eed99986e2d9538df1) is corrupt
% rm -f .git/objects/86/e7247af5865e857a3b61eed99986e2d9538df1
% git fsck --full
Checking object directories: 100% (256/256), done.
error: inflate: data stream error (incorrect header check)
error: unable to unpack a94406345ac44982b00cf57b4b9660a35436637f header
error: inflate: data stream error (incorrect header check)
fatal: loose object a94406345ac44982b00cf57b4b9660a35436637f (stored in .git/objects/a9/4406345ac44982b00cf57b4b9660a35436637f) is corrupt
And so on. I deleted five objects before git fsck
came up clean, corresponding (as I suppose) to the five files in the commit I was trying to make. I guess that the file history was not corrupted at all.
等等。在git fsck
清理干净之前,我删除了五个对象,对应于(如我所想)我试图制作的提交中的五个文件。我猜文件历史记录根本没有损坏。
Incidentally, I thought of another method that seems to work as well. git clone
copies the bad objects, but git push
does not. After backing up, I created a new empty repository (--bare, because otherwise you can't push to master), then unstaged my changes and pushed both branches into the new repository. Then it was just a matter of checking it out again and restoring the latest changes from my backups.
顺便说一句,我想到了另一种似乎也有效的方法。git clone
复制坏对象,但git push
不复制。备份后,我创建了一个新的空存储库(--bare,因为否则您无法推送到 master),然后取消暂存我的更改并将两个分支推送到新存储库中。然后只需再次检查它并从我的备份中恢复最新更改即可。
Still interested if anyone cares to shed light on the failure mechanism here.
如果有人想了解这里的故障机制,仍然很感兴趣。
回答by TooCooL
Simple answer to this question for anyone facing this problem: the git clone command is the fix, if have a remote repo then clone it to the local folder (after deleting the corrupted local repo), in case you dont have remote repo then try to push the corrupt repo to github and then clone it from there, I think that corrupted objects wont be pushed and it will fix the problem
对于任何面临这个问题的人来说,这个问题的简单答案是:git clone 命令是修复程序,如果有远程存储库,则将其克隆到本地文件夹(删除损坏的本地存储库后),如果您没有远程存储库,请尝试将损坏的 repo 推送到 github,然后从那里克隆它,我认为不会推送损坏的对象,它将解决问题
回答by BitBot
As described in this answerI ran:
如this answer中所述,我跑了:
git reflog expire --expire-unreachable=now --all
git gc --prune=now
Which removed all of my dangling blobs and dangling commits, as well as the corrupt db objects.
这删除了我所有的悬空 blob 和悬空提交,以及损坏的 db 对象。
It was a lot faster than tracking them down one-by-one!
这比一一追踪他们要快得多!