Git HTTP+SSH 代理配置

github访问日益困难,配置git服务代理以快速clone代码🐶

技术方案转载于http://www.chenhe.cc/p/406

HTTP 代理

配置全局代理

# HTTP 代理
git config --global http.proxy http://127.0.0.1:7890
git config --global https.proxy http://127.0.0.1:7890

# Socks5 代理
git config --global http.proxy socks5://127.0.0.1:7890
git config --global https.proxy socks5://127.0.0.1:7890

注意这里的socks5仅仅是代理使用的协议,它依然是针对 http 设置的,所以仅对 http 协议的仓库有效。使用 git@xxx 这种 ssh 连接的不会使用代理。

分域名设置代理

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

SSH 代理

SSH 代理需要在密钥目录 (~/.ssh) (Windows 下是 C:\Users\{UserName}\.ssh) 新建一个 config 文件,没有后缀名。

Linux/MacOS 系统写入以下配置:

# 需要 netcat
Host github.com
  User git
  Port 22
  Hostname github.com
  TCPKeepAlive yes
  ProxyCommand nc -v -x 127.0.0.1:1080 %h %p

Windows系统配置

需要额外安装connect.exe程序,如果找不到 connect 命令那么指定其绝对路径,一般在 git 安装目录下 \mingw64\bin\connect.exe。

Host github.com
  User git
  Port 22
  Hostname github.com
  TCPKeepAlive yes
  # -S 为 socks, -H 为 HTTP
  ProxyCommand connect -S 127.0.0.1:1080 %h %p