问题
最近在用 git 命令行工具 clone GitHub 上的 repo 时,经常会遇到这样的问题:
# Mac 上
fatal: unable to access 'https://github.com/**/**/': LibreSSL SSL_connect: SSL_ERROR_SYSCALL in connection to github.com:443
# Windows 上
fatal: unable to access 'https://github.com/**/**/': Failed to connect to github.com port 443: Timed out
解决办法
git 使用代理
遇到这种情况,需要为 git 命令行工具配置代理。假设你的 socks 代理本地端口为 1080,那么需要在命令行执行下面的命令:
git config --global http.proxy 'socks5://127.0.0.1:1080'
git config --global https.proxy 'socks5://127.0.0.1:1080'
这样再进行 clone、pull 之类的 git 操作,就不会报错了,问题解决!
注意,虽然 socks 端口一般为 1080,但还真不一定就是 1080。我最近用的一台 Mac 上,不知道为啥 socks 端口居然自动配到了 1086。我一开始按 1080 配的,所以一直没通,过了半天才发现问题。
那么如何判断当前的 socks 服务端口号是多少呢?Mac 和 Windows 下方法大同小异,简单来说就是在“设置”->“网络” ->“高级”->“代理” 菜单下,会有一些当前使用代理的信息,找找看就是了,在此就不赘述。
取消代理
但如果一直走代理也会有问题,比如我连接公司内网的 GitLab 就失败了。这时候就需要取消 git 的代理:
git config --global --unset http.proxy
git config --global --unset https.proxy
目前没找到更好的解决办法,只能是根据需要手动设置和取消代理。
备注
一开始我在网上搜到的解决方法是这样的:
git config --global http.proxy http://127.0.0.1:1086
git config --global https.proxy http://127.0.0.1:1086
主要的区别在于使用 http://
而不是socks5
。我记得这种方法之前在 Windows 机器上成功过,但最近我换了这台 Mac 之后不知道怎么就失败了,会报如下错误:
fatal: unable to access 'https://github.com/**/**/': Proxy CONNECT aborted
换成上面的方法就好了。