前提
本文针对自己有 socks/https 代理,但不知道如何给 git 配置的人。
git 协议
git 主要有以下三种协议与服务器通信。
- https
- ssh
- git protocol
各自的代理 socks 配置方法不同,下面分别举例介绍如何配置。
https
https 协议,需要使用 https 代理,在 ~/.gitconfig 如下配置即可:
ssh
ssh 协议与 git 关系不大,是 git 调用 ssh 命令与服务器通信,所以配置在
~/.ssh/confg。下面是让 ssh 到 github.com 时,使用 socks 配置:
git
git 协议比较特殊,有专门一个配置 core.gitproxy
做代理,但需要额外写一个脚本,才能使用上 socks 代理。
~/.gitconfig 配置如下:
或执行如下代码:
git config --global core.gitproxy "/path/to/gitproxy for github.com"
gitproxy 脚本例子:
#!/bin/sh
#
# Proxy wrapper for git protocol (9418).
#
# git config --global core.gitproxy "ido gitproxy"
#
exec /usr/bin/nc -X 5 -x <socks_host>:<socks_port> $1 $2
注意
本文配置或脚本中使用的 nc 是 Mac OSX 自带的 BSD nc (netcat) 。Linux 下可以装同版本的 (各发行版安装方法不一致,就不举例了),或者根据你当前所用的 nc 的说明,调整参数达到同样的目的。