错误信息

Mac 上 Git 提示“Could not resolve host: github.com”

Git 无法获取 github.com 的地址。对于 HTTPS 远程仓库,Git 显示 curl 的消息;对于 SSH 远程仓库,ssh 显示它自己的消息。

适用于:macOS 或 Homebrew 安装的 Git,所有当前 macOS 版本。文字已在 Git 2.56 和 OpenSSH 10.5 上核对。核查日期:2026年10月29日

症状

  • Git 显示 fatal: unable to access 'https://github.com/…/': Could not resolve host: github.com。
  • 使用 SSH 远程仓库时,ssh 显示 Could not resolve hostname github.com: nodename nor servname provided, or not known。
  • git clone、git pull 和 git push 全部失败。

可能的原因

  • 没有连接或没有 DNS。Mac 无法查询任何名称。
  • Git 没有使用代理。在需要代理的网络上,Git 需要 http.proxy 或代理环境变量。
  • VPN 或 DNS 过滤器。VPN 的 DNS 服务器或过滤器可能没有对 github.com 作出应答。
  • 远程 URL 错误。远程地址中的拼写错误会得到一个不存在的名称。

如何检查

  1. 显示仓库的远程 URL。

    git remote -v
  2. 通过 macOS 解析器查询 github.com。

    dscacheutil -q host -a name github.com
  3. 显示 Git 的代理设置和你的代理环境变量。

    git config --get http.proxy
    env | grep -i proxy
  4. 用 curl 测试 HTTPS 路径。

    curl -I https://github.com

如何修复

  1. 如果所有名称都查询失败,修复 DNS。参阅“Mac 上 DNS 无法解析”。

  2. 如果网络需要代理,为 Git 设置代理。

    git config --global http.proxy http://proxy.example.com:8080
  3. 如果旧的代理设置有误,将其删除。

    git config --global --unset http.proxy
  4. 如果远程 URL 有拼写错误,改正它。

    git remote set-url origin https://github.com/OWNER/REPO.git

使用 Kilobat

  1. Kilobat 的连接诊断逐层测试主机:网络、代理、DNS、规则、IPv4、IPv6、TLS 和服务器,并指出失败的那一层。

    kilobat why github.com
  2. Kilobat 的这些检查都是免费的。AI 智能体可以通过 MCP 用 diagnose_connection 和 diagnose_network 运行它们。请参阅找出某个网站或 API 连不上的原因。

常见问题

GitHub 宕机了吗?

有可能,但查询错误通常是本地问题。如果 dscacheutil 能查到其他名称却查不到 github.com,检查你的 DNS 或过滤器。

参考资料

  • curl:libcurl error codes
  • git-config(1),http.proxy,macOS 27.2 自带的 man 手册页(Git 2.56)
  • ssh(1) 和 dscacheutil(1),macOS 27.2 自带的 man 手册页

参考资料核查于 2026年10月29日。man 手册页为 macOS 自带版本;在“终端”中用 man <name> 打开。