如何修复因 git fetch 中断而损坏的 git 存储库?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10671638/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 06:56:33  来源:igfitidea点击:

How to fix git repository broken by interrupted git fetch?

gitgit-pullgit-fetch

提问by nrz

If git fetchgets interrupted for example by Ctrl-C or caused by connectivity problems, after that git fetchand also git pullfail to work.

如果git fetch被 Ctrl-C 等中断或由连接问题引起,在此之后git fetchgit pull无法工作。

user@computer:~/code/openttd-git$ git fetch
^C
user@computer:~/code/openttd-git$ git fetch
error: Unable to find 22d90742fc79a9011fb86ee03d8aeea66bc12657 under http://git.openttd.org/openttd/trunk.git
Cannot obtain needed object 22d90742fc79a9011fb86ee03d8aeea66bc12657
error: Fetch failed.

I believe this is not related to the repository. Using git cloneto create a copy of this broken local repository into a new local repository does not fix this. The only solution I know so far is to git clonethe entire remote repository (origin/master) into a new local repository. But is there any better (faster) solution?

我相信这与存储库无关。使用git clone将这个损坏的本地存储库的副本创建到新的本地存储库并不能解决此问题。到目前为止,我知道的唯一解决方案是git clone将整个远程存储库 ( origin/master) 放入一个新的本地存储库。但是有没有更好(更快)的解决方案?

There is Debian bug reportthat has last message from February 2011. Is this the same bug I have or is there already a fix or any solution or workaround to this? My git version is 1.7.10.

Debian 错误报告,其中包含 2011 年 2 月的最后一条消息。这是我遇到的相同错误还是已经有修复程序或任何解决方案或变通方法?我的 git 版本是 1.7.10。

采纳答案by Matija Nalis

find a *.pack.tempin .git/objects/packin your local repository. Then find an .idxfile with same basename, and move both of them away (or remove them, but it is better to be safe than sorry). Rerun git fetchand it should work (well, it did for me).

在您的本地存储库中找到一个*.pack.tempin .git/objects/pack。然后找到一个.idx具有相同基本名称的文件,并将它们都移开(或删除它们,但安全总比抱歉好)。重新运行git fetch它应该可以工作(好吧,它对我有用)。

For example:

例如:

% git fetch
error: Unable to find a4fb0b54b2609df8a1ee4b97c268d205fc5bf9f1 under https://www.example.com/~someuser/something.git
Cannot obtain needed object a4fb0b54b2609df8a1ee4b97c268d205fc5bf9f1
error: fetch failed.

% ls -l .git/objects/pack
total 65872
-rw-r--r-- 1 someuser someuser    64072 Feb 12  2014 pack-2e31e66e67d8596f1193bbbc06c87293900c6e45.idx
-rw-r--r-- 1 someuser someuser    16920 Jul 21  2013 pack-3d76e0bf6c67d71913efc0711d56f04c7f79b95d.idx
-rw-r--r-- 1 someuser someuser    62224 Feb 11  2014 pack-74107fa80989df6619479874d94b5f8ed010fd2f.idx
-rw-r--r-- 1 someuser someuser    96552 Oct 30 22:55 pack-bb75633331ea0e74d4d3cb29f7660e1ba00fb899.idx
-rw-r--r-- 1 someuser someuser    73228 Mar  6  2014 pack-de0c1bcf3550cd7a2fd0c5a981bc17d15f1144c0.idx
-r--r--r-- 1 someuser someuser   129144 Feb  2 18:57 pack-ffb25d036dea040923468e2de07023f9b497aeb7.idx
-r--r--r-- 1 someuser someuser 46413554 Feb  2 18:57 pack-ffb25d036dea040923468e2de07023f9b497aeb7.pack
-r--r--r-- 1 someuser someuser   129312 Feb  2 19:10 pack-ffbdfa2c676aaf392ea722cb68eaa87e45af092c.idx
-rw-r--r-- 1 someuser someuser 20450545 Feb  2 19:09 pack-ffbdfa2c676aaf392ea722cb68eaa87e45af092c.pack
-rw-r--r-- 1 someuser someuser   129312 Feb  2 18:36 pack-ffbdfa2c676aaf392ea722cb68eaa87e45af092c.idx
-rw-r--r-- 1 someuser someuser  9863168 Feb  2 18:37 pack-ffbdfa2c676aaf392ea722cb68eaa87e45af092c.pack.temp

% mv .git/objects/pack/pack-ffbdfa2c676aaf392ea722cb68eaa87e45af092c.idx /tmp/
% mv .git/objects/pack/pack-ffbdfa2c676aaf392ea722cb68eaa87e45af092c.pack.temp /tmp/
% git fetch
From https://www.example.com/~someuser/something
   3288ab9..a4fb0b5  master     -> origin/master

回答by klogg

Try these commands:

试试这些命令:

git fsck
git gc

回答by teaforthecat

man git-fsck

says to use rsync :

说要使用 rsync :

Any corrupt objects you will have to find in backups or other archives (i.e., you can just remove them and do an rsync with some other site in the hopes that somebody else has the object you have corrupted).

您必须在备份或其他档案中找到任何损坏的对象(即,您可以删除它们并与其他站点进行 rsync,希望其他人拥有您损坏的对象)。

rsync -av user@host:repo/.git ./.git

worked for me

对我来说有效

回答by bcmpinc

It is likely that the repository iscorrupt. Running git fsckand git gcon the server might solve it. Cloning into a separate directory and pulling from that directory will also give you the commits. Afterwards a git fetchwill work, as it only updates the refs and does not have to fetch any objects.

存储库可能损坏。在服务器上运行git fsck和运行git gc可能会解决它。克隆到一个单独的目录并从该目录中提取也将为您提供提交。之后 agit fetch将起作用,因为它只更新 refs 而不必获取任何对象。

回答by larsks

Are you able to run :

你能跑吗:

git reset --hard <some prior commit>

In theory, if you've just run git fetch, you should be able to:

理论上,如果您刚刚运行git fetch,您应该能够:

git reset --hard HEAD

This should discard the changes caused by the interrupted fetch operation, returning your repository to a prior state. At this point you should be able to rerun your fetchoperation.

这应该会丢弃由中断的获取操作引起的更改,将您的存储库返回到先前的状态。此时,您应该能够重新运行您的fetch操作。

回答by positron

Have you tried cleaning the repo?

您是否尝试过清理回购?

git gc

Be warned, as the above command also cleans the reflog stuff.

请注意,因为上述命令还会清除 reflog 内容。