使用 git 克隆 github 上的项目失败

现象

今天在使用 git clone nextjs demo project 源代码的时, git clone https://github.com/XXXX/next-blog.git 下载速度很慢,然后下载一段时间后,总是提示下面的错误信息

1
2
3
4
5
6
7
8
nCloning into 'next-blog'...
remote: Enumerating objects: 111, done.
remote: Counting objects: 100% (111/111), done.
remote: Compressing objects: 100% (83/83), done.
error: RPC failed; curl 18 transfer closed with outstanding read data remaining
fatal: The remote end hung up unexpectedly
fatal: early EOF
fatal: index-pack failed

原因

由于Http协议错误,当 pull 或者 clone 的时候,或者是 github 某个CDN被伟大的墙屏蔽所致。

解决办法

协议错误

  1. 先执行下列命令

    1
    
    git config --global http.postBuffer 524288000
    
  2. 再执行git pull 或者 git clone命令

墙屏蔽

  1. 访问 http://github.global.ssl.fast… 获取cdn域名以及IP地址

  2. 访问 http://github.com.ipaddress.c… 获取cdn域名以及IP地址 github的cdn域名以及IP地址

  3. 将上述获取的IP地址添加到/etc/hosts

    1
    
    sudo vim /etc/hosts
    

    添加IP地址到hosts

  4. 刷新dns缓存

    1
    2
    
    sudo killall -HUP mDNSResponder
    sudo dscacheutil -flushcache
    

结果

再执行 git clone 的操作的时候,速度飕飕飕的上去了,一下子达到几百Kb啦~