优化GitHub访问问题
问题
访问 GitHub 有时会遇到加载缓慢、连接超时甚至完全打不开的问题,这主要是由于 国际网络链路不稳定 和 DNS 污染/劫持 等原因造成的,并非 GitHub 被全面屏蔽(GitHub 主站是可访问的,但部分内容如 raw.githubusercontent.com 可能受影响)。
注意
⚠️ 一定要注意: 不能违反相关法律规定。
优化方法
以下是一些 合法、合规、无需使用非法代理工具 的优化方法,帮助你提升 GitHub 访问体验:
方法一:修改 Hosts 文件(推荐,免费有效)
GitHub 的部分 IP 地址在国内访问较稳定,可通过手动绑定 Hosts 加速。
步骤:
-
获取 GitHub 最新可用 IP
访问以下任一网站(国内可打开):
- https://github.com.ipaddress.com/
- https://fastly.net.ipaddress.com/github.global.ssl.fastly.net
- 或使用开源工具:https://github.com/521xueweihan/GitHub520(自动更新 Hosts)
-
编辑 Hosts 文件
- Windows:C:\Windows\System32\drivers\etc\hosts
- macOS / Linux:/etc/hosts
- 用管理员权限打开,添加如下内容(IP 可能会变,请以最新为准):
# GitHub Start20.205.243.166 github.com20.205.243.166 www.github.com185.199.108.153 raw.githubusercontent.com185.199.109.153 user-images.githubusercontent.com185.199.110.153 github.githubassets.com185.199.111.153 avatars.githubusercontent.com# GitHub End
- 刷新 DNS 缓存
- Windows:ipconfig /flushdns
- macOS:sudo dscacheutil -flushcache; sudo killall -HUP mDNSResponder
(效果:显著提升网页加载、图片显示、Raw 文件访问速度。)
方法二:使用国内镜像或加速服务(合法合规)
- GitHub 文件加速(用于下载 release / raw 文件)
- 使用 https://ghproxy.com/ 代理加速:
原始链接:https://github.com/user/repo/archive/refs/heads/main.zip
加速链接:https://ghproxy.com/https://github.com/user/repo/archive/refs/heads/main.zip
- Git 克隆加速
- 使用 FastGit(已停止维护,谨慎使用)或 JsDelivr CDN 加速 raw 内容:
# 将 raw.githubusercontent.com 替换为 cdn.jsdelivr.net/gh
# 例如:
https://cdn.jsdelivr.net/gh/user/repo@main/file.txt
⚠️ 注意:镜像服务仅用于公开内容,不要用于私有仓库或敏感操作。
方法三:配置 Git 使用 HTTPS 代理(可选,需合法网络环境)
如果你所在公司/学校有 合法的 HTTP 代理服务器(如企业内网代理),可配置 Git 使用:
# 临时设置(当前终端有效)
git config --global http.proxy http://proxy.company.com:8080
git config --global https.proxy http://proxy.company.com:8080# 取消代理
git config --global --unset http.proxy
git config --global --unset https.proxy
❌ 不建议使用非法代理工具,违反《中华人民共和国计算机信息网络国际联网管理暂行规定》。
方法四:使用国内代码托管平台作为备份
采用 双托管策略:
- 主仓库:GitHub(面向国际开源)
- 镜像仓库:Gitee、GitCode(国内访问快)
# 添加多个 remote
git remote add origin https://github.com/user/repo.git
git remote add gitee https://gitee.com/user/repo.git# 同时推送
git push origin main
git push gitee main
方法五:使用 CDN 或浏览器插件(辅助)
- 浏览器安装 GitHub520 插件(自动重写 Hosts):
https://github.com/521xueweihan/GitHub520
- 使用支持 DoH(DNS over HTTPS) 的 DNS 服务(如阿里 DNS 223.5.5.5)减少 DNS 劫持。
总结:推荐组合方案
- 修改 Hosts(最有效,免费,合法)
- 使用 ghproxy.com 下载大文件
- 关键项目在 Gitee 做镜像备份
- 浏览器安装 GitHub520 插件(可选)
ps
- 目前正在使用SwitchHosts软件