如何恢复因硬盘故障损坏的 Git 对象?

编程入门 行业动态 更新时间:2024-10-18 10:36:36
本文介绍了如何恢复因硬盘故障损坏的 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-object,git mktree 和git 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 show 或 git cat-file 或 git 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 show、git cat-file和git 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 对象?

本文发布于:2023-06-06 01:39:45,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/531178.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:故障   对象   硬盘   Git

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!