Github warehouse git clone is too slow solution

Posted by sweetmaster on Thu, 24 Feb 2022 10:54:38 +0100

Many times, if you want to clone a warehouse from GitHub, you will encounter the problem of slow speed, and the connection often fails. Here is an effective solution.

1, Background

It should be the problem encountered by many small partners: if you want to clone the project from GitHub, it will be too slow in many cases. After waiting for a long time, you will report an error:

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

Today, I encountered this problem again. After tossing, I summarized the most effective solutions and sorted out the most common answers on the Internet.

2, git set proxy mode (valid for personal test)

Several commands are required.

1. Set up agent

Global agent:

# git config --global http.proxy http://127.0.0.1:1081
# git config --global https.proxy https://127.0.0.1:1081

# After the actual measurement, the following can achieve the effect of accelerating clone and avoid some pits of setting certificates
git config --global http.proxy 127.0.0.1:1081

Local agent, executed in the warehouse of github clone:

# git config --local http.proxy http://127.0.0.1:1081
# git config --local https.proxy https://127.0.0.1:1081

# After the actual measurement, the following can achieve the effect of accelerating clone and avoid some pits of setting certificates
git config --local http.proxy 127.0.0.1:1081

It only acts as an agent for github and does not affect domestic warehouses:

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

# After the actual measurement, the following can achieve the effect of accelerating clone and avoid some pits of setting certificates
git config --global http.https://github.com.proxy 127.0.0.1:1081

2. Query whether to act as agent

Query whether the current git environment uses a proxy.

Query global agent:

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

Query local agent:

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

Query the proxy for github:

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

3. Cancellation of agency

Cancel the agent used in the current git environment and restore the direct connection mode.

Cancel global proxy:

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

Cancel local proxy:

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

Cancel proxy on github:

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

4. Pay attention to the proxy port

It should be noted that the address 127.0.0.1:1081 above is my own proxy address. Everyone needs to check whether their port is also 1081, and distinguish between socks port and http port, because I mainly use https to clone GitHub project.

3, Copy items to code cloud (troublesome)

If there is no agent, this is also an effective method. The disadvantage is that the steps are too cumbersome.

1. Registration code cloud

Gitee is a Git based code hosting and R & D cooperation platform. It is simply understood as the domestic GitHub. See the official website for the specific registration method.

2. Project import code cloud

If the project you want to clone on GitHub is owned by others, you must first fork the project to your GitHub account.

Open the code cloud and copy the items on GitHub to the code cloud through the "import GitHub warehouse" function.

After the import is completed, download the project you just need from the code cloud to the local. At this time, the speed is OK.

4, Modify the hosts file (the pro test is invalid)

The problem of slow searching GitHub clone is the most common method on the Internet, but it has no effect on my environment.

The method is as follows. The principle is easy to understand. Just understand it.

1. DNS resolution of domain name finds ip address

ping the following three domain names, or go directly to IPAddress.com Query and get their corresponding IP addresses.

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

It's best to re query the IP address before each download to ensure that it is the latest address every time.

2. Modify the hosts file

C:/Windows/system32/drivers/etc/hosts under windows. Linux systems such as Ubuntu are generally in / etc/hosts.

Add the following to hosts:

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

3. Clear local DNS cache Refresh the local DNS cache immediately after the change. The following is the command of the Windows operating system. For other operating systems, you can check the Internet by yourself.

ipconfig /flushdns

5, Adjust the transmission cache of git (the pro test is invalid)

I tried this method and felt that it had no obvious effect.

Execute a line of git command in the current environment:

git config --global http.postBuffer 524288000

However, this command may be useful in another scenario, which is:

During git push operation, 500 error occurs. According to the error information, it is judged that the file is too large.

Because Git sets the cache of http post to 1M by default, if you encounter large files, you need to adjust the post cache, for example, to 500M.