如何让 Git 使用 HTTP 代理服务器

因为我们的内部网络使用了代理,所以在 安装 OpenStack 基于 Web 的管理控制台 的时候有个小麻烦,我们的 http 代理服务器无法通过 git 协议下载 openstack-dashboard 所需的代码,也就是说可以 git clone https:// 这样克隆代码,但是不能使用 git 协议 git clone git://。糟糕的是在 openstack-dashboard/tools/pip-requires 里恰好用到了 git 协议,所以运行 openstack-dashboard 安装脚本的时候会因为网络无法连接 git:// 而中途失败。如何让 git 使用 http 代理服务器呢?

如果是 git clone http:// 或 git clone https:// 的话直接把代理服务器加到环境变量就可以了:

$ export http_proxy="http://username:password@squid.vpsee.com:3128/"
$ export https_proxy="http://username:password@squid.vpsee.com:3128/"

如果是 git clone git:// 的话麻烦一些(可能有的 git 源不提供 http/https 的方式),需要先安装 socat,然后创建一个叫做 gitproxy 的脚本并填上合适的服务器地址、端口号等,最后配置 git 使用 gitproxy 脚本:

$ sudo apt-get install socat

$ sudo vi /usr/bin/gitproxy
#!/bin/bash

PROXY=squid.vpsee.com
PROXYPORT=3128
PROXYAUTH=username:password
exec socat STDIO PROXY:$PROXY:$1:$2,proxyport=$PROXYPORT,proxyauth=$PROXYAUTH

$ sudo  chmod +x /usr/bin/gitproxy

$ git config --global core.gitproxy gitproxy

评论 (9 Comments)

  1. Finally got a feasible solution. It took me so long to solve it. Thank u very much.

  2. 试验成功,帮我解决了一个大问题,非常感谢!

  3. 太感谢了.一直困扰于此问题.这下好了.顺便问候该死的gfw.

  4. Requires that the proxy allows CONNECT to port 9418.

  5. 用了之后怎么取消代理啊

  6. 为什么每次开启虚拟机都要重新设置git代理呢?我用的是virtualbox,有没有什么方法一次性设好就不用每次都设置了~~

  7. 怎么取消啊

  8. Thanks for the tip that solve my problem :)

  9. 很好,解决我的问题了!

发表评论