Skip to content

Git 连接 GitHub 的问题解决方案 🔧 ​

在日常开发中,我们经常会遇到 Git 无法正常连接 GitHub 的情况,表现为连接超时、速度过慢或完全无法访问。本文将系统性地介绍问题原因和解决方案。

问题表现 ❌ ​

当执行 Git 操作时,可能会遇到以下错误:

bash
# SSH 连接超时
$ git clone git@github.com:username/repo.git
Cloning into 'repo'...
ssh: connect to host github.com port 22: Connection timed out
fatal: Could not read from remote repository.

# HTTPS 连接缓慢或失败
$ git clone https://github.com/username/repo.git
Cloning into 'repo'...
fatal: unable to access 'https://github.com/username/repo.git/': 
Failed to connect to github.com port 443: Connection timed out

核心概念理解 📚 ​

HTTPS vs SSH 协议 ​

💡 关键概念:Git 支持两种主要的传输协议,它们的工作方式完全不同

HTTPS 协议:

  • 使用 HTTP/HTTPS 端口(80/443)
  • 每次操作需要输入用户名和密码(或 Personal Access Token)
  • 适合临时使用或公司防火墙严格的环境

SSH 协议:

  • 使用 SSH 端口(默认 22)
  • 通过公钥私钥对进行身份验证
  • 配置一次后无需重复输入密码

Origin URL 的重要性 ​

bash
# 查看当前仓库的 origin URL
git remote -v

# HTTPS 方式克隆的仓库
# origin  https://github.com/username/repo.git (fetch)
# origin  https://github.com/username/repo.git (push)

# SSH 方式克隆的仓库  
# origin  git@github.com:username/repo.git (fetch)
# origin  git@github.com:username/repo.git (push)

💡 重要提示:origin URL 在 git clone 时确定,影响后续所有 git pull、git push、git fetch 操作的协议选择

解决方案 ✅ ​

方案一:SSH over HTTPS(推荐) 🌟 ​

这是最稳定可靠的解决方案,通过 443 端口使用 SSH 协议。

步骤 1:测试连通性

bash
# 测试 GitHub 的 SSH over HTTPS 是否可用
ssh -T -p 443 git@ssh.github.com

步骤 2:配置 SSH

bash
# Windows 用户
notepad %USERPROFILE%\.ssh\config

# Linux/Mac 用户  
vim ~/.ssh/config

步骤 3:添加配置

bash
# SSH over HTTPS 配置
Host github.com
    Hostname ssh.github.com
    Port 443
    User git

步骤 4:验证配置

bash
# 测试 SSH 连接
ssh -T git@github.com

# 成功输出示例:
# Hi username! You've successfully authenticated, but GitHub does not provide shell access.

方案二:配置 Git 代理 🌐 ​

适用于使用科学上网工具的用户。

针对 HTTPS 协议:

bash
# 设置 HTTP/HTTPS 代理(端口号根据你的代理软件调整)
git config --global http.proxy http://127.0.0.1:10809
git config --global https.proxy http://127.0.0.1:10809

# 验证配置
git config --global --get http.proxy
git config --global --get https.proxy

针对 SSH 协议:

在 ~/.ssh/config 文件中添加:

bash
Host github.com
    Hostname github.com
    Port 22
    User git
    # Windows 用户(确保路径正确)
    ProxyCommand "C:/Program Files/Git/usr/bin/nc.exe" -X 5 -x 127.0.0.1:7890 %h %p
    
    # Linux/Mac 用户
    # ProxyCommand nc -X 5 -x 127.0.0.1:7890 %h %p

💡 端口说明:

  • 10809:HTTP 代理端口(常见于 Clash、V2Ray)
  • 7890:SOCKS5 代理端口
  • 请根据你的代理软件实际端口调整

取消代理配置:

bash
# 取消 Git 代理
git config --global --unset http.proxy
git config --global --unset https.proxy

方案三:修改 Hosts 文件 📝 ​

当 DNS 解析有问题时使用此方案。

步骤 1:获取 GitHub IP

bash
# 查询 GitHub 当前 IP
nslookup github.com

# 或使用在线工具查询最新 IP
# https://www.ipaddress.com/website/github.com

步骤 2:修改 Hosts 文件

bash
# Windows: C:\Windows\System32\drivers\etc\hosts
# Linux/Mac: /etc/hosts

# 添加以下内容(IP 地址可能变化,请查询最新)
140.82.112.4 github.com
140.82.113.4 api.github.com
185.199.108.153 assets-cdn.github.com

步骤 3:刷新 DNS 缓存

bash
# Windows
ipconfig /flushdns

# Mac
sudo dscacheutil -flushcache

# Linux
sudo systemctl restart systemd-resolved

方案四:切换协议 🔄 ​

如果当前协议有问题,可以尝试切换到另一种协议。

bash
# 从 HTTPS 切换到 SSH
git remote set-url origin git@github.com:username/repo.git

# 从 SSH 切换到 HTTPS  
git remote set-url origin https://github.com/username/repo.git

# 验证修改
git remote -v

故障排查步骤 🔍 ​

1. 诊断当前状态 ​

bash
# 检查当前 origin URL
git remote -v

# 检查 Git 代理配置
git config --global --get http.proxy
git config --global --get https.proxy

# 测试网络连通性
ping github.com

2. 测试不同协议 ​

bash
# 测试 HTTPS 连接
curl -I https://github.com

# 测试 SSH 连接(端口 22)
ssh -T git@github.com

# 测试 SSH over HTTPS(端口 443)
ssh -T -p 443 git@ssh.github.com

3. 逐步尝试解决方案 ​

  1. 首选:SSH over HTTPS(方案一)
  2. 次选:配置代理(方案二)
  3. 备选:修改 Hosts(方案三)
  4. 最后:切换协议(方案四)

最佳实践建议 💡 ​

长期使用建议 ​

  1. 推荐使用 SSH 协议:配置一次,长期有效
  2. 优先选择 SSH over HTTPS:兼容性最好,最稳定
  3. 定期更新 SSH 密钥:提高安全性

团队协作建议 ​

bash
# 为不同项目配置不同的 SSH 密钥
# ~/.ssh/config
Host github-work
    Hostname github.com
    User git
    IdentityFile ~/.ssh/id_rsa_work

Host github-personal  
    Hostname github.com
    User git
    IdentityFile ~/.ssh/id_rsa_personal

# 使用时指定 Host
git clone git@github-work:company/repo.git

常见问题 FAQ ❓ ​

Q: 为什么配置了代理还是很慢? A: 检查代理端口是否正确,SSH 协议需要 SOCKS5 代理,HTTPS 协议需要 HTTP 代理。

Q: SSH 密钥配置正确但仍然连接失败? A: 尝试 SSH over HTTPS(443 端口),很多网络环境会阻止 22 端口。

Q: 如何在公司网络环境下使用? A: 优先尝试 HTTPS 协议 + HTTP 代理,或联系网络管理员开放相关端口。

参考资源 🔗 ​