三步走解决提交GitHub超时的问题
更新: 12/31/2025, 6:43:00 AM 字数: 0 字 时长: 0 分钟
问题分析
提交GitHub时经常会出现链接失败或者超时的问题,具体如下:
Failed to connect to github.com port 443 after 42079 ms:
Couldn't connect to server
完成时带有错误,见上文。一句话解决问题
① 使用 谷歌DNS 或 GitHub520更新Hosts文件中github域名对应的IP
② 改为使用SSH协议提交(不要使用HTTPS协议提交)
解决方案
检查网络连接
尝试nslookup github.com,看是否能解析到正确的IP地址。若返回的IP地址有问题,可以尝试更换DNS服务器。例如将DNS改为8.8.8.8或1.1.1.1,改完重试检查是否OK了。
Server: dns.google
Address: 8.8.8.8
Non-authoritative answer:
Name: github.com
Address: 20.205.243.166 配置完成后使用ping github.com测试是否可以获取可访问的GitHub的IP,若还不能访问就将DNS切换到另一个再尝试一下;若两个均不能获取,还可以参考这里。
检查代理设置
如果使用了代理,需要确认Git是否配置了正确的代理。可以通过以下命令查看Git的代理设置:
git config --global http.proxy
git config --global https.proxy 如果返回了某个代理地址,可能需要检查该代理是否可用。或者,尝试取消代理设置:
git config --global --unset http.proxy
git config --global --unset https.proxy 注意,除了Git的本身代理外还需检查Git工具(如sourceTree)、Win系统代理(如V2rayN) 中配置的代理。代理均检查完毕后,可以重试检查是否OK了。
改用SSH协议提交
若使用HTTPS协议提交还是可能报错的,建议切换到 SSH 协议可以绕过 HTTPS 端口限制(使用 22 端口)。首先,执行如下命令生成SSH密钥:
ssh-keygen -t ed25519 -C "your_email@example.com" 生成密钥的默认保存路径是~/.ssh/(如:C:\Users\Administrator\.ssh)
~/.ssh/
id_ed25519 私钥
id_ed25519.pub 公钥,注意这些需要将公钥复制到GitHub SSH Keys中 然后,在 GitHub 设置中添加 GitHub SSH Keys,将公钥复制到 GitHub,

然后,通过如下命令用SSH 协议连接远程服务器和服务并向它们验证。
① 用SSH 协议连接远程服务器和服务并向它们验证
ssh -T git@github.com
② 出现如下提示,需要输入yes
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
③ 最后,显示如下结果即表示配置成功
Hi xxx! You've successfully authenticated 确认配置成功后,修改远程仓库地址为 SSH,然后再次推送即可解决。
git remote set-url origin git@github.com:用户名/仓库名.git 若在使用Sourcetree提交时提示ssh密钥认证失败,点击 工具 > 选项 -> 一般,先将SSH客户端改为OpenSSH,然后在SSH密钥处选择刚生成的私钥即可。
附录
修改hosts文件
若配置DNS后还不能获得可访问的GitHubIP,可以在Windods下执行该脚本即可自动请求管理员权限,并更新GitHub域名对应的最新IP到Host文件。
Update-GitHubHosts.ps1
# 检查是否以管理员身份运行
if (-Not ([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator")) {
Write-Output "The script requires administrator privileges. Requesting administrator privileges..."
Start-Process powershell.exe "-NoProfile -ExecutionPolicy Bypass -File `"$PSCommandPath`"" -Verb RunAs
exit
}
# 定义GitHub提供的Hosts文件URL
$hostsUrl = "https://raw.githubusercontent.com/521xueweihan/GitHub520/main/hosts"
# 定义本地Hosts文件路径
$hostsPath = "$env:SystemRoot\System32\drivers\etc\hosts"
# 下载GitHub提供的Hosts文件内容
try {
$githubHostsContent = Invoke-WebRequest -Uri $hostsUrl -UseBasicParsing
} catch {
Write-Output "Failed to download GitHub hosts file: $_"
exit
}
# 构造新的Hosts区块(包含标记和最新内容)
$newBlock = "`n# GitHub Hosts Start`n$($githubHostsContent.Content)`n# GitHub Hosts End`n"
# 读取本地Hosts文件内容,处理可能的错误或空内容
try {
$localHostsContent = Get-Content -Path $hostsPath -Raw -ErrorAction Stop
} catch {
# 如果文件不存在或无法读取,初始化为空字符串
$localHostsContent = ""
}
# 正则表达式匹配旧的GitHub Hosts区块
$pattern = "(?s)# GitHub Hosts Start.*?# GitHub Hosts End"
if ($localHostsContent -match $pattern) {
# 替换旧区块为新内容
$updatedContent = $localHostsContent -replace $pattern, $newBlock
Write-Output "GitHub Hosts block has been updated."
} else {
# 确保内容不为null(兼容旧版PowerShell)
if ($null -eq $localHostsContent) {
$localHostsContent = ""
}
$updatedContent = $localHostsContent.TrimEnd() + "`n`n$newBlock"
Write-Output "New GitHub Hosts block has been added."
}
# 将更新后的内容写入Hosts文件
try {
$updatedContent | Set-Content -Path $hostsPath -Encoding ASCII -Force
} catch {
Write-Output "Failed to write to hosts file: $_"
exit
}
# 刷新DNS缓存
ipconfig /flushdns | Out-Null
Write-Output "DNS cache has been refreshed."
# 暂停并提示用户输入任意键继续
Write-Output "Press any key to continue..."
[void][System.Console]::ReadKey($true) 脚本执行完后后,提示如下,按任意键即可结束。
已成功刷新 DNS 解析缓存。
DNS cache has been refreshed.
Press any key to continue... 或可打开C:\Windows\System32\drivers\etc\hosts 与 这里 进行比对验证IP是否替换正确。
原版Hosts文件
# Copyright (c) 1993-2009 Microsoft Corp.
#
# This is a sample HOSTS file used by Microsoft TCP/IP for Windows.
#
# This file contains the mappings of IP addresses to host names. Each
# entry should be kept on an individual line. The IP address should
# be placed in the first column followed by the corresponding host name.
# The IP address and the host name should be separated by at least one
# space.
#
# Additionally, comments (such as these) may be inserted on individual
# lines or following the machine name denoted by a '#' symbol.
#
# For example:
#
# 102.54.94.97 rhino.acme.com # source server
# 38.25.63.10 x.acme.com # x client host
# localhost name resolution is handled within DNS itself.
# 127.0.0.1 localhost
# ::1 localhost
127.0.0.1 ieonline.microsoft.com