我们在使用 Git for Windows 时可能遇到的 git clone
连接超时问题(错误信息:Failed to connect to github.com port 443 after 21110 ms
),以下是综合多个可靠来源的解决方案,按优先级排序:
1. 检查网络与防火墙
- 验证网络连通性:
- 运行ping www.github.com,若返回超时(100% 丢失),则是 DNS 解析或网络路由问题。
- 浏览器访问
https://github.com
,若失败则需解决网络限制(如企业防火墙、VPN干扰)。
- 关闭防火墙/安全软件:临时禁用 Windows 防火墙、杀毒软件或企业级安全工具,测试是否被拦截。
2. 更新 Hosts 文件(解决 DNS 解析失败)
获取 GitHub 最新 IP:
访问IP 查询网站获取当前有效的 IP(如140.82.113.3)。
修改 Hosts 文件:
路径:
C:\Windows\System32\drivers\etc\hosts
以管理员权限编辑,末尾添加:
140.82.113.3 github.com 151.101.1.194 github.global.ssl.fastly.net
保存后运行
ipconfig /flushdns
刷新 DNS 缓存。
3. 配置代理或取消代理
若使用代理:
git config --global http.proxy http://127.0.0.1:7890 # 替换为你的代理端口 git config --global https.proxy http://127.0.0.1:7890
若未使用代理:清除可能残留的代理设置:
git config --global --unset http.proxy git config --global --unset https.proxy
4. 改用 SSH 协议(绕过 HTTPS 限制)
生成并添加 SSH 密钥:
ssh-keygen -t rsa -b 4096 -C "your_email@example.com" # 生成密钥 cat ~/.ssh/id_rsa.pub # 复制公钥
将公钥添加到GitHub SSH Keys。
使用 SSH 克隆:
git clone git@github.com:Kitware/ITK.git
5. 其他关键措施
切换 Git SSL 后端(解决证书问题):
git config --global http.sslBackend schannel # 使用 Windows 系统证书库
更新 Git 版本:从Git 官网下载最新版,修复旧版兼容性问题。
浅克隆或下载 ZIP:
git clone --depth 1 https://github.com/Kitware/ITK.git # 仅克隆最近提交
或直接在 GitHub 页面点击 Code → Download ZIP。
6.故障排查流程图
7.总结
以上方法覆盖了 90% 的 Git 连接超时场景。推荐优先尝试 SSH 协议(步骤4),因其不受 HTTPS 端口限制
。若问题持续,请检查 GitHub 服务状态(https://www.githubstatus.com)或联系网络管理员。