GitHub 是全球最大的代码托管平台,但在国内访问经常抽风:克隆仓库几 KB/s、下载 Release 文件直接超时、图片加载不出来。这篇文章把 GitHub 加速的常见方法全部整理出来,从最简单到最彻底,按需选择。
一、为什么 GitHub 那么慢
GitHub 的服务器主要在海外,国内直连的线路质量差,加上部分 CDN 节点被限制,导致三大典型问题:
git clone速度极慢(几 KB/s 甚至卡死);- Release 二进制文件(.zip、.tar.gz)下载失败;
- raw 文件、图片等静态资源加载不出来。
不同问题用不同的加速方案,下面按场景分类。
二、最简单:修改 Hosts 加速
GitHub 慢的很大原因是 DNS 解析到了被污染的节点,手动指定最快的 IP 就能明显提速。
先用工具查到当前最快的 IP:
# 在本地 Windows/Linux 上运行
nslookup github.com
nslookup github.global.ssl.fastly.net
nslookup raw.githubusercontent.com
也可以直接用网上的 hosts 工具(如 GitHub520)自动生成:
# 下载自动生成的 hosts(Windows 管理员运行)
curl -o hosts https://raw.hellogithub.com/hosts
把解析出的 IP 写进 hosts 文件:
# Linux/Mac:/etc/hosts
# Windows:C:\Windows\System32\drivers\etc\hosts
140.82.121.3 github.com
151.101.1.194 github.global.ssl.fastly.net
185.199.108.133 raw.githubusercontent.com
刷新 DNS 缓存后重新测试:
# Windows
ipconfig /flushdns
# Linux
sudo systemd-resolve --flush-caches
这种方法零成本、立即生效,但 IP 会变,需要隔段时间更新一次。
三、换源加速 git clone
方案 1:用国内镜像仓库
很多仓库在 Gitee(码云)有镜像,克隆前先搜一下,Gitee 国内速度飞快:
git clone https://gitee.com/镜像仓库地址
Gitee 还支持"从 GitHub 导入仓库":在 Gitee 新建仓库时选择"从 GitHub 导入",填入原仓库地址即可,之后克隆 Gitee 上的镜像。
方案 2:ghproxy 代理加速
ghproxy 是专门的 GitHub 代理加速服务,支持 clone 和 Release 下载:
# 克隆仓库
git clone https://ghproxy.net/https://github.com/用户名/仓库名.git
# 下载 Release 文件
wget https://ghproxy.net/https://github.com/用户名/仓库名/releases/download/v1.0/xxx.zip
常用可用的 ghproxy 前缀地址(失效就换):
https://ghproxy.net/
https://gh-proxy.com/
https://ghfast.top/
https://mirror.ghproxy.com/
方案 3:git 配置代理
如果你有可用的代理/VPN,直接在 git 里配置:
# HTTP 代理
git config --global http.proxy http://127.0.0.1:7890
git config --global https.proxy http://127.0.0.1:7890
# 或 SOCKS5 代理
git config --global http.proxy socks5://127.0.0.1:1080
# 取消代理
git config --global --unset http.proxy
git config --global --unset https.proxy
四、Release 大文件下载加速
GitHub Release 下载慢是最常见的痛点,几个实用方案:
1. 用 ghproxy 代理(推荐,最简单)
在下载链接前面拼上代理地址即可,无需任何安装。
2. 用国内镜像加速站
jsDelivr 可以加速 GitHub 仓库里的文件(但加速不了 Release):
https://cdn.jsdelivr.net/gh/用户名/仓库名@分支/文件路径
3. 下载时开多线程工具
Windows 上用 IDM(Internet Download Manager)或 Motrix,支持多线程下载,配合代理前缀效果很好:
# Motrix 直接粘贴 ghproxy 处理过的链接即可
五、加速 raw 文件与图片
写 Markdown 时引用的 GitHub 图片、raw 文件加载不出来,用 jsDelivr CDN 替换:
原始地址:
https://raw.githubusercontent.com/用户名/仓库名/main/图片.png
替换为:
https://cdn.jsdelivr.net/gh/用户名/仓库名@main/图片.png
也可以把仓库部署到 Gitee Pages,或用 Cloudflare Workers 做反代。
六、终端与工具类加速
1. pip 从 GitHub 装包慢
pip install git+https://github.com/xxx/yyy
# 换成
pip install git+https://ghproxy.net/https://github.com/xxx/yyy
2. Docker 从 GitHub 拉不到构建文件
Docker 构建时引用 GitHub 文件,可以先用 ghproxy 下载到本地再 COPY 进镜像。
3. 浏览器访问 GitHub 网页卡
修改 hosts 是最直接的办法;也可以用 SwitchyOmega 配合代理规则,让 github.com 走代理、其他流量直连。
七、常见问题排查
1. 提示 unable to access ... Failed to connect
网络到 GitHub 不通,试试换 hosts 或换 ghproxy 前缀。
2. clone 中途卡住/断线
用 --depth 1 只拉最新一次提交,减少数据量:
git clone --depth 1 https://github.com/用户名/仓库名.git
或者用 SSH 协议试试:
git clone git@github.com:用户名/仓库名.git
3. 代理前缀提示 404
确认链接格式正确,前缀后面必须跟完整的 https://github.com/... 路径,且确认代理服务本身可用(有的前缀域名不稳定,多换几个)。
4. Release 下载返回 403/429
可能是镜像站限流,换个时间或换一个镜像站。
总结
GitHub 加速按场景选方案:
- 网页访问卡 → 改 hosts;
- git clone 慢 → ghproxy 代理前缀或 Gitee 镜像;
- Release 大文件下载 → ghproxy 前缀 + 多线程下载工具;
- raw 文件/图片 → jsDelivr CDN 替换。
优先级最高的万能方案是 ghproxy 代理前缀:不用装任何软件,在原始链接前拼一个前缀就能同时解决 clone 和下载问题。记得收藏几个备用的代理前缀,一个失效马上换下一个。
