首页 > 如何为 Git 设置代理?

如何为 Git 设置代理?

最近从 Bitbucket经常访问不了,Github拖代码的速度也抽风,什么原因大家都知道。
所以,最好的方法就是给Git设置代理了,我希望能指定部分仓库走代理方式,不知该如何设置?

补充另一种情况

本地环境下,本人设置了ssh代理,通过SOCKS走127.0.0.1:7070实现,如果在本地ssh代理开启的情况下,是不是又有另外的方法?


最优方案是proxychains(https://github.com/haad/proxychains)。
下面命令运行程序program
$ proxychains program
它强制给定程序发起的TCP连接通过事先配置的代理。至少在Linux上,它比“SOCKS代理转为HTTP代理”用途更广泛。两者互为补充,可以涵盖所有需要代理的情景。
以Git为例,没有proxychains的话,就必须为每个协议(http, git, ssh)按照git文档的要求分别设置代理,过程复杂且不稳定。有了proxychains,这些都可以忘掉了!

$ sudo apt-get install proxychains
打开/etc/proxychains.conf,注释掉下面这行(disable远程DNS解析。有DNS污染风险。下文讲如何解决enable不work的问题。)
proxy_dns
最后添加如下行:
socks5 134.64.206.85 1081
$ proxychains git clone git://github.com/yuzhichang/cppdep
$ sudo proxychains apt-get update
这里134.64.206.85:1081是SOCKS代理位置。


从bitbucket克隆用ssh协议的话可以用所有ssh的代理使用方式

比如如果是http代理或socks代理,可以使用 http://bent.latency.net/bent/git/goto... 这个小程序做主机的ProxyCommand

如果是你说的有某个中转服务器的话,可以用远程主机上的ncProxyCommand http://www.undeadly.org/cgi?action=ar...

关于你说的区分流量,可以在~/.ssh/config里进行区别设置。例如我之前用的配置

Host bitbucket.org
    ProxyCommand ~/.ssh/connect -H 192.168.1.106:3128 %h 22

这样git clone ssh://git@bitbucket.org/XXXX时会自动调用这里定义的ProxyCommand


可以为该仓库设置 http.proxy 或者 core.gitproxy

git config http.proxy http://user:pwd@server.com:port
git config core.gitproxy '"proxy-command" for example.com'

http://www.kernel.org/pub/software/sc...


根据1L的答案写了一个smart_switcher,可以自动判别并设置各种代理,基于http代理配置,特别适合office有代理、家里无代理各种切换的情况,不过简化到超简单,只需设置你的网关IP和Port即可。
ReadMe在此。。。

smart_switcher

A auto-detect proxy switcher fot http, https, ftp, rsync, ssh, git protocols.


Overview

A smart proxy switcher wrapper, supports http, https, ftp, rsync, ssh(connect depanded), git(connect depanded) protocols. It can automatically detect your network environment and set proxy for you.

If you usually switch the network environment (maybe home with no-proxy and workplace with proxy), it may help you a lot.

Tested in zsh and bash.

Screenshot

Install

Simply source it in your .zshrc, or any shell script resource file like this:

source /path/to/smart_switcher.sh

and, make sure set your proxy_server/gateway in smart_switcher.sh.

Usage

Normally, it antomatically executes when you login in.

smart_switcher supports cecho, who will bring some colors for you.

connect is required if proxy is supported in ssh and git. You can install it easily in path /usr/bin/connect.


我想你可能是问的是这个问题。

问题:
E:\PDFium>git clone https://pdfium.googlesource.com/pdfium
Cloning into 'pdfium'...
fatal: unable to access 'https://pdfium.googlesource.com/pdfium/': Failed connect to pdfium.googlesource.com:443; No error

解决:
git config --global http.proxy http://proxy.com:1234

git config --global https.proxy http://proxy.com:1234
git config --global http.sslverify false
转自:http://ricksu.blog.163.com/blog/static/18906433820125294929508/

参考:http://infong.net/config-proxy-for-git/

我使用本机上的小软件翻墙,所以设置为

E:\PDFium>git config --global http.proxy http://127.0.0.1:8580
E:\PDFium>git config --global https.proxy https://127.0.0.1:8580
E:\PDFium>git config

--global http.SSLVERIFY false


在ubuntu 16.04上还可以这样子:

在一个终端上输入:

export all_proxy=socks://addr:port
export ALL_PROXY=socks://addr:port

然后在这个终端上执行的git所有操作都会走代理

如果想取消掉的话:

unset all_proxy
unset ALL_PROXY

github ssh proxy | github ssh 协议代理配置

配置一个 proxy-wrapper 脚本

bash
cat > $HOME/bin/proxy-wrapper
#!/bin/bash
nc -x127.0.0.1:7080 -X5 $*

给它增加一个可执行权限

bash
$ chmod +x $HOME/bin/proxy-wrapper

配置 .ssh/config , 对 github.com 设置一个代理命令

bash
Host github github.com
    Hostname github.com
    User git
    ProxyCommand $HOME/bin/proxy-wrapper '%h %p'

必须全部走ssh协议

bash
$ git clone git@github.com:jjrdn/node-open.git

git 协议请参考 [Using GIT through a SOCKS proxy](http://twopenguins.org/tips/git-throu...).

参考

  1. [如何为 Git 设置代理?](http://.com/q/10100000001...)
  2. [Using GIT through a SOCKS proxy](http://twopenguins.org/tips/git-throu...)
  3. [免费的ssh](http://milnk.com/link/10645)

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

对于所有的协议全部使用 SSH 隧道进行代理

GIT 协议的配置

建立 /path/to/socks5proxywrapper 文件,使用 https://bitbucket.org/gotoh/connect 工具进行代理的转换,各发行版一般打包为 proxy-connect 或者 connect-proxy。

#!/bin/sh
connect -S 127.0.0.1:7070 "$@"

配置 git

[core]
        gitproxy = /path/to/socks5proxywrapper

或者

export GIT_PROXY_COMMAND="/path/to/socks5proxywrapper"

SSH 协议的配置

建立 /path/to/soks5proxyssh 文件

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

配置 git 使用该 wrapper

export GIT_SSH="/path/to/socks5proxyssh“

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

HTTP 协议的配置

[http]
        #这里是因为 Git 使用 libcurl 提供 http 支持
        proxy = socks5://127.0.0.1:7070

所有协议全部使用 http 代理

在前一部分的基础上, /path/to/socks5proxywrapper 文件改为

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

HTTP 协议配置

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

针对域名启用代理

gitproxy 参数提供 * for * 结构,具体看 man git-config 的 core.gitproxy 部分。


git协议连接方式使用的是ssh同服务器通讯,设置ssh走sock5代理连接服务器的同时也解决了git的代理问题。

1.

https://raw.githubusercontent.com/bronzeee/ssh_connect/master/connect.c

将上述代码使用gcc编译并保存在环境变量目录中同时改名connect
进入.ssh目录新建

文件config

Host *
    User git
    ProxyCommand $HOME/.ssh/proxy-wrapper '%h %p'

文件 proxy-wrapper

#!/bin/bash
~/connect.exe -S http://IP:PORT $@

给git设置代理可以用yanyaoer的方法。
至于你说的ssh+pac+socks的代理,是跟yanyaoer说的git代理不冲突的。
socks代理是一个电路级的底层代理,而git config中设置的代理是应用级的。

举个例子,你的pac里设置了 github.com 走 socks 127.0.0.1:7070 ;而git config里有为github.com设置了走 proxy.server.com 的代理。
那么这个时候,你进行git操作,所有的网络请求走到socks那一层的时候,已经是proxy.server.com了,自然就不受影响,会直接出去。


上面有不少答案了。假设用程序在无状态、无工作目录的情况下运行git指令,利用-c参数可以在运行时重载git配置,包括关键的http.proxy

例如:

git clone -c http.proxy=http://192.168.117.131:8888  http://github.com/me/test1.git

tsocks - http://tsocks.sourceforge.net/

$ tsocks git clone git@github.com:xxx/xxx.git

【热门文章】
【热门文章】