简体   繁体   中英

Add corrupted or deleted commit to remote git server

It seems that some files got corrupted on my remote git server and I'm getting the following error when I try to clone the repo to a new system.

remote: error: Could not read c168e82dd62c0cdbf3ea7c3be3a84218a12c8a03
remote: fatal: Failed to traverse parents of commit 02d8c9217333d89afd61da1788fa82329b692610
remote: aborting due to possible repository corruption on the remote side.
fatal: protocol error: bad pack header

When I run a git fsck --name-objects :

broken link from  commit 02d8c9217333d89afd61da1788fa82329b692610 (~17)
              to  commit c168e82dd62c0cdbf3ea7c3be3a84218a12c8a03 (~18)

I have a local copy of the repo that doesnt have the error, is there a way to just copy the missing or corrupted files?

It's not so much files that are missing or corrupt, it's that the internal object (commit 02d8c9217333d89afd61da1788fa82329b692610 on the server) refers to another internal object ( c168e82dd62c0cdbf3ea7c3be3a84218a12c8a03 ) that is missing. While some objects are stored in individual files, others are packed: thousands of objects in a single file.

If you have object c168e82dd62c0cdbf3ea7c3be3a84218a12c8a03 you could extract it from your own repository, send it over, and insert it into the other repository. However, this is usually a lot harder than just re-cloning the good repository (provided it has everything—not every clone will be complete, eg, it may be out of date, or it might be a --single-branch clone).

To extract the one object, first find its type:

git cat-file -t c168e82dd62c0cdbf3ea7c3be3a84218a12c8a03

Then extract its raw data:

git cat-file -p c168e82dd62c0cdbf3ea7c3be3a84218a12c8a03 > /tmp/obj.data

Copy the object data over and insert it into the repository:

scp /tmp/obj.data serverhost:/tmp/obj.data
ssh serverhost
cd ...
git hash-object -w -t $type /tmp/obj.data

where $type is the type from git cat-file -t on the machine with the good copy of the repository.

(Adding the missing object may fix everything, or may just expose more problems, which is another reason it's often preferable just to replace the bad repository entirely with another clone.)

由于我的本地PC上有一个最新的存储库,因此最终使用--bare参数克隆了存储库,并使用这些文件更新了服务器。

git clone --bare <path to clone from> <path to clone to>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM