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。

来源:百度云盘资源

 
 
posted @ 2021-01-07 22:22  风吹头蛋凉嗖嗖  阅读(2475)  评论(0编辑  收藏  举报