如何处理 Git“无法读取”错误

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

How to deal with Git "Could not read" error

git

提问by Fragsworth

I am getting this error in my git repository:

我在我的 git 存储库中收到此错误:

22:09:15 $ git status
# On branch master
error: Could not read 8124cc15c63be92d534e4cdfa33c38d54deee122
error: unable to read tree object HEAD
nothing to commit (working directory clean)

A Google search for error: unable to read tree object HEADdoesn't result in much help, this error seems to be very rare. I am not sure how to deal with it. Could it be a hard drive failure?

谷歌搜索error: unable to read tree object HEAD并没有带来太多帮助,这个错误似乎非常罕见。我不知道如何处理它。会不会是硬盘故障?

Edit: The output of git fsckis the following:

编辑:输出git fsck如下:

broken link from  commit 607328dc80e4901a55b95c683d4fbf43e6df28bf
              to    tree 8124cc15c63be92d534e4cdfa33c38d54deee122
missing tree 8124cc15c63be92d534e4cdfa33c38d54deee122
dangling tree 56b5d4a5e429d251582ec927bca7ef1225510c41
dangling tree 0259d2d38b18b6136bb6070fb41faf3624453cc6

采纳答案by VonC

On a "broken link" message, you could follow the GitFaqrecommendations:

在“断开的链接”消息中,您可以遵循GitFaq建议

  • back up all your state so that anything you do is re-doable if you corrupt things more!
  • explode any corrupt pack-files
    • See "man git-unpack-objects", and in particular the "-r" flag.
      Also, please realize that it only unpacks objects that aren't already available, so you need to move the pack-file away from its normal location first (otherwise git-unpack-objectswill find all objects that are in the pack-file in the pack-file itself, and not unpack anything at all)
  • replace any broken and/or missing objects
    • This is the challenging part.
      Sometimes (hopefully often!) you can find the missing objects in other copies of the repositories.
      At other times, you may need to try to find the data some other way (for example, maybe your checked-out copy contains the file content that when hashed will be the missing object?).
  • make sure everything is happy with "git fsck --full"
  • repack everything to get back to an efficient state again
  • 备份您的所有状态,以便在您破坏更多东西时可以重新执行您所做的任何事情!
  • 爆炸任何损坏的包文件
    • 请参阅“ man git-unpack-objects”,尤其是“ -r”标志。
      另外,请注意它只解包不可用的对象,因此您需要先将包文件从其正常位置移开(否则git-unpack-objects会在包文件本身中找到包文件中的所有对象,并且根本不解压任何东西)
  • 更换任何损坏和/或丢失的物体
    • 这是具有挑战性的部分。
      有时(希望经常!)您可以在存储库的其他副本中找到丢失的对象。
      在其他时候,您可能需要尝试以其他方式查找数据(例如,您的检出副本可能包含散列后将成为丢失对象的文件内容?)。
  • 确保一切都对“ git fsck --full”感到满意
  • 重新打包一切以再次回到有效状态


Notes:

笔记:

Update July 2016 (7 years laters), with Git 2.10 soon to be released, you now have:

2016 年 7 月更新(7 年后),随着 Git 2.10 即将发布,您现在拥有:

git fsck --name-objects

It helps namingthe origin of those broken links

它有助于命名那些断开链接的来源

See "How to fix git error broken link from tree to tree?" for more.

有关更多信息,请参阅“如何修复从树到树的 git 错误断开的链接?”。

回答by Tilman Vogel

I had a similar issue just now. The corruption arose when my laptop did a hard power-off during a git pull. I have a remote backup repository. First I had several object files in .git/objects/??/* that were zero size. After a cp -abackup of the repository, I did this:

我刚才有一个类似的问题。当我的笔记本电脑在git pull. 我有一个远程备份存储库。首先,我在 .git/objects/??/* 中有几个大小为零的目标文件。一个后cp -a库的备份,我这样做:

  • remove the zero length objects
  • clone the remote repository into a ../fresh/repository
  • in the broken repository, I did

    cat ../fresh/.git/objects/pack/pack-*.pack | git unpack-objects

  • 删除零长度对象
  • 将远程存储库克隆到../fresh/存储库中
  • 在损坏的存储库中,我做了

    cat ../fresh/.git/objects/pack/pack-*.pack | git unpack-objects

This filled up the missing objects in the object database. The repository seems to be back up now.

这填补了对象数据库中缺失的对象。存储库现在似乎已备份。

回答by Honza Pokorny

I had the same problem. After a lot of hair-pulling, I discovered that it was cause by changed permission to the repository's git files. I have solved it as follows:

我有同样的问题。经过一番摸索,我发现这是由于对存储库的 git 文件的权限更改造成的。我已经解决了如下:

$ cd .git
$ chmod 755 *

Done!

完毕!

回答by Rory O'Kane

I got a similar error in my Homebrewinstall's Git repository. Rather than restoring all the missing objects one by one, I found it easier to simply delete the .gitdirectory and create it again by re-cloning from Homebrew's public repository. These were my steps:

我在Homebrew安装的 Git 存储库中遇到了类似的错误。与其一一恢复所有丢失的对象,我发现简单地删除.git目录并通过从Homebrew 的公共存储库重新克隆来重新创建它更容易。这些是我的步骤:

  • Check what information you have in your Git repository that you won't get by just re-cloning. For me, it was private branches, stashes, and remotes.
    • Convert stashes into real commits by creating a new branch, applying the stash, and committing with something like “[WIP]” in the name to show that it's a stash.
    • Save branches that are not on the public remote by pushing them to a remote of your own. This could be fork of the repository on GitHub, or just a new Git repository in a different location on your machine.
    • If you have more than one remote, save the output of git remote -v, which contains the names and URLs of your remotes, so you can manually add them back later.
  • Delete your repoistory's .gitdirectory (or rename it to .git-brokenand delete it later). On the command-line, this is rm -rf .git.
  • Re-clone the remote directory with git clone https://github.com/Homebrew/homebrew.gitor whatever URI.
  • This will have created a new sub-folder homebrewnamed after the repository. You want just the .gitdirectory from that; your local files are already okay. So mv homebrew/.git .git, and then delete the homebrewfolder.
  • Your Git repository should have no errors, since you recreated it from scratch. Now just restore any information you saved in the first step.
    • If you had additional remotes, add them again with git remote add <name> <url>.
    • If you backed up any branches (or stashes converted to branches) to a remote repository, pull them from that repository to your local repository.
    • If you want, you can convert the stash-branches back to stashes by rolling the “[WIP]” commit with git reset HEAD^and saving the working directory to a stash again with git stash save <custom-message>.
  • 检查您的 Git 存储库中有哪些信息是通过重新克隆无法获得的。对我来说,它是私人分支、藏匿处和遥控器。
    • 通过创建一个新分支、应用存储并在名称中使用类似“[WIP]”之类的内容进行提交以表明它是一个存储,从而将存储转换为真正的提交。
    • 通过将它们推送到您自己的遥控器来保存不在公共遥控器上的分支。这可能是 GitHub 上存储库的分支,或者只是您机器上不同位置的新 Git 存储库。
    • 如果您有多个遥控器,请保存 的输出git remote -v,其中包含遥控器的名称和 URL,以便您稍后手动添加它们。
  • 删除存储库的.git目录(或将其重命名为.git-broken稍后删除)。在命令行上,这是rm -rf .git.
  • 使用git clone https://github.com/Homebrew/homebrew.gitURI 或任何 URI重新克隆远程目录。
  • 这将创建一个homebrew以存储库命名的新子文件夹。你只想要那个.git目录;你的本地文件已经没问题了。所以mv homebrew/.git .git,然后删除homebrew文件夹。
  • 您的 Git 存储库应该没有错误,因为您是从头开始重新创建它的。现在只需恢复您在第一步中保存的任何信息。
    • 如果您有其他遥控器,请使用git remote add <name> <url>.
    • 如果您将任何分支(或转换为分支的存储)备份到远程存储库,请将它们从该存储库拉到您的本地存储库。
    • 如果需要,您可以通过滚动“[WIP]”提交git reset HEAD^并使用git stash save <custom-message>.

If you run git fsck, you should see no errors:

如果你运行git fsck,你应该看不到任何错误:

$ git fsck
Checking object directories: 100% (256/256), done.
Checking objects: 100% (197135/197135), done.
Checking connectivity: 197162, done.
$

And git stash list, git branch, and git remote -vshould show the same output as before.

git stash list, git branch, 和git remote -v应该显示与以前相同的输出。

回答by Syntaxsizer

i fixed the error by making changes in the same directory/project folder and then tried to commit the new change , what happened is i got an error msg 'invalid object 100644 e38e910ceb18b09f436f353c3a131bfe2caba130 for 'Book/alise_mathe/app/src/main/res/menu/drawermenu.xml' this msg solved the problem , i just refactored the drawermenu.xml by changing the file name to 'drawer_menu.xml'. added the changes committed pushed and thats all. (android-studio)

我通过在同一个目录/项目文件夹中进行更改来修复错误,然后尝试提交新更改,发生的事情是我收到错误消息“无效对象 100644 e38e910ceb18b09f436f353c3a131bfe2caba130 for 'Book/alise_mathe/app/src/main menu/drawermenu.xml' 这个 msg 解决了这个问题,我只是通过将文件名更改为 'drawer_menu.xml' 重构了 drawermenu.xml。添加了提交的更改推送,仅此而已。(安卓工作室)

I hope this helps some how

我希望这有助于一些

回答by weaveoftheride

I fixed this error by deleting the Capistrano 'repo' folder from my remote server directory. I went through a number of the other suggested problems and resolved that the problem didn't lie with my local project. The problem appeared to occur when Capistrano was executing pulling from the repo to the remote. For me, this was perhaps due to a halted deployment that left corrupted objects / object references. My host also had just done a server migration, perhaps something became corrupted during this process.

我通过从远程服务器目录中删除 Capistrano 'repo' 文件夹来修复此错误。我经历了许多其他建议的问题并解决了问题不在于我的本地项目。当 Capistrano 执行从 repo 到远程的拉取时,问题似乎发生了。对我来说,这可能是由于停止部署导致对象/对象引用损坏。我的主机也刚刚完成了服务器迁移,可能在此过程中出现了某些损坏。

回答by Matt Brite

If you don't have uncommited changes the easiest solution is to delete the local branch: git branch -D [branch name]

如果您没有未提交的更改,最简单的解决方案是删除本地分支: git branch -D [branch name]

and then checkout again the remote branch: git checkout -b [branch name] origin/[branch name]

然后再次结帐远程分支: git checkout -b [branch name] origin/[branch name]