Git仓库数据损坏后如何恢复

这里写自定义目录标题

服务器意外断电后,导致git仓库数据损坏,在客户端pullclone均无法成功,出错如下:

$ git pull
fatal: failed to read object 7c45ff324431a754692e4bdb69225d8fe979aed5: Invalid argument
fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.
$ git clone  ssh://git@192.168.1.238/jw-project.git
Cloning into 'jw-project'...
fatal: failed to read object 7c45ff324431a754692e4bdb69225d8fe979aed5: Invalid argument
fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.

既然clone都不行,说明不是本地仓库的问题,只能去服务器上查原因了。

git@gotech-desktop:~/repositories/jw-project.git$ git log
fatal: failed to read object 7c45ff324431a754692e4bdb69225d8fe979aed5: Invalid argument
git@gotech-desktop:~/repositories/jw-project.git$ git fsck --full
fatal: failed to read object 14670d8b18182c991c971058a3eeac4066a9bcfa: Invalid argument

基本上git的命令都用不了了。查看head节点

git@gotech-desktop:~/repositories/jw-project.git$ cat HEAD
ref: refs/heads/master
git@gotech-desktop:~/repositories/jw-project.git$ cat refs/heads/master
7c45ff324431a754692e4bdb69225d8fe979aed5
git@gotech-desktop:~/repositories/jw-project.git$ cd objects/7c
git@gotech-desktop:~/repositories/jw-project.git/objects/7c$ ls -l 45ff324431a754692e4bdb69225d8fe979aed5
-r--r--r-- 1 git git 0 Nov 26 11:08 45ff324431a754692e4bdb69225d8fe979aed5

这里注意refs/heads/master中记录的objecthash值是这个object的目录名字(2个字符,也就是7c)和文件名字的联合值。可以看到这个object大小是0。我从本地仓库找到这个object将其覆盖后,错误变成新的:

$ git clone  ssh://git@192.168.1.238/jw-project.git
Cloning into 'jw-project'...
error: git upload-pack: git-pack-objects died with error.
fatal: git upload-pack: aborting due to possible repository corruption on the remote side.
remote: fatal: failed to read object 6f60eef4fba13b61b9adf8ee2c4222a9b58d101b: Invalid argument
remote: aborting due to possible repository corruption on the remote side.
fatal: early EOF
fatal: index-pack failed

虽然错误提示办了,但是可以看到这个object是一个问题,大小还是0,继续替换,最后大概有20个文件,都替换完成后,成功了,客户端可以正常clone和pull了。
不过这种方式首先要找到谁是最后一次上传的那个人,用他的本地仓库的这些文件才行。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值