git 设置代理拉取代码(兼容 windows/mac/linux)

git 设置代理拉取代码(兼容 windows/mac/linux)

git 设置 http/https/socks5 协议的代理

windows/mac/linux 通用设置

http 协议的

git config --global http.proxy "http://127.0.0.1:8080"
git config --global https.proxy "http://127.0.0.1:8080"

socks5 协议的

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

取消 http

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

mac/linux 设置 ssh 代理

配置文件为: ~/.ssh/config

# 必须是 github.com
Host github.com
   HostName github.com
   User git
   # 走 socks5 代理
   ProxyCommand nc -v -x 127.0.0.1:1080 %h %p
   # 走 HTTP 代理
   # ProxyCommand nc -v -X connect -x 127.0.0.1:8090 %h %p

windows 设置 ssh 代理

配置文件同样是 : ~/.ssh/config

Host github.com
    HostName github.com
    User git
    # 走 socks5 协议
    # ProxyCommand connect -S 127.0.0.1:8090 %h %p
    # 走 http 协议
    ProxyCommand connect -H 127.0.0.1:8090 %h %p

区别

在设置 http/https 上,两者系统没有区别。

但是在设置 ssh 代理时,git bash 内置了 connect ,所以在 windows 中,使用的是 connect 。

而在 mac 或者 linux 上,执行 connect 找不到命令,取而代之的是 nc 所以两者的区别仅在于 ProxyCommand 后面使用的命令是不同的

目前网上许多的设置方法,仅仅标注了设置 ssh 代理的方法,并未标注平台,某些使用 linux ,某些使用 windows。导致有的设置不生效或报错
仅对设置方法做整合,以上设置都是自用的

Copyright: 采用 知识共享署名4.0 国际许可协议进行许可

Links: https://zwc365.com/2021/11/09/git-set-proxy-pull-code

Buy me a cup of coffee ☕.