GitHub GHClone - 國內 GitHub 加速技能
適用場景
當使用者在國內網路環境遇到以下情況時啟用:
git clone https://github.com/...慢、卡住、Recv failure: 連接被對方重置、超時- 直連 GitHub 網頁 / API 不穩定或完全被牆(
curl https://github.com0 位元組) - 需要批量拉取 GitHub 倉庫、Release 檔案、raw 檔案
核心思路:把 github.com 的存取改走國內可直達的「加速前綴(mirror proxy)」,並對多個前綴做自動輪換 + 重試。單個前綴掛死時不卡死,自動換下一個。
一鍵安裝
# 把本倉庫 clone 下來(用任意可用前綴先拉本倉庫也行)
git clone https://gh-proxy.com/https://github.com/OWNER/github-ghclone.git
cd github-ghclone
bash scripts/install.sh
source ~/.bashrc # 讓 ~/bin 生效
安裝後得到:
~/bin/ghclone—— 可執行腳本(自動輪換 3 個前綴,每前綴重試 3 次)- git 全域靜態加速
url."https://gh-proxy.com/https://github.com/".insteadOf(普通git clone直接受益) - git 別名
gclone→ghclone
使用
# 方式一:直接呼叫腳本
ghclone https://github.com/owner/repo.git --depth 1
# 方式二:git 別名
git gclone https://github.com/owner/repo.git --depth 1
# 普通 git 命令(已被全域加速,無需別名)
git clone https://github.com/owner/repo.git
ghclone 會自動探測哪個前綴當前可用,成功即返回,全失敗才報錯。
手動配置(不想跑 install.sh 時)
- 下載腳本(本倉庫自帶
bin/ghclone),放到~/bin/並chmod +x。 - git 全域靜態加速(任選一個当前可用的前綴):
git config --global url."https://gh-proxy.com/https://github.com/".insteadOf "https://github.com/" - git 別名:
git config --global alias.gclone '!~/bin/ghclone' - PATH:確保
~/bin在PATH(追加到~/.bashrc):export PATH="$HOME/bin:$PATH"
更換 / 增刪前綴(某前綴掛死時)
加速前綴是社區維護的代理,時有時無,需要能輪換:
- 改
~/bin/ghclone(或本倉庫bin/ghclone)裡的PATS=(...)陣列,增刪前綴。 - 改 git 全域靜態前綴:
# 換成新的可用前綴 git config --global url."https://新前綴/https://github.com/".insteadOf "https://github.com/" # 清掉舊前綴 git config --global --unset url."https://舊前綴/https://github.com/".insteadOf
驗證前綴是否可用
對每個前綴跑一次真實小倉庫探測(返回 commit hash 即為可用,0 位元組/報錯為不可用):
for p in https://gh-proxy.com https://ghfast.top https://ghproxy.net; do
n=$(git ls-remote "$p/https://github.com/octocat/Hello-World.git" HEAD 2>/dev/null | head -1)
echo "$p -> ${n:-不可用}"
done
已知可用前綴(2026-10 實測,隨時會變)
| 前綴 | 用法 |
|---|---|
https://gh-proxy.com | https://gh-proxy.com/https://github.com/owner/repo.git |
https://ghfast.top | https://ghfast.top/https://github.com/owner/repo.git |
https://ghproxy.net | https://ghproxy.net/https://github.com/owner/repo.git |
直連
github.com在國內通常 0 位元組(被牆),務必走前綴。前綴服務不穩定,ghclone的重試邏輯就是為了應對這種「連接被對方重置」的瞬時故障。
局限與注意
- 前綴代理是第三方社區服務,可用性、速度、存續時間不可控;若全部失效,需尋找新前綴替換
PATS。 - 只加速
github.com的 git 協定存取;objects.githubusercontent.com(Release 大檔案)等子域同理可用前綴包一層。 - 合規提示:請確保使用符合當地法規的出網方式。