用 nslookup github.com 排查 Git 推送失败:DNS 原因分析与修复方案(Windows)
很多人遇到过这种情况:本地 git commit 正常,但一旦执行 git push 就报错,例如:
Could not resolve host: github.comFailed to connect to github.com port 443: Timed outOpenSSL SSL_connect: Connection was resetfatal: unable to access 'https://github.com/...': ...
这类问题里,DNS 解析异常是非常常见的一类根因。本文用 nslookup github.com 作为切入点,帮助你定位问题并给出可执行的修复方案。
1. 为什么 DNS 会导致 Git 推送失败?
当你执行 git push https://github.com/... 时,Git 通常需要完成:
- 解析
github.com的 IP(DNS) - 与该 IP 建立 TCP 连接(443)
- 进行 TLS 握手(证书校验)
- HTTP(S) 上传数据
其中 第 1 步 DNS 解析如果失败或返回“错误/不可达的 IP”,后面都会失败。
常见 DNS 问题包括:
- DNS 解析失败:直接报
Could not resolve host - 解析到不可达/异常的 IP:连接超时、连接被重置
- IPv6 优先导致走了不可用 IPv6 路径:解析到 AAAA,但网络实际不可达
- 本机/路由器 DNS 缓存异常:错误解析被缓存,导致长期失败
- 代理/安全软件拦截:影响解析或 TLS 建链(表现为 reset/timeout)
2. nslookup github.com 示例
在 Windows PowerShell 或 CMD 运行:
nslookup github.com
你可能看到类似输出(示例):
Server: UnKnown
Address: <你的DNS服务器IP>
Non-authoritative answer:
Name: github.com
Addresses: <IPv4地址1>
<IPv4地址2>
如果你想明确指定公共 DNS 做对比测试(更推荐):
nslookup github.com 1.1.1.1
示例输出(示例):
Server: one.one.one.one
Address: 1.1.1.1
Non-authoritative answer:
Name: github.com
Addresses: <IPv4地址1>
<IPv4地址2>
再测一个公共 DNS(Google):
nslookup github.com 8.8.8.8
3. 如何根据 nslookup 判断问题在哪?
3.1 直接解析失败
表现可能是:
*** can't find github.com: Non-existent domain
或超时:
DNS request timed out.
timeout was 2 seconds.
结论:当前 DNS 服务器不可用、被拦截、或网络无法访问该 DNS。
3.2 解析成功,但 git push 仍超时/重置
这类通常是:
- 解析到的线路不可达/不稳定
- TLS/代理/安全软件导致的握手问题
建议进一步验证连通性(不涉及 Git):
curl -I https://github.com
- 如果
curl也超时:更像是网络链路/DNS解析到的线路问题 - 如果
curl能访问但git push不行:更多是 Git 代理、证书、或 Git 配置问题
3.3 IPv6 相关(AAAA 优先导致失败)
有些环境会优先走 IPv6,导致访问 GitHub 异常。你可以分别看 A/AAAA 记录:
nslookup -type=AAAA github.com
nslookup -type=A github.com
如果 AAAA 有结果,但你的网络 IPv6 出口不可用,就可能出现 GitHub 访问不稳定。
4. 解决方案(从“最有效”到“备选方案”)
4.1 更换 DNS(最常见有效)
把你的网络适配器 DNS 改为稳定的公共 DNS(任选一组):
- Cloudflare:
1.1.1.1/1.0.0.1 - Google:
8.8.8.8/8.8.4.4
修改后,执行刷新 DNS 缓存:
ipconfig /flushdns
然后再次验证:
nslookup github.com
curl -I https://github.com
git ls-remote https://github.com/<owner>/<repo>.git
4.2 清理 Git 代理配置(很多人踩坑点)
查看 Git 是否配置了代理:
git config --global --get http.proxy
git config --global --get https.proxy
如不需要代理,取消:
git config --global --unset http.proxy
git config --global --unset https.proxy
如果你确实需要代理,确保代理地址可用且支持 HTTPS CONNECT,并且代理规则允许访问 GitHub 域名。
4.3 换成 SSH 推送(绕开部分 HTTPS/TLS/代理问题)
如果 HTTPS 经常不稳定,可以用 SSH:
- 配置 SSH key(略)
- 把 remote 改成 SSH:
git remote -v
git remote set-url origin git@github.com:<owner>/<repo>.git
git push origin main
说明:SSH 仍需要 DNS,但有时比 HTTPS 更少受系统证书/代理配置影响。
4.4 临时方案:写 hosts(不推荐长期使用)
你可以把 github.com 临时写入 hosts,但 GitHub IP 会变化,长期维护成本高,且可能导致更大问题。
如果只是救急,建议恢复网络后及时删除 hosts 记录。
5. 快速排查清单(Windows 命令合集)
# 1) DNS 解析
nslookup github.com
nslookup github.com 1.1.1.1
nslookup -type=A github.com
nslookup -type=AAAA github.com
# 2) 连通性验证
curl -I https://github.com
# 3) Git 侧验证(把仓库替换成你自己的)
git remote -v
git ls-remote https://github.com/<owner>/<repo>.git
# 4) 代理检查
git config --global --get http.proxy
git config --global --get https.proxy
# 5) 清理本机 DNS 缓存
ipconfig /flushdns
6. 常见“最终结论”总结(可直接放文章结尾)
- 如果
nslookup github.com失败:优先修 DNS(换 DNS +ipconfig /flushdns) - 如果
nslookup正常但curl -I https://github.com失败:网络链路/IPv6/代理/安全软件问题 - 如果
curl正常但git push失败:优先查 Git 代理、证书、remote URL(HTTPS/SSH)