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

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

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


当前回答

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

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

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

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

其他回答

我注意到一些事情,想在这里分享:

git config --global http.proxy http://<proxy address>:<port number>

上述方法不适用于SSH url(即git@github.com:<用户名>/<项目名>.git):

git clone git@github.com:<user name>/<project name>.git // will not use the http proxy

如果我们将SSH设置为HTTPS端口(https://help.github.com/en/articles/using-ssh-over-the-https-port),事情也不会改变,因为它只改变了SSH连接连接的端口(默认为22)。

我已经尝试了以上所有的答案,但都不适合我,因为有一个代理密码编码问题。

这个命令起作用了:

git config --global http.proxy http://username@proxy.example.com:PortNumber 

请不要在命令中输入密码。当你尝试连接到任何git repo时,它会动态地询问。

使用命令:

git config --global http.proxy http://proxyuser:proxypwd@proxy.server.com:8080

将proxyuser更改为代理用户 将proxypwd更改为您的代理密码 将proxy.server.com更改为代理服务器的URL 将8080更改为代理服务器上配置的代理端口

注意,这适用于http和https回购。

如果你决定在任何时候重置这个代理并在没有代理的情况下工作:

使用命令:

git config --global --unset http.proxy

最后,检查当前设置的代理:

git config --global --get http.proxy

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

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

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

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