Github 倉庫 git clone 速度過慢解決方法!


很多時候想從 GitHub 上 clone 一個倉庫,都會遇到速度慢的問題,而且經常連接失敗,這里給出有效解決方案。

一、背景

應該是很多小伙伴碰到過的問題:想從 GitHub 上面 clone 項目,很多情況下會慢的離譜,等待好久后報錯:

fatal: early EOF
fatal: the remote end hung up unexpectedly
fatal: index-pack failed
error: RPC failed; curl 18 transfer closed with outstanding read data remaining

今天又遇到了這個問題了,折騰之后,總結出最為有效的解決方案,同時整理下網上最常見的幾種答案。

二、git 設置代理模式(親測有效)

需要用到幾個命令。

1. 設置代理

全局代理:

bash
# git config --global http.proxy http://127.0.0.1:1081 # git config --global https.proxy https://127.0.0.1:1081 # 實測后,用下面這條就能實現加速 clone 的效果,且能避開一些設置證書的坑 git config --global http.proxy 127.0.0.1:1081 

局部代理,在 github clone 的倉庫內執行:

bash
# git config --local http.proxy http://127.0.0.1:1081 # git config --local https.proxy https://127.0.0.1:1081 # 實測后,用下面這條就能實現加速 clone 的效果,且能避開一些設置證書的坑 git config --local http.proxy 127.0.0.1:1081 

只對 github 進行代理,對國內的倉庫不影響:

bash
git config --global http.https://github.com.proxy https://127.0.0.1:1081 git config --global https.https://github.com.proxy https://127.0.0.1:1081 # 實測后,用下面這條就能實現加速 clone 的效果,且能避開一些設置證書的坑 git config --global http.https://github.com.proxy 127.0.0.1:1081 

2. 查詢是否代理

查詢當前的 git 環境是否使用了代理。

查詢全局代理:

bash
git config --global http.proxy git config --global https.proxy 

查詢局部代理:

bash
git config --local http.proxy git config --local https.proxy 

查詢對 github 進行的代理:

bash
git config --global http.https://github.com.proxy git config --global https.https://github.com.proxy 

3. 取消代理

取消當前 git 環境使用的代理,恢復直連模式。

取消全局代理:

bash
git config --global --unset http.proxy git config --global --unset https.proxy 

取消局部代理:

bash
git config --local --unset http.proxy git config --local --unset https.proxy 

取消對 github 進行的代理:

bash
git config --global --unset http.https://github.com.proxy git config --global --unset https.https://github.com.proxy 

4. 注意代理端口

要注意的是,上面的 127.0.0.1:1081 這個地址是我自己的代理地址,每個人都需要查看自己的端口是不是也是 1081,同時也要區分 socks 端口和 http 端口,因為我這里主要是用的 https 方式來 clone GitHub 項目。

三、復制項目到碼雲(比較麻煩)

如果沒有代理,這也是一種有效的方法,缺點是步驟太麻煩。

1. 注冊碼雲

碼雲(Gitee)是個基於 Git 的代碼托管和研發協作平台,簡單理解就是國產的 GitHub,具體的注冊方式見官網

2. 項目導入碼雲

如果你要在 GitHub 上 clone 的項目是別人的,就先要將這個項目 fork 到自己的 GitHub 賬號下。

打開碼雲,通過「導入 GitHub 倉庫」功能,將 GitHub 上的項目復制到碼雲。

導入完成后再從碼雲下載你剛剛需要的項目到本地,這個時候速度就可以了。

四、修改 hosts 文件(親測無效)

搜 GitHub clone 慢的問題,這種方法在網上最常見,但我的環境上是沒有任何效果的。

方法如下,原理很容易理解,了解一下即可。

1. 域名 DNS 解析找到 ip

ping 下列三個域名,或者直接到 IPAddress.com 查詢,得到它們對應的 IP 地址。

bash
github.com
github.global.ssl.fastly.net
codeload.github.com     

最好每一次下載前都重新查詢一下 IP 地址,確保每一次都是最新地址。

2. 修改 hosts 文件

Windows 下在 C:/Windows/system32/drivers/etc/hosts
Ubuntu 等 Linux 系統一般在 /etc/hosts

在 hosts 中添加如下內容:

bash
# Github 192.30.253.112 github.com 199.232.5.194 github.global.ssl.fastly.net 140.82.113.10 codeload.github.com 

3. 清除本地 DNS 緩存

改完之后立刻刷新下本地的 DNS 緩存,下面這個是 Windows 操作系統的命令,其它操作系統的可自行上網查下。

bash
ipconfig /flushdns

五、調整 git 的傳輸緩存(親測無效)

這個方法我試了下,感覺沒什么明顯的效果。

當前環境執行一行 git 命令:

bash
git config --global http.postBuffer 524288000 

不過這個命令可能在另一種場景下有用,就是:

在進行 git push 操作時,出現 500 錯誤,根據錯誤信息判斷是文件過大所致。

因為 Git 默認設置 http post 的緩存為 1M,如果遇到大文件,需要調整 post 緩存,例如調整到 500M。

來源:百度雲盤資源

 
 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM