如何恢复因硬盘故障损坏的 Git 对象? [英] How to recover Git objects damaged by hard disk failure?

查看:26
本文介绍了如何恢复因硬盘故障损坏的 Git 对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了硬盘故障,导致 Git 存储库的某些文件损坏.运行 git fsck --full 时,我得到以下输出:

I have had a hard disk failure which resulted in some files of a Git repository getting damaged. When running git fsck --full I get the following output:

error: .git/objects/pack/pack-6863e0a0e4b4ded6090fac5d12eba6ca7346b19c.pack SHA1 checksum mismatch
error: index CRC mismatch for object 6c8cae4994b5ec7891ccb1527d30634997a978ee from .git/objects/pack/pack-6863e0a0e4b4ded6090fac5d12eba6ca7346b19c.pack at offset 97824129
error: inflate: data stream error (invalid code lengths set)
error: cannot unpack 6c8cae4994b5ec7891ccb1527d30634997a978ee from .git/objects/pack/pack-6863e0a0e4b4ded6090fac5d12eba6ca7346b19c.pack at offset 97824129
error: inflate: data stream error (invalid stored block lengths)
error: failed to read object 0dcf6723cc69cc7f91d4a7432d0f1a1f05e77eaa at offset 276988017 from .git/objects/pack/pack-6863e0a0e4b4ded6090fac5d12eba6ca7346b19c.pack
fatal: object 0dcf6723cc69cc7f91d4a7432d0f1a1f05e77eaa is corrupted

我有存储库的备份,但唯一包含包文件的备份已经损坏.所以我认为我必须找到一种方法来从不同的备份中检索单个对象,并以某种方式指示 Git 生成一个仅包含正确对象的新包.

I have backups of the repository, but the only backup that includes the pack file has it already damaged. So I think that I have to find out a way to retrieve the single objects from different backups and somehow instruct Git to produce a new pack with only correct objects.

你能给我提示如何修复我的存储库吗?

Can you please give me hints how to fix my repository?

推荐答案

在以前的一些备份中,您的坏对象可能已打包在不同的文件中,或者可能是松散的对象.因此,您的对象可能会被恢复.

In some previous backups, your bad objects may have been packed in different files or may be loose objects yet. So your objects may be recovered.

您的数据库中似乎有一些坏对象.所以你可以手动完成.

It seems there are a few bad objects in your database. So you could do it the manual way.

由于git hash-objectgit mktreegit commit-tree 不写对象,因为它们是在包中找到的,然后开始这样做:

Because of git hash-object, git mktree and git commit-tree do not write the objects because they are found in the pack, then start doing this:

mv .git/objects/pack/* <somewhere>
for i in <somewhere>/*.pack; do
  git unpack-objects -r < $i
done
rm <somewhere>/*

(您的包已从存储库中移出,并再次在其中解压缩;现在数据库中只有好的对象)

(Your packs are moved out from the repository, and unpacked again in it; only the good objects are now in the database)

你可以这样做:

git cat-file -t 6c8cae4994b5ec7891ccb1527d30634997a978ee

并检查对象的类型.

如果类型是 blob:从以前的备份中检索文件的内容(使用 git showgit cat-filegit unpack-file; 然后你可以 git hash-object -w 重写当前存储库中的对象.

If the type is blob: retrieve the contents of the file from previous backups (with git show or git cat-file or git unpack-file; then you may git hash-object -w to rewrite the object in your current repository.

如果类型是树:您可以使用 git ls-tree 从以前的备份中恢复树;然后 git mktree 在您当前的存储库中再次编写它.

If the type is tree: you could use git ls-tree to recover the tree from previous backups; then git mktree to write it again in your current repository.

如果类型是commit:与git showgit cat-filegit commit-tree相同.

If the type is commit: the same with git show, git cat-file and git commit-tree.

当然,我会在开始此过程之前备份您的原始工作副本.

Of course, I would backup your original working copy before starting this process.

另外,看看如何恢复损坏的 Blob 对象.

这篇关于如何恢复因硬盘故障损坏的 Git 对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆