为 git 配置代理

https://infong.net/config-proxy-for-git/


1条回复

有时候在内网工作,工作机不能连接到外网,要 push 只能通过内网提供的代理时,那么就必需要设置 git 的代理了。

Git 目前支持的三种协议 git://ssh:// 以及 http(s)://,其代理配置各不相同:core.gitproxy 用于git:// 协议,http.proxy 用于 http(s):// 协议,而ssh:// 协议的代理需要配置 ssh 的 ProxyCommand 参数。

所以我们要针对不同的 Git 协议来进行代理的设置,而且还需要根据代理服务器的不同来进行不同的设置。

一、代理服务器为 socks4/5

使用 socks 代理服务器的时候,我们需要用 https://bitbucket.org/gotoh/connect 来进行代理的转换,可以下载下来自己编译,也可以 Linux 发行版的仓库中查找命名为 proxy-connect 或者 connect-proxy 的软件包,Archlinux 可以在 aur 中找到 connect-proxy。

建立一个 /PATH/TO/socks5proxywrapper 文件,内容为,假设服务器地址为 10.22.0.4,端口为 1080:

#!/bin/sh
connect -S 10.22.0.4:1080 "$@"

1.GIT 协议的配置

配置 ~/.gitconfig

[core]
       gitproxy = /PATH/TO/socks5proxywrapper

或者直接设置 GIT_PROXY_COMMAND 环境变量

export GIT_PROXY_COMMAND=”/PATH/TO/socks5proxywrapper”

2.SSH 协议的配置

建立一个 /PATH/TO/socks5proxyssh 文件:

#!/bin/sh
ssh -o ProxyCommand="/PATH/TO/socks5proxywrapper %h %p" "$@"

配置 git 使用该 wrapper

export GIT_SSH=”/PATH/TO/socks5proxyssh”

或者

GIT_SSH=”/PATH/TO/socks5proxyssh” git pull/push

当然也可以直接配置 ~/.ssh/configProxyCommand

3.HTTP(S) 协议的配置

因为 Git 使用 libcurl 提供 http 支持,所以直接在 git 配置文件中加入

[http]
       proxy = socks5://127.0.0.1:7070

二、代理服务器为 http 代理

1.HTTP 协议配置

[http]
   proxy = http://10.22.0.4:8080

2.SSH/GIT 协议

建立 /PATH/TO/httpproxywrapper 文件:

#!/bin/sh
connect -H 10.22.0.4:8080 "$@"

然后根据上述 socks 代理中的设置,将 socks5proxywrapper 改为 httpproxywrapper

EOF

参考:http://segmentfault.com/q/1010000000118837