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

我需要从Git服务器签出代码,但它每次都显示“请求超时”。我该怎么解决这个问题呢?

或者如何设置代理服务器?


当前回答

尝试将以下内容放到~/。gitconfig文件:

[http]
    proxy = http://proxy:8080
[https]
    proxy = http://proxy:8080
[url "https://"]
    insteadOf = git://

其他回答

作为使用git配置的替代方案——global http。代理地址:端口,可以在命令行设置代理:

git -c "http.proxy=address:port" clone https://...

优点是代理不需要持久设置。在Bash下你可以设置一个别名:

alias git-proxy='git -c "http.proxy=address:port"'

如果你正在使用ubuntu,那么执行以下操作…

步骤1:安装开瓶器

$ sudo apt-get install corkscrew

步骤2:编写名为git-proxy.sh的脚本,并添加以下内容

#!/bin/sh

exec corkscrew <name of proxy server> <port> $*

# <name_of_proxy_server> and <port> are the ip address and port of the server
# e.g. exec corkscrew 192.168.0.1 808 $*

步骤3:使脚本可执行

$ chmod +x git-proxy.sh

步骤4:通过设置环境变量为GIT设置代理命令

$ export GIT_PROXY_COMMAND="/<path>/git-proxy.sh"

现在使用git命令,例如

git clone git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git

在不知疲倦地尝试这个页面上的每个解决方案后,我的工作是使用SSH密钥代替!

打开Git Bash ssh-keygen.exe -t rsa -C 打开你的Git提供商(Github, Bitbucket等) 添加复制id_rsa。pub文件内容到Git提供者的输入页面(检查您的配置文件)

尝试将以下内容放到~/。gitconfig文件:

[http]
    proxy = http://proxy:8080
[https]
    proxy = http://proxy:8080
[url "https://"]
    insteadOf = git://

如果您已经安装和配置了tsocks或proxychains,则可以

$ tsocks git clone <you_repository>

or

$ proxychains git clone <you_repository>

为了使它更短,我为proxychains创建了一个符号link /usr/bin/p,所以我可以像这样使用它

p git clone <you_repository>

我可以用它来代理任何命令

p <cmd-need-be-proxied>

顺便说一下,proxychains很长一段时间没有更新,你可能想试试proxychians-ng