如何通过 HTTP 代理从 Git 存储库中提取?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/128035/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I pull from a Git repository through an HTTP proxy?
提问by James A. Rosen
Note: while the use-case described is about using submodules within a project, the same applies to a normal git clone
of a repository over HTTP.
注意:虽然描述的用例是关于在项目中使用子模块,但这同样适用于git clone
通过 HTTP 的存储库的正常情况。
I have a project under Git control. I'd like to add a submodule:
我有一个受 Git 控制的项目。我想添加一个子模块:
git submodule add http://github.com/jscruggs/metric_fu.git vendor/plugins/metric_fu
But I get
但我得到
...
got 1b0313f016d98e556396c91d08127c59722762d0
got 4c42d44a9221209293e5f3eb7e662a1571b09421
got b0d6414e3ca5c2fb4b95b7712c7edbf7d2becac7
error: Unable to find abc07fcf79aebed56497e3894c6c3c06046f913a under http://github.com/jscruggs/metri...
Cannot obtain needed commit abc07fcf79aebed56497e3894c6c3c06046f913a
while processing commit ee576543b3a0820cc966cc10cc41e6ffb3415658.
fatal: Fetch failed.
Clone of 'http://github.com/jscruggs/metric_fu.git' into submodule path 'vendor/plugins/metric_fu'
I have my HTTP_PROXY set up:
我设置了 HTTP_PROXY:
c:\project> echo %HTTP_PROXY%
http://proxy.mycompany:80
I even have a global Git setting for the http proxy:
我什至有一个 http 代理的全局 Git 设置:
c:\project> git config --get http.proxy
http://proxy.mycompany:80
Has anybody gotten HTTP fetches to consistently work through a proxy? What's really strange is that a few project on GitHub work fine (awesome_nested_set
for example), but others consistently fail (railsfor example).
有没有人通过代理获取 HTTP 获取以始终如一地工作?真正奇怪的是,GitHub 上的一些项目运行良好(awesome_nested_set
例如),但其他项目始终失败(例如rails)。
采纳答案by James A. Rosen
What finally worked was setting the http_proxy
environment variable. I had set HTTP_PROXY
correctly, but git apparently likes the lower-case version better.
最终起作用的是设置http_proxy
环境变量。我设置HTTP_PROXY
正确,但 git 显然更喜欢小写版本。
回答by Derek Mahar
You can also set the HTTP proxy that Git uses in global configuration property http.proxy
:
您还可以在全局配置属性中设置 Git 使用的 HTTP 代理http.proxy
:
git config --global http.proxy http://proxy.mycompany:80
To authenticate with the proxy:
要使用代理进行身份验证:
git config --global http.proxy http://mydomain\myusername:mypassword@myproxyserver:8080/
(Credit goes to @EugeneKulabuhovand @JaimeReynosofor the authentication format.)
(归功于@EugeneKulabuhov和@JaimeReynoso的身份验证格式。)
回答by Max MacLeod
There's some great answers on this already. However, I thought I would chip in as some proxy servers require you to authenticate with a user Id and password. Sometimes this can be on a domain.
已经有一些很好的答案。但是,我想我会介入,因为某些代理服务器要求您使用用户 ID 和密码进行身份验证。有时这可以在域上。
So, for example if your proxy server configuration is as follows:
因此,例如,如果您的代理服务器配置如下:
Server: myproxyserver
Port: 8080
Username: mydomain\myusername
Password: mypassword
Then, add to your .gitconfig
file using the following command:
然后,.gitconfig
使用以下命令添加到您的文件中:
git config --global http.proxy http://mydomain\myusername:mypassword@myproxyserver:8080
Don't worry about https
. As long as the specified proxy server supports http, and https, then one entry in the config file will suffice.
别担心https
。只要指定的代理服务器支持http和https,那么配置文件中的一项就足够了。
You can then verify that the command added the entry to your .gitconfig
file successfully by doing cat .gitconfig
:
然后,您可以.gitconfig
通过执行cat .gitconfig
以下操作来验证该命令是否已成功将条目添加到您的文件中:
At the end of the file you will see an entry as follows:
在文件末尾,您将看到如下条目:
[http]
proxy = http://mydomain\myusername:mypassword@myproxyserver:8080
That's it!
就是这样!
回答by sethbc
It looks like you're using a mingw compile of Git on windows (or possibly another one I haven't heard about). There are ways to debug this: I believe all of the http proxy work for git is done by curl. Set this environment variable before running git:
看起来您正在 Windows 上使用 Git 的 mingw 编译(或者可能是另一个我没有听说过的)。有一些方法可以对此进行调试:我相信 git 的所有 http 代理工作都是由 curl 完成的。在运行 git 之前设置此环境变量:
GIT_CURL_VERBOSE=1
This should at least give you an idea of what is going on behind the scenes.
这至少应该让您了解幕后发生的事情。
回答by alijandro
If you just want to use proxy on a specified repository, don't need on other repositories. The preferable way is the -c, --config <key=value>
option when you git clone
a repository. e.g.
如果您只想在指定的存储库上使用代理,则不需要在其他存储库上。-c, --config <key=value>
当您git clone
是存储库时,更可取的方法是选项。例如
$ git clone https://chromium.googlesource.com/chromium/tools/depot_tools.git --config "http.proxy=proxyHost:proxyPort"
回答by bbaassssiiee
When your network team does ssl-inspection by rewriting certificates, then using a http url instead of a https one, combined with setting this var worked for me.
当您的网络团队通过重写证书进行 ssl 检查时,然后使用 http url 而不是 https url,并结合设置此 var 对我有用。
git config --global http.proxy http://proxy:8081
回答by TonyT_32909023190
For me the git:// just doesn't work through the proxy although the https:// does. This caused some bit of headache because I was running scripts that all used git:// so I couldn't just easily change them all. However I found this GEM
对我来说 git:// 只是不能通过代理工作,尽管 https:// 可以。这引起了一些头痛,因为我正在运行的脚本都使用 git:// ,所以我不能轻易地更改它们。但是我发现了这个 GEM
git config --global url."https://github.com/".insteadOf git://github.com/
回答by STB Land
You could too edit .gitconfig filelocated in %userprofile% directory on Windows system (notepad %userprofile%.gitconfig) or in ~ directory on Linux system (vi ~/.gitconfig) and add a http sectionas below.
您也可以编辑位于 Windows 系统上的 %userprofile% 目录(记事本 %userprofile%.gitconfig)或 Linux 系统上的 ~ 目录(vi ~/.gitconfig)中的.gitconfig 文件,并添加一个 http 部分,如下所示。
Content of .gitconfig file :
.gitconfig 文件的内容:
[http]
proxy = http://proxy.mycompany:80
回答by Benjamin Wootton
This is an old question but if you are on Windows, consider setting HTTPS_PROXY as well if you are retrieving via an https URL. Worked for me!
这是一个老问题,但如果您使用的是 Windows,如果您通过 https URL 检索,请考虑设置 HTTPS_PROXY。对我来说有效!
回答by Carlosin
I find neither http.proxy
nor GIT_PROXY_COMMAND
work for my authenticated http proxy. The proxy is not triggered in either way. But I find a way to work around this.
我发现我的经过身份验证的 http 代理既不适用http.proxy
也不GIT_PROXY_COMMAND
工作。代理不会以任何一种方式触发。但我找到了解决这个问题的方法。
- Install corkscrew, or other alternatives you want.
Create a authfile. The format for
authfile
is:user_name:password
, anduser_name
,password
is your username and password to access your proxy. To create such a file, simply run command like this:echo "username:password" > ~/.ssh/authfile
.Edit
~/.ssh/config
, and make sure its permission is644
:chmod 644 ~/.ssh/config
- 安装corkscrew或其他您想要的替代品。
创建一个授权文件。格式为
authfile
:user_name:password
, 和user_name
,password
是您访问代理的用户名和密码。要创建这样的文件,只需运行命令是这样的:echo "username:password" > ~/.ssh/authfile
。编辑
~/.ssh/config
,并确保其权限为644
:chmod 644 ~/.ssh/config
Take github.com as an example, add the following lines to ~/.ssh/config
:
以github.com为例,添加以下几行~/.ssh/config
:
Host github.com
HostName github.com
ProxyCommand /usr/local/bin/corkscrew <your.proxy> <proxy port> %h %p <path/to/authfile>
User git
Now whenever you do anything with [email protected]
, it will use the proxy automatically. You can easily do the same thing to Bitbucketas well.
现在,无论您何时使用[email protected]
,它都会自动使用代理。您也可以轻松地对Bitbucket执行相同的操作。
This is not so elegant as other approaches, but it works like a charm.
这不像其他方法那么优雅,但它就像一种魅力。