ChatGPT解决这个技术问题 Extra ChatGPT

How do I pull from a Git repository through an HTTP proxy?

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.

I have a project under Git control. I'd like to add a submodule:

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:

c:\project> echo %HTTP_PROXY%
http://proxy.mycompany:80

I even have a global Git setting for the http proxy:

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 (rails for example).

Could your proxy be censoring some words or something, thereby only affecting those repos where those words happen to show up as part of the byte stream?
@alvaro, I think you mean that Getting git to work with a proxy server is a possible duplicate of this question. This question is older than the one to which you refer.
@James, your "global" Git setting for the proxy server is not actually global, but local. To be global, you need to supply argument --global to git config.
@DerekMahar Older questions may be closed of duplicates of newer questions, if the newer question is better quality (or more generally applicatble).

D
Derek Mahar

You can also set the HTTP proxy that Git uses in global configuration property 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 @EugeneKulabuhov and @JaimeReynoso for the authentication format.)


This worked for me: Set HTTP_PROXY in environment for user (on Windows), then the above comment with $HTTP_PROXY (uppercase). +1 for this solution.
CleverCoder, on Windows, you need to specify %HTTP_PROXY%, not $HTTP_PROXY (unless you're running Cygwin, of course). I will edit my answer to work in Windows instead of a Unix environment.
Use git config --global http.proxy http://mydomain\\myusername:mypassword@myproxyserver:8080 syntax to provide username/password
For anyone who might be having problems with the proxy, http://:@:/ worked for me
M
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.

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:

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.

You can then verify that the command added the entry to your .gitconfig file successfully by doing 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!


Interesting. I am able to get to the "Resolving deltas" portion with 100% but then it looks like the clone process just hangs. Anyone experienced that?
+1. And soon, you will be able to setup proxy per url!
@apoplexy can you comment on your suggested edit please so I can consider
If you have @ symbol in your username or password, then you can URL encode it %40 and it will work.
Instead of cat .gitconfig, you can list the config settings with git config --list
D
Derek Mahar

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.


Does setting http.proxy in the global Git configuration work? In your question, you set http.proxy in the local repository configuration.
In my case I had to set the https_proxy
https.proxy seemed to work for me as I was using https over github
@MSmith If you had to set https_proxy this means you were using https not http which question is about.
Curl and libcurl don't recognize uppercase HTTP_PROXY and HTTPS_PROXY. See this link for more info: curl.haxx.se/mail/archive-2001-12/0034.html
a
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.

$ git clone https://chromium.googlesource.com/chromium/tools/depot_tools.git --config "http.proxy=proxyHost:proxyPort"

This is strange, I tried setting $http_proxy, but git didn't respect it. Looks like it only cares about its own config...
Works. You can also specify --config multiple times, e.g. when you also want to set https.proxy.
clone with proxy config but without changing the proxy config of the user. Exactly what I need ! Thx.
--config "core.gitProxy..." howto,I want to proxy ssh protocol without global proxy seting, thx.@alijandro
s
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:

GIT_CURL_VERBOSE=1

This should at least give you an idea of what is going on behind the scenes.


When I add this environment variable, msysgit doesn't print out anything extra. :(
after fighting with proxies for some time, this tip helped me find out that curl's cert database was missing/messed up, and THAT was why requests were failing.
b
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.

git config --global http.proxy http://proxy:8081

You can also add this to the environment to accept the rewritten certificates that the network dudes offer to inspect your traffic: export GIT_SSL_NO_VERIFY=true
Worked for me perfectly. Thanks. I just had to add git config --global http.proxy proxy:8081 git config --global https.proxy proxy:8081
Thanks to @sethbc, @datasmid! I needed GIT_CURL_VERBOSE=1 to debug and GIT_SSL_NO_VERIFY=true to skip the verification.
not working with me. not even with the GIT_SSL_NO_VERIFY var
l
legoscia

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 config --global url."https://github.com/".insteadOf git://github.com/

I know that I also had problems to pull from git://. Have you considered a SSH key problem? My problem was due to a bad SSH key configuration.
S
Stéphane B.

You could too edit .gitconfig file located in %userprofile% directory on Windows system (notepad %userprofile%.gitconfig) or in ~ directory on Linux system (vi ~/.gitconfig) and add a http section as below.

Content of .gitconfig file :

[http]
        proxy = http://proxy.mycompany:80

B
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!


It was enough to set https_proxy environment variable alone (no git global or repo proxy option set) in my case (msysgit on Windows XP).
C
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.

Install corkscrew, or other alternatives you want. Create a authfile. The format for authfile is: user_name:password, and user_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 is 644: chmod 644 ~/.ssh/config

Take github.com as an example, add the following lines to ~/.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 git@github.com, it will use the proxy automatically. You can easily do the same thing to Bitbucket as well.

This is not so elegant as other approaches, but it works like a charm.


Can you please elaborate on step 2, "create a authfile"? It seems like your approach is the only one that will work for git:// urls.
B
Boris Brodski

On Windows, if you don't want to put your password in .gitconfig in the plain text, you can use

Cntml (http://cntlm.sourceforge.net/)

It authenticates you against normal or even Windows NTLM proxy and starts localhost-proxy without authentication.

In order to get it run:

Install Cntml

Configure Cntml according to documentation to pass your proxy authentication

Point git to your new localhost proxy: [http] proxy = http://localhost:3128 # change port as necessary


For what it's worth, this should've been the accepted answer, he's on windows. There isn't a transparent proxy (otherwise there would not be a problem.
L
Lesswire

For me what it worked was:

sudo apt-get install socat

Create a file inside your $BIN_PATH/gitproxy with:

#!/bin/sh 
_proxy=192.168.192.1 
_proxyport=3128 
exec socat STDIO PROXY:$_proxy:$1:$2,proxyport=$_proxyport

Dont forget to give it execution permissions

chmod a+x gitproxy

Run following commands to setup environment:

export PATH=$BIN_PATH:$PATH
git config --global core.gitproxy gitproxy

Link to an explanation and a link to a gist with updates to the (mini-)script: emilsit.net/blog/archives/…
This answer is the only one that works for git:// protocol URLs, for those (rare) git servers that don't support https. Also works on windows, with an appropriate batch file and socat for windows.
C
Clairton Luz

Setup proxy to git

command

git config --global http.proxy http://user:password@domain:port

example

git config --global http.proxy http://clairton:123456@proxy.clairtonluz.com.br:8080

T
Thor88

Set Git credential.helper to wincred.

git config --global credential.helper wincred

Make sure there is only 1 credential.helper

git config -l

If there is more than 1 and it's not set to wincred remove it.

git config --system --unset credential.helper

Now set the proxy with no password.

git config --global http.proxy http://<YOUR WIN LOGIN NAME>@proxy:80

Check that all the settings that you added looks good....

git config --global -l

Now you good to go!


K
Kalle Richter

I had the same problem, with a slightly different fix: REBUILDING GIT WITH HTTP SUPPORT

The git: protocol did not work through my corporate firewall.

For example, this timed out:

git clone git://github.com/miksago/node-websocket-server.git

curl github.com works just fine, though, so I know my http_proxy environment variable is correct.

I tried using http, like below, but got an immediate error.

git clone http://github.com/miksago/node-websocket-server.git

->>>  fatal: Unable to find remote helper for 'http' <<<-

I tried recompiling git like so:

./configure  --with-curl --with-expat

but still got the fatal error.

Finally, after several frustrating hours, I read the configure file, and saw this:

# Define CURLDIR=/foo/bar if your curl header and library files are in # /foo/bar/include and /foo/bar/lib directories.

I remembered then, that I had not complied curl from source, and so went looking for the header files. Sure enough, they were not installed. That was the problem. Make did not complain about the missing header files. So I did not realize that the --with-curl option did nothing (it is, in fact the default in my version of git).

I did the following to fix it:

Added the headers needed for make: yum install curl-devel (expat-devel-1.95.8-8.3.el5_5.3.i386 was already installed). Removed git from /usr/local (as I want the new install to live there). I simply removed git* from /usr/local/share and /usr/local/libexec Searched for the include dirs containing the curl and expat header files, and then (because I had read through configure) added these to the environment like so: export CURLDIR=/usr/include export EXPATDIR=/usr/include Ran configure with the following options, which, again, were described in the configure file itself, and were also the defaults but what the heck: ./configure --with-curl --with-expat And now http works with git through my corporate firewall: git clone http://github.com/miksago/node-websocket-server.git Cloning into 'node-websocket-server'... * Couldn't find host github.com in the .netrc file, using defaults * About to connect() to proxy proxy.entp.attws.com port 8080 * Trying 135.214.40.30... * connected ...


I'd glad you put this one forward, I didn't realise that the git protocol wasn't being handled on my squid proxy
n
noufalcep

This worked to me.

git config --global http.proxy proxy_user:proxy_passwd@proxy_ip:proxy_port

R
RishiD

Just to post this as it is the first result on Google, this blog post I found solves the problem for me by updated the curl certificates.

http://www.simplicidade.org/notes/archives/2011/06/github_ssl_ca_errors.html


Halleluyah, this plus git config --global http.sslcainfo NEW_CERTS_BUNDLE.crt as described in: stackoverflow.com/a/8248484/98528 did it for me! Thanks!
f
fangxing

Use proxychains

proxychains git pull ...

update: proxychains is discontinued, use proxychains-ng instead.


unfortunately Unix only
@Samoth Jun, for windows your may want try SocksCap64
g
gratinierer

Worth to mention: Most examples on the net show examples like

git config --global http.proxy proxy_user:proxy_passwd@proxy_ip:proxy_port

So it seems, that - if your proxy needs authentication - you must leave your company-password in the git-config. Which isn't really cool.

But, if you just configure the user without password:

git config --global http.proxy proxy_user@proxy_ip:proxy_port

Git seems (at least on my Windows-machine without credentials-helper) to recognize that and prompts for the proxy-password on repo-access.


You are my man, as my sso company username and password have so much special simboles that was quite impossibile to encode and get through. With this "escamotage" I was able to provide the password on the clone face or any other next steps after have set the proxy address.
M
Montells

you can use:

git config --add http.proxy http://user:password@proxy_host:proxy_port

N
Nguyen Van Duc

The below method works for me:

echo 'export http_proxy=http://username:password@roxy_host:port/' >> ~/.bash_profile
echo 'export https_proxy=http://username:password@roxy_host:port' >> ~/.bash_profile

Zsh note: Modify your ~/.zshenv file instead of ~/.bash_profile.

Ubuntu and Fedora note: Modify your ~/.bashrc file instead of ~/.bash_profile.


S
School Boy

For Windows

Goto --> C:/Users/user_name/gitconfig

Update gitconfig file with below details

[http]

[https]

proxy = https://your_proxy:your_port

[http]

proxy = http://your_proxy:your_port

How to check your proxy and port number?

Internet Explorer -> Settings -> Internet Options -> Connections -> LAN Settings


R
Rosen Matev

There is a way to set up a proxy for a specific URL, see the http.<url>.* section in the git config manual. For example, for https://github.com/ one can do

git config --global 'http.https://github.com/.proxy' http://proxy.mycompany:80

s
sethbc

This isn't a problem with your proxy. It's a problem with github (or git). It fails for me on git-1.6.0.1 on linux as well. Bug is already reported (by you no less).

Make sure to delete your pasties, they're already on google. Edit: Must've been dreaming, i guess you can't delete them. Use Gist instead?


I posted to github and msysGit as well b/c I wasn't sure where the problem lay. I can't seem to delete pasties, unfortunately.
j
jimagic

$http_proxy is for http://github.com.... $https_proxy is for https://github.com...


P
Praveen

The above answers worked for me when my proxy doesn't need authentication. If you are using proxy which requires you to authenticate then you may try CCProxy. I have small tutorial on how to set it up here,

http://blog.praveenkumar.co.in/2012/09/proxy-free-windows-xp78-and-mobiles.html

I was able to push, pull, create new repos. Everything worked just fine. Make sure you do a clean uninstall and reinstall of new version if you are facing issues with Git like I did.


C
Community

as @user2188765 has already pointed out, try replacing the git:// protocol of the repository with http[s]://. See also this answer


n
ndsmyter

I got around the proxy using https... some proxies don't even check https.

Microsoft Windows [Version 6.1.7601]
Copyright (c) 2009 Microsoft Corporation.  All rights reserved.

c:\git\meantest>git clone http://github.com/linnovate/mean.git
Cloning into 'mean'...
fatal: unable to access 'http://github.com/linnovate/mean.git/': Failed connect
to github.com:80; No error

c:\git\meantest>git clone https://github.com/linnovate/mean.git
Cloning into 'mean'...
remote: Reusing existing pack: 2587, done.
remote: Counting objects: 27, done.
remote: Compressing objects: 100% (24/24), done.
rRemote: Total 2614 (delta 3), reused 4 (delta 0)eceiving objects:  98% (2562/26

Receiving objects: 100% (2614/2614), 1.76 MiB | 305.00 KiB/s, done.
Resolving deltas: 100% (1166/1166), done.
Checking connectivity... done

C
Community

As this was answered by many but This is just for Winodws USER who is behind proxy with auth.

Re-Installing(first failed, Don't remove).

Goto ->
**Windows**
1. msysgit\installer-tmp\etc\gitconfig
    Under [http]
        proxy = http://user:pass@url:port

**Linux**
1. msysgit\installer-tmp\setup-msysgit.sh
      export HTTP_PROXY="http://USER:PASS@proxy.abc.com:8080"

if you have any special char in user/pass use url_encode