Post

GitHub无法连接的完全解决方案

GitHub无法连接的完全解决方案

0 前言

  1. 尽量保持在同一系统环境中使用Git,否则GitHub公钥/私钥、Git用户名/邮箱、代理、https/ssh策略等可能需要重复配置。
  2. 如果项目必须在Linux中编译运行,建议使用虚拟机中的Ubuntu 22.04,因为实测在WSL2和Ubuntu上编译运行存在差异,有时前者编译报错(如CLOCK_MONOTONIC问题,Windows不支持POSIX)。

1 port 22: Connection timed out

1
2
3
4
5
6
user@thinkzh:~/digitzh.github.io$ git push
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.

这是最常见的问题。以下是完全的解决方案:

1.1 检查用户名和邮箱

要查看当前的Git用户名和邮箱地址,可以使用以下命令:

1
2
git config user.name
git config user.email

如果需要修改用户名和邮箱地址,可以使用以下命令进行全局设置:

1
2
git config --global user.name "新用户名"
git config --global user.email "新邮箱地址"

1.2 检查公钥

GitHub - Settings - Access - SSH and GPG keys,查看是否配置了公钥。如果没有,在终端使用ssh-keygen命令生成SSH密钥。在Windows中,在C:\Users\用户名\.ssh目录下生成id_ed25519.pub文件;在Linux中,在~/.ssh目录下生成id_ed25519.pub文件。打开文件,复制其中的公钥内容,在GitHub - SSH and GPG keys - New SSH Key中添加SSH密钥。

1.3 修改端口

如果端口 22 被阻止,可以尝试使用端口 443 来连接。

1
ssh -T -p 443 git@ssh.github.com

如果显示以下信息,说明连接成功:

1
Hi <username>! You've successfully authenticated, but GitHub does not provide shell access.

修改 SSH 配置文件来永久使用端口443。编辑sudo vim ~/.ssh/config(Windows下在~/.ssh目录创建config文件),添加以下内容:

1
2
3
Host github.com
Hostname ssh.github.com
Port 443

保存并关闭文件,然后再次尝试连接:

1
ssh -T git@github.com

如果提示Hi <username>! You've successfully authenticated, but GitHub does not provide shell access.,说明连接成功,即可执行Git操作。

2 ServicePointManager 不支持具有 socks5 方案的代理

1
2
3
fatal: ServicePointManager 不支持具有 socks5 方案的代理。
Username for 'https://github.com': digitzh
Password for 'https://digitzh@github.com':

因为GitHub已经取消了终端密码登录,所以在登录之前可以申请一个token,密码这里填token即可 申请方法:GitHub右上角Settings - 左侧最下方Developer Settings - Personal access tokens (classic),权限设置repo和workflow即可

3 Missing or invalid credentials.

参考链接

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
user@user:~$ git push
Missing or invalid credentials.
Error: connect ECONNREFUSED /run/user/1000/vscode-git-e661ea07d7.sock
    at PipeConnectWrap.afterConnect [as oncomplete] (node:net:1611:16) {
  errno: -111,
  code: 'ECONNREFUSED',
  syscall: 'connect',
  address: '/run/user/1000/vscode-git-e661ea07d7.sock'
}
Missing or invalid credentials.
Error: connect ECONNREFUSED /run/user/1000/vscode-git-e661ea07d7.sock
    at PipeConnectWrap.afterConnect [as oncomplete] (node:net:1611:16) {
  errno: -111,
  code: 'ECONNREFUSED',
  syscall: 'connect',
  address: '/run/user/1000/vscode-git-e661ea07d7.sock'
}
remote: [session-5c415197] Unauthorized
fatal: Authentication failed for 'https://gitee.com/digitzh/ComoFramework.git/'
  1. Ctrl+,打开设置,输入git.terminalAuthentication,取消勾选复选框
  2. Ctrl+Shift+P,输入Reload Window,重启VSCode窗口
This post is licensed under CC BY 4.0 by the author.