返回

GitHub加速终极教程

   
摘要GPT
摘要小助理暂时失联跑路啦……😜

本文意图解决使用 GitHub 访问(https) 或者 git clone(https or ssh)慢的问题。在此分享我的方法,我所了解的 GitHub 加速最佳方案。

前提是,你的木弟子应该还行,木弟子越好,GitHub 体验越好

很多文章没有讲全面,只讲了 http proxy,而没有讲 ssh proxy。事实上大部分程序员使用 GitHub 都会使用 SSH keys(普通用户可能就不会了),在本机生成 rsa 公私钥(其他的类型还有 dsa | ecdsa | ecdsa-sk | ed25519 | ed25519-sk),然后把公钥内容拷贝、设置进 GitHub。

所以程序员 clone 一个仓库一般是 ssh clone 而不是 https clone

1

git clone git@github.com:xxx/yyy.git

如果你不配置 ssh 代理或者没有透明代理之类的网络环境(其实还有一些代理工具,不过更加小众),直接硬拖到本地大概率是很慢的。如果使用 http 代理,如果木弟子质量好,其实也还行

1

git clone https://github.com/xxx/yyy.git

但这样不如 ssh clone 稳定。下面我们来设置 http proxyssh proxy

设置 Http Proxy

git config --global http.proxy socks5://127.0.0.1:7890 
git config --global https.proxy socks5://127.0.0.1:7890

好了,说回来。但这样配置的话会使本机所有的 git 服务都走了代理,假如你在良心云上(国内主机)部署了自己的 gitea,服务地址 https://gitea.example.com,那么可以只配置 GitHub 的 http proxy,即

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

这样做实际上是修改了 ~/.gitconfig 文件,添加了如下内容

[http "https://github.com"]
        proxy = socks5://127.0.0.1:7890

设置 SSH Proxy (推荐)

设置ssh代理,可加速 git clone SSH协议下载

Linux & macOS

配置文件在用户家目录下的 .ssh/config 其中 nc 程序位于 /usr/bin/nc

cat ~/.ssh/config

Host github.com
 Hostname ssh.github.com
 IdentityFile /xxx/.ssh/github_id_rsa
 User git
 Port 443
 ProxyCommand nc -v -x 127.0.0.1:7890 %h %p

Windows

Win 下与之对应的 netcat 程序是 connect.exe,程序位于 Git 安装路径 C:\Program Files\Git\mingw64\bin,win 下推荐使用 Git Bash,路径也是 Linux style

因为 connect 程序内置在 Git 中,只要是正常安装 Git 的电脑环境都有这个程序,在 Git Bash 终端输入 connect 即可知晓程序路径在 C:\Program Files\Git\mingw64\bin\connect.exe

添加config文件

nano ~/.ssh/config

配置:

Host github.com
    Hostname ssh.github.com
    IdentityFile ~/.ssh/id_rsa
    User git
    Port 443
    ProxyCommand "C:\Program Files\Git\mingw64\bin\connect.exe" -S 127.0.0.1:7890 %h %p

Ctrl + O 保存文件,然后按 Ctrl + X 退出。

现在,你的 Git 配置已经正确添加到 ~/.ssh/config 文件中,可以在 Bash 中使用 Git 时生效了。

一键设置SSH Proxy (windows)

要使用 Bash 命令自动运行并保存上述 SSH 配置到 ~/.ssh/config 文件,你可以使用以下命令:

cat <<EOL >> ~/.ssh/config
Host github.com
    Hostname ssh.github.com
    IdentityFile ~/.ssh/id_rsa
    User git
    Port 443
    ProxyCommand "C:\Program Files\Git\mingw64\bin\connect.exe" -S 127.0.0.1:7890 %h %p
EOL
知识共享许可证 CC BY-NC-SA 4.0
最后更新于 2024-07-17 18:28
使用 Hugo 构建
主题 hugo-magic小洋葱 魔改 由 Jimmy 设计
Written by Human, Not by AI