将损坏的松散对象修复为 git 中的提交

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

fixing a corrupt loose object as a commit in git

git

提问by Will.

Immediately before receiving this error I did the following:

在收到此错误之前,我立即执行了以下操作:

user@thismachine:~/file/path$ git add *
user@thismachine:~/file/path$ git push 
^C
user@thismachine:~/file/path$ git commit -m "my commitmesg"

(I panicked because I forgot to add a commit before pushing, so I cntrl+c'ed it.

(我很恐慌,因为我在推送之前忘记添加提交,所以我 cntrl+c'ed 它。

Now, I receive the following error from git fsck --full:

现在,我收到以下错误git fsck --full

error: inflate: data stream error (incorrect header check)
error: corrupt loose object '5cdeb9c3a1fe164cb4d2779d1e0d9d9f4ef18c6a'
fatal: loose object 5cdeb9c3a1fe164cb4d2779d1e0d9d9f4ef18c6a (stored in .git/objects/5c/deb9c3a1fe164cb4d2779d1e0d9d9f4ef18c6a)

git cat-file -t 5cdeb9c3a1fe164cb4d2779d1e0d9d9f4ef18c6a returns that this object is a commit.

git cat-file -t 5cdeb9c3a1fe164cb4d2779d1e0d9d9f4ef18c6a 返回该对象是一个提交。

After searching, I've found how fix this if the object is a blobbut not if it's a commit.

搜索后,我发现了如果对象是blob而不是提交时如何解决这个问题。

回答by sakovias

A solution from http://vincesalvino.blogspot.ca/2013/08/git-empty-files-corrupt-objects-and.htmlallowed me to fix the problem:

来自http://vincesalvino.blogspot.ca/2013/08/git-empty-files-corrupt-objects-and.html的解决方案让我解决了这个问题:

find .git/objects/ -size 0 -exec rm -f {} \;

回答by Schwern

First, make a backup of your existing repository. cp -ror something. That way if your attempts to repair your repository screw it up worse you can restore.

首先,备份您现有的存储库。 cp -r或者其他的东西。这样,如果您尝试修复您的存储库搞砸了,您可以恢复。

Simplest thing to try is replacing that corrupt object file with a working one. If you have a backup of your repository, use that. Otherwise do a git clonefrom your remote repository to get a fresh copy and copy .git/objects/5c/deb9c3a1fe164cb4d2779d1e0d9d9f4ef18c6ainto your existing broken one. See if that fixes it.

最简单的尝试是用一个有效的目标文件替换那个损坏的目标文件。如果您有存储库的备份,请使用它。否则,git clone从您的远程存储库中获取一份新副本并复制.git/objects/5c/deb9c3a1fe164cb4d2779d1e0d9d9f4ef18c6a到您现有的损坏的副本中。看看能不能解决

回答by Will.

thanks for replying back. I ran that in the new cloned repo and returned that it unpacked 100% of the objects however they were not in .git/objects/pack of that repo.

感谢您的回复。我在新克隆的 repo 中运行它并返回它解压缩了 100% 的对象,但是它们不在该 repo 的 .git/objects/pack 中。

So, instead and tried something this morning which worked. 1. cloning my github repository into a separate, new directory. 2. copying the locally changed files (that I wanted to commit originally) into my new cloned repository and then pushed them to github. 3. nuked my old local repository and 4. cloned it again to the same file path that I had my old repository.

所以,相反,今天早上尝试了一些有效的方法。1. 将我的 github 存储库克隆到一个单独的新目录中。2. 将本地更改的文件(我最初想提交的)复制到我的新克隆存储库中,然后将它们推送到 github。3. nuked 我的旧本地存储库 4. 再次将其克隆到我拥有旧存储库的相同文件路径。

回答by Paul Weaver

Very similar to Schwerm's answer, but I had to init a new repo into which to unpack the .packfiles:

与 Schwerm 的回答非常相似,但我必须初始化一个新的存储库来解压.pack文件:

git clone <repo-uri> my_repo.fresh_clone
mkdir my_repo.newly_unpacked
cd !$
git init
for pack_file in ../my_repo.fresh_clone/.git/pack/*.pack; do
    git unpack-objects < $pack_file
done

Then I copied over the files from my_repo.newly_unpacked/.git/objects/<xx>/<sha1>as indicated by the error messages. I got caught out because a few operations, such as git checkoutrevealed more missing objects than a simple git status, so best to keep the restoration directories around for a bit before cleaning them up.

然后我my_repo.newly_unpacked/.git/objects/<xx>/<sha1>按照错误消息的指示复制了文件。我被抓住了,因为一些操作,例如git checkout显示比简单的更多丢失的对象git status,所以最好在清理它们之前将恢复目录保留一段时间。

回答by TooCooL

Simple answer to this question for anyone facing this problem: the git clonecommand is the fix, if have a remote repo then clone it to the local folder (after deleting the corrupted local repo), in case you dont have remote repo then try to push the corrupt repo to github and then clone it from there, I think that corrupted objects wont be pushed and it will fix the problem

对于任何面临这个问题的人来说,这个问题的简单答案是:git clone命令是修复程序,如果有远程存储库,则将其克隆到本地文件夹(删除损坏的本地存储库后),如果您没有远程存储库,请尝试将损坏的 repo 推送到 github,然后从那里克隆它,我认为不会推送损坏的对象,它将解决问题

回答by Vijay Kumar Kanta

I had a similar issue when the system crashed before doing a commit. Fortunately all I had to do was clone the full repo again to a new directory and discard the old one.

当系统在提交之前崩溃时,我遇到了类似的问题。幸运的是,我所要做的就是再次将完整的 repo 克隆到一个新目录并丢弃旧的。

Newly cloned repo didn't have the .git/objects/#num/#hash file that was corrupt.

新克隆的 repo 没有损坏的 .git/objects/#num/#hash 文件。