5.0 KiB
5.0 KiB
Git 网络连接问题排查指南
问题:fatal: unable to access 'https://github.com/...': Recv failure: Connection was reset
这是常见的网络连接问题,通常出现在中国大陆访问 GitHub 时。
解决方案
方案一:配置 Git 代理(推荐)
如果你有可用的代理(VPN/SSR/V2Ray),可以配置 Git 使用代理:
设置 HTTP/HTTPS 代理
# 设置全局代理(所有 Git 操作都使用代理)
git config --global http.proxy http://127.0.0.1:7890
git config --global https.proxy http://127.0.0.1:7890
# 仅针对 GitHub 设置代理
git config --global http.https://github.com.proxy http://127.0.0.1:7890
git config --global https.https://github.com.proxy http://127.0.0.1:7890
# 查看代理配置
git config --global --get http.proxy
git config --global --get https.proxy
常见代理端口:
- Clash:
7890 - V2Ray:
10808 - SSR:
1080 - 其他:根据你的代理软件设置
取消代理设置
# 取消全局代理
git config --global --unset http.proxy
git config --global --unset https.proxy
# 取消 GitHub 特定代理
git config --global --unset http.https://github.com.proxy
git config --global --unset https.https://github.com.proxy
方案二:使用 SSH 代替 HTTPS
SSH 连接通常比 HTTPS 更稳定:
1. 生成 SSH 密钥
# 生成 SSH 密钥对
ssh-keygen -t ed25519 -C "your_email@example.com"
# 按 Enter 使用默认路径
# 可以设置密码或直接按 Enter(不设置密码)
2. 添加 SSH 公钥到 GitHub
# 查看公钥内容
cat $env:USERPROFILE\.ssh\id_ed25519.pub
# 复制公钥内容,然后:
# 1. 访问 https://github.com/settings/keys
# 2. 点击 "New SSH key"
# 3. 粘贴公钥内容
# 4. 点击 "Add SSH key"
3. 测试 SSH 连接
# 测试 SSH 连接
ssh -T git@github.com
# 如果看到 "Hi bujie9527! You've successfully authenticated..." 说明成功
4. 更改远程仓库 URL 为 SSH
# 更改远程 URL 为 SSH
git remote set-url origin git@github.com:bujie9527/wecom-ai-assistant.git
# 验证更改
git remote -v
# 再次推送
git push -u origin main
方案三:使用 GitHub 镜像(临时方案)
可以使用 GitHub 镜像站点:
# 使用镜像站点(不推荐,仅临时使用)
git remote set-url origin https://github.com.cnpmjs.org/bujie9527/wecom-ai-assistant.git
# 推送后改回原地址
git push -u origin main
git remote set-url origin https://github.com/bujie9527/wecom-ai-assistant.git
方案四:增加超时和重试
# 增加 Git 超时时间
git config --global http.postBuffer 524288000
git config --global http.lowSpeedLimit 0
git config --global http.lowSpeedTime 999999
# 使用重试机制推送
git push -u origin main --verbose
方案五:使用 GitHub CLI(gh)
如果安装了 GitHub CLI:
# 安装 GitHub CLI(如果还没有)
# 下载:https://cli.github.com/
# 登录
gh auth login
# 推送代码
git push -u origin main
快速诊断
检查网络连接
# 测试 GitHub 连接
Test-NetConnection github.com -Port 443
# 测试 DNS 解析
nslookup github.com
# 使用 curl 测试
curl -I https://github.com
检查 Git 配置
# 查看所有 Git 配置
git config --list
# 查看远程仓库配置
git remote -v
# 查看当前分支
git branch
推荐方案
最佳实践(按优先级):
-
配置代理(如果你有 VPN/代理)
git config --global http.https://github.com.proxy http://127.0.0.1:7890 git config --global https.https://github.com.proxy http://127.0.0.1:7890 -
使用 SSH(更稳定,一次配置长期使用)
# 生成 SSH 密钥并添加到 GitHub # 然后更改远程 URL git remote set-url origin git@github.com:bujie9527/wecom-ai-assistant.git -
重试推送(网络临时问题时)
git push -u origin main
常见错误和解决方案
错误 1:SSL certificate problem
# 临时忽略 SSL 验证(不推荐,仅测试用)
git config --global http.sslVerify false
错误 2:Authentication failed
# 清除缓存的凭据
git credential-manager-core erase
# 或
git credential reject https://github.com
# 重新推送时会提示输入用户名和 token
错误 3:Connection timed out
- 检查防火墙设置
- 检查代理配置
- 尝试使用 SSH
验证推送成功
推送成功后,访问以下 URL 验证:
- 仓库地址:https://github.com/bujie9527/wecom-ai-assistant
- 提交历史:https://github.com/bujie9527/wecom-ai-assistant/commits/main
获取帮助
如果以上方案都无法解决:
- 检查 GitHub 状态:https://www.githubstatus.com/
- 查看 Git 日志:
git push -u origin main --verbose - 提交 Issue:https://github.com/bujie9527/wecom-ai-assistant/issues