0%

git突然无法连接到GitHub仓库

昨晚在上传博客时突然发现没有办法hexo d以为是hexo的_config.yml里做了什么修改导致上传失败,结果看了看报错发现是无法连接到Github的仓库,于是开始查找问题。

1
2
3
4
5
6
7
8
9
10
11
ssh: connect to host github.com port 22: Connection timed out
fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.
FATAL Something's wrong. Maybe you can find the solution here: https://hexo.io/docs/troubleshooting.html
Error: Spawn failed
at ChildProcess.<anonymous> (C:\Blog\node_modules\hexo-deployer-git\node_modules\hexo-util\lib\spawn.js:51:21)
at ChildProcess.emit (node:events:514:28)
at cp.emit (C:\Blog\node_modules\cross-spawn\lib\enoent.js:34:29)
at ChildProcess._handle.onexit (node:internal/child_process:294:12)

询问了ChatGPT可能的原因和解决方法,回答是:

  1. SSH 密钥未设置或配置错误:确保你已经生成了 SSH 密钥并添加到了 GitHub 账户。可以通过 ssh -T git@github.com 命令测试 SSH 连接。如果没有配置 SSH 密钥,需要生成一个新的密钥并添加到 GitHub。

  2. 网络问题:ssh: connect to host github.com port 22: Connection timed out 这一错误提示可能是因为网络连接问题,尤其是在某些网络环境下访问 GitHub 可能受限。考虑检查网络连接或使用代理。

  3. GitHub 仓库设置问题:确保你的 Hexo 配置文件中的仓库地址是正确的,并且你有权限访问该仓库。

  4. 防火墙或安全软件干扰:有时候本地的防火墙或安全软件可能阻止 SSH 连接。检查这些软件的设置,确保它们不会干扰到你的 SSH 连接。

  5. SSH 客户端配置问题:在某些情况下,SSH 客户端的配置可能导致连接问题。可以查看 ~/.ssh/config 文件(如果存在)来确认配置是正确的。

先是尝试第一步验证SSH公钥的时候失败,发现发现无法连接

1
2
$ ssh -T git@github.com
ssh: connect to host github.com port 22: Connection timed out

但明明自己可以正常访问github.com,代理设置也没问题,然后尝试了一下能否和github.com ping通

1
ping github.com

结果发现并不可以,显示请求超时

1
2
3
4
5
6
7
8
9
10
C:\Users\13781>ping github.com

正在 Ping github.com [192.30.255.112] 具有 32 字节的数据:
请求超时。
请求超时。
请求超时。
请求超时。

192.30.255.112 的 Ping 统计信息:
数据包: 已发送 = 4,已接收 = 0,丢失 = 4 (100% 丢失),

此时,尝试ping一下百度

1
2

ping baidu.com

结果可以ping通,说明网络是没有问题的

1
2
3
4
5
6
7
8
9
10
11
12
C:\Users\13781>ping baidu.com

正在 Ping baidu.com [110.242.68.66] 具有 32 字节的数据:
来自 110.242.68.66 的回复: 字节=32 时间=31ms TTL=53
来自 110.242.68.66 的回复: 字节=32 时间=30ms TTL=53
来自 110.242.68.66 的回复: 字节=32 时间=30ms TTL=53
来自 110.242.68.66 的回复: 字节=32 时间=30ms TTL=53

110.242.68.66 的 Ping 统计信息:
数据包: 已发送 = 4,已接收 = 4,丢失 = 0 (0% 丢失),
往返行程的估计时间(以毫秒为单位):
最短 = 30ms,最长 = 31ms,平均 = 30ms

之后网上搜了下问题,发现有的时候的确会出现明明之前可以正常使用,但突然无法connect到repositories,应该就是我遇到的问题,然后参考了这篇博客

解决方法:

  1. 以管理员身份运行记事本
  2. C:\Windows\System32\drivers\etc中找到host.txthost文件,用记事本打开
  3. 在结尾处添加以下代码并保存:
    1
    2
    3
    # github
    192.30.255.112 github.com git
    185.31.16.184 github.global.ssl.fastly.net
  4. 再次尝试ping,发现可以ping通
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    C:\Users\13781>ping github.com

    正在 Ping github.com [192.30.255.112] 具有 32 字节的数据:
    来自 192.30.255.112 的回复: 字节=32 时间=337ms TTL=46
    来自 192.30.255.112 的回复: 字节=32 时间=337ms TTL=46
    来自 192.30.255.112 的回复: 字节=32 时间=337ms TTL=46
    来自 192.30.255.112 的回复: 字节=32 时间=339ms TTL=46

    192.30.255.112 的 Ping 统计信息:
    数据包: 已发送 = 4,已接收 = 4,丢失 = 0 (0% 丢失),
    往返行程的估计时间(以毫秒为单位):
    最短 = 337ms,最长 = 339ms,平均 = 337ms

此时,我们回到开头的问题,无法连接到GitHub仓库,此时,我们随便git clone一个项目尝试一下

1
git clone https://github.com/xie-xyh/path_planning.git

发现成功clone

此时我们再次hexo三部曲

1
2
3
hexo clean
hexo g
hexo d

没有问题,博客重新上传,问题解决!

-------------本文结束感谢您的阅读-------------

欢迎关注我的其它发布渠道