How to remove the shallow clone warning from HomeBrew

120 阅读3分钟

在一次我学习的过程中,我需要在本机(MacOS)中安装Nginx,常用的方法有:

  • 使用 Homebrew 安装
  • 从源代码编译安装
  • 用预编译二进制文件
  • 使用MacPorts安装

但我的电脑在之前就已经无法正常使用Homebrew了,每次使用就会报错: homebrew-core is a shallow clone.,要我重新去github仓库克隆:git -C /usr/local/Homebrew/Library/Taps/homebrew/。然而每次按照它的方式去运行,都会很久没有反应,最终不耐烦之下就选择去尝试其他的解决方法来绕过此问题。

Error:  
homebrew-core is a shallow clone.  
homebrew-cask is a shallow clone.

To brew update, first run:  
git -C /usr/local/Homebrew/Library/Taps/homebrew/homebrew-core fetch --unshallow  
git -C /usr/local/Homebrew/Library/Taps/homebrew/homebrew-cask fetch --unshallow

但这次却绕不过去了,我尝试了其他方法都没成功(虽然使用MacPorts成功安装,但是因文件权限问题,后续十分令人头疼,无奈作罢。),最终只能回来面对这个问题。

我习惯性地向ChatGPT寻求帮助,折腾了很多时间,尝试了它所提供的种种问题,都无法解决。无奈之下我只能自己亲自下场。我打开stackoverflow去检索解决方案。最终在这篇帖子中找到了解决办法。其中写道:

somehow the unshallow is not working for me, I wait like 10 mins but nothing show up, even with -v option.Just found one possible solution that might working(at least working for me). You can remove the repository and clone again to get latest one.

cd /usr/local/Homebrew/Library/Taps/homebrew/rm -rf homebrew-core
git clone github.com/Homebrew/ho…

same command for homebrew-cask if you need to update that too all the credits belong to this answer

评论区有一条评论写道:

"It works. It is also slow but at least we can see the progress ."

这个方法的思路就是直接进入本地Homebrew的目录,移除原来浅克隆的homebrew-core,再从github克隆。这样看起来貌似跟之前的方法没有太大区别,我尝试之后也依然报错,但相比于之前的卡住与没有任何反应,这次清晰地告诉了原因(不知道为什么,很玄学):

致命错误:无法访问 'https://github.com/Homebrew/homebrew-core.git/':OpenSSL SSL_read: SSL_ERROR_SYSCALL, errno 0。

这个错误表明:在尝试从GitHub克隆homebrew-core仓库时出现了SSL连接问题。紧接着我在与ChatGPT的交流中获得了一种解决方法:试使用SSH协议而不是HTTPS协议进行克隆操作。

这篇帖子记载了如何从本机获取SSH密钥,并添加到自己的Github账户中。

  • 第一步,终端运行:ssh-keygen -t rsa -b 4096 -C "your_email@example.com"替换 your_email@example.com为 GitHub 帐户中注册的电子邮件地址。一路回车接受默认值。
  • 第二步,终端运行:cat ~/.ssh/id_rsa.pub,查看生成的公钥文件的内容,并复制。
  • 第三步,添加到自己的Github账户中,详细步骤这篇文章中都有,要值得注意的是在选择Key Type的时候要选择:Authentication Key,不然会报错。

经过这三步,就成功地将SSH密钥添加到了自己的Github账户中。然后重新进入目录中,运行:git clone git@github.com:Homebrew/homebrew-core.git。使用SSH方式从Github进行克隆。终于成功!