How to Solve The Problem of git clone From GitHub Very Slow

Solution 1: Setting Proxy

First, use a US proxy instead of others, like HK.

Setting HTTP Proxy for git clone by HTTPS URL

setting HTTP proxy for git

git config --global http.proxy socks5h://127.0.0.1:1080
# or just proxy GitHub
git config --global http.https://github.com.proxy socks5h://127.0.0.1:1080

or temporarily setting HTTP proxy for your terminal

// linux
export ALL_PROXY=socks5://127.0.0.1:1080
// windows
set ALL_PROXY=socks5://127.0.0.1:1080

Test Case

Before Setting Proxy: about 200 KiB/s.

git clone https://github.com/mybatis/mybatis-3.git mybatis-clone-test
Cloning into 'mybatis-clone-test'...
remote: Enumerating objects: 397085, done.
remote: Counting objects: 100% (195/195), done.
remote: Compressing objects: 100% (79/79), done.
Receiving objects: 4% (15884/397085), 5.25 MiB | 197.00 KiB/s

After Setting Proxy: about 3 MiB/s.

git clone https://github.com/mybatis/mybatis-3.git mybatis-clone-test
Cloning into 'mybatis-clone-test'...
remote: Enumerating objects: 397085, done.
remote: Counting objects: 100% (195/195), done.
remote: Compressing objects: 100% (79/79), done.
Receiving objects: 14% (55592/397085), 18.63 MiB | 3.16 MiB/s

Proxy types

or Setting SSH Proxy for git clone by SSH URL

On Windows, add the following content to your git SSH config file ~/.ssh/config:

ProxyCommand "C:\Program Files\Git\mingw64\bin\connect.exe" -S 127.0.0.1:1080 %h %p

Host github.com
User git
Port 22
Hostname github.com
IdentityFile "C:\Users\Taogen\.ssh\id_ed25519"
TCPKeepAlive yes
IdentitiesOnly yes

Host ssh.github.com
User git
Port 443
Hostname ssh.github.com
IdentityFile "C:\Users\Taogen\.ssh\id_ed25519"
TCPKeepAlive yes
IdentitiesOnly yes

You can put the ProxyCommand ... under a specific Host xxx.xxx.xxx like this if you need to access multiple git hosting sites but set an SSH proxy for only one site.

Host ssh.github.com
ProxyCommand "C:\Program Files\Git\mingw64\bin\connect.exe" -S 127.0.0.1:1080 %h %p
...

Git SSH config file path

  • Windows:
    • per user SSH configuration file: ~/.ssh/config
    • system SSH configuration file: C:\ProgramData\ssh\ssh_config
  • Linux:
    • per user SSH configuration file: ~/.ssh/config
    • system SSH configuration file: /etc/ssh/ssh_config

Git connect program

  • Windows: {git_install_path}\Git\mingw64\bin\connect.exe (You can get the file path by running the command line where connect.exe)
  • Linux: /usr/bin/nc (You can get the file path by running the command line whereis nc)

Solution 2: Switching from using SSH to HTTPS

Using SSH: about 20 KiB/s

git clone git@github.com:xxx/xxx.git
Receiving objects: 0% (309/396867), 172.01 KiB | 26.00 KiB/s

Using HTTPS: about 400 KiB/s

git clone https://github.com/xxx/xxx.git
Receiving objects: 100% (396867/396867), 113.49 MiB | 402.00 KiB/s, done.

Other Improvements

git config --global http.postbuffer 524288000
git config --global credential.helper "cache --timeout=86400"

References

[1] GitHub 加速终极教程

[2] SSH in git behind proxy on windows 7

[3] ssh_config - OpenBSD

[4] nc - OpenBSD