解决git问题:fatal: unable to access ‘https:..‘: Failed to connect to ..Could not connect to server
目录
方案 1:清除 / 重置 Git 代理配置(最常见解决方式)
一、核心原因分析
代理配置异常:本地开启了代理(如梯子、公司代理),但 Git 未配置代理,或代理配置错误;DNS 解析失败:本地 DNS 缓存过期 / 污染,导致无法解析 github.com 域名;网络 / 防火墙限制:本地防火墙、杀毒软件、公司 / 校园网络屏蔽了 443 端口;GitHub 服务器临时故障:极少情况,可通过 GitHub Status 验证;HTTPS 协议限制:部分网络环境对 HTTPS 协议的 443 端口做了拦截。
二、解决方案
方案 1:清除 / 重置 Git 代理配置(最常见解决方式)
很多开发者开启了代理但未同步配置到 Git,或代理配置错误,是导致 443 报错的首要原因。
步骤 1:查看当前 Git 代理配置
# 查看 Git 全局代理配置 git config --global --get http.proxy git config --global --get https.proxy步骤 2:清除错误的代理配置
如果代理配置错误 / 不需要代理,直接清除:
# 清除 http/https 代理 git config --global --unset http.proxy git config --global --unset https.proxy步骤 3:配置 Git 全局代理
# 方案1:SOCKS5代理(推荐,兼容性更好) git config --global http.https://github.com.proxy socks5://127.0.0.1:7891 git config --global https.https://github.com.proxy socks5://127.0.0.1:7891 # 方案2:HTTP代理(若SOCKS5不行) 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 方案 2:切换 SSH 协议
GitHub 的 HTTPS 协议易被网络拦截,SSH 协议更稳定,且无需频繁验证身份。
步骤 1:生成 / 配置 SSH 密钥(首次使用需操作)
1.打开终端,执行密钥生成命令(替换为你的 GitHub/Gitee 邮箱):
# 推荐ED25519算法(更安全) ssh-keygen -t ed25519 -C "[email protected]" # 若系统不支持ED25519,改用RSA ssh-keygen -t rsa -b 4096 -C "[email protected]"- 执行后全程按回车(默认路径、无密码),密钥会生成在:
- Windows:
C:\Users\你的用户名\.ssh\id_ed25519.pub(或 id_rsa.pub) - Mac/Linux:
~/.ssh/id_ed25519.pub(或 id_rsa.pub)
- Windows:
2.复制公钥内容:
- Windows(Git Bash):
cat ~/.ssh/id_ed25519.pub,复制输出的全部内容; - Mac/Linux:
pbcopy < ~/.ssh/id_ed25519.pub(直接复制到剪贴板)。
3.将公钥配置到远程仓库平台:
- GitHub:登录后 → 右上角头像 →
Settings→SSH and GPG keys→New SSH key→ 粘贴公钥 →Add SSH key; - Gitee:登录后 → 个人设置 →
SSH公钥→ 粘贴公钥 → 确定。
步骤 2:修改仓库远程地址为 SSH 格式
| 协议类型 | 地址示例 |
|---|---|
HTTPS |
|
SSH |
|
SSH |
|
若已克隆仓库:修改本地仓库的远程地址:
# 进入仓库目录 cd 你的仓库名 # 查看当前远程地址 git remote -v # 修改为SSH地址 git remote set-url origin [email protected]:xxx/xxx.git若未克隆仓库:直接用 SSH 地址克隆:
git clone [email protected]:xxx/xxx.git