ChatGPT解决这个技术问题 Extra ChatGPT

Getting Git to work with a proxy server - fails with "Request timed out"

How do I get Git to use a proxy server?

I need to check out code from a Git server, but it shows "Request timed out" every time. How do I get around this?

Alternatively, how can I set a proxy server?

Wouldn't this be a git client configuration item? The only aspect of this that seems like would involve cmd.exe is that the git client may allow you to specify a proxy in an environement variable.
I have edited the original question to remove all Windows references, as this problem is git-specific per se.
Note that you will be able toon to setup a proxy per git repo url!
Most of the answers in this question go about HTTP proxies. If you have a SOCKS proxy, see this question for the HTTP/S protocols and this one for the git:// protocol.

J
Johan

Command to use:

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

change proxyuser to your proxy user

change proxypwd to your proxy password

change proxy.server.com to the URL of your proxy server

change 8080 to the proxy port configured on your proxy server

Note that this works for both http and https repos.

If you decide at any time to reset this proxy and work without proxy:

Command to use:

git config --global --unset http.proxy

Finally, to check the currently set proxy:

git config --global --get http.proxy

Thanks Salim. Mine worked without the proxyuser:proxypwd in the URL.
Thanks,guys. For me it was NTLM based authentication and just updating my .gitconfig didn't work :( . So I had to get CNTLM which uses NTLM authetication. All I had to do was point my CNTLM to my network's proxy server and then point my software update manager(like yum,apt-get or ssh) to the local proxy. In case of git operations like clone,pull the http.proxy was my locally configured proxy with the local server.
Hello, how can I set proxypass if it have "@" character? I know I must scape this character but I don't know how.. Thank you
@patricK You can use %40 as a replacement for the @ in username/ password string, though I've not tested this myself. Hope it helps. :-)
@patricK I can confirm that using %40 as a replacement for @ works. It seems if a special character isn't working you should try URL encoding it. The encodings are found here: w3schools.com/tags/ref_urlencode.asp
a
alvaro

This worked for me, in windows XP behind a corporate firewall.

I didnt have to install any local proxy or any other software besides git v1.771 from http://code.google.com/p/msysgit/downloads/list?can=3

$ git config --global http.proxy http://proxyuser:proxypwd@proxy.server.com:8080
$ git config --system http.sslcainfo /bin/curl-ca-bundle.crt
$ git remote add origin https://mygithubuser:mygithubpwd@github.com/repoUser/repoName.git
$ git push origin master

proxyuser= the proxy user I was assigned by our IT dept, in my case it is the same windows user I use to log in to my PC, the Active Directory user

proxypwd= the password of my proxy user

proxy.server.com:8080 = the proxy name and port, I got it from Control Panel, Internet Options, Connections, Lan Settings button, Advanced button inside the Proxy Server section, use the servername and port on the first (http) row.

mygithubuser = the user I use to log in to github.com

mygithubpwd = the password for my github.com user

repoUser = the user owner of the repo

repoName = the name of the repo


Hooray, thanks! git config --global http.sslcainfo MY_NEW_CERTS_BUNDLE.crt plus certificates downloaded as described in simplicidade.org/notes/archives/2011/06/… (thanks to: stackoverflow.com/a/7206627/98528) did it for me!
Seems to work for me, but git still doesn't work when I ask it to clone a repository using the git:// protocol.
A couple of clarifications to pablolic's information, based on what worked for me: 1) If proxyuser is a Windows login, leave out the AD domain, just include the user name without the domain; 2) Spaces in the proxypwd should be encoded as "+". eg "My Password" should be entered as "My+Password".
@EdmundYeung99: I'd suggest replacing the @ symbol with %40. Haven't tried it but I believe that is standard URL encoding.
@demongolem: I suspect you may need to URL encode your password (haven't tried it, this is just an educated guess). See UrlEncoded column in the table in the following answer: stackoverflow.com/a/11236038/216440 In your case that would mean replacing the $ with %24.
J
Jens Wirth

Set a system variable named http_proxy with the value of ProxyServer:Port. That is the simplest solution. Respectively, use https_proxy as daefu pointed out in the comments.

Setting gitproxy (as sleske mentions) is another option, but that requires a "command", which is not as straightforward as the above solution.

References: http://bardofschool.blogspot.com/2008/11/use-git-behind-proxy.html


Or use 'https_proxy' if you're connecting to a https-repository.
@daefu: Not sure if you need to set https_proxy. I'm connected to an https repository and followed the first step of pablolic's instructions (ie adding http.proxy to the git config file). That was all it took for me.
https_proxy was required for me. Setting only http_proxy did not allow it to clone an HTTPS repo
@daefu +1 this also works with widows github - related question : stackoverflow.com/questions/16216176/…
@SimonTewsi @explunit While setting environment variables http_proxy is required for HTTP URLs and https_proxy is required for HTTPS URLs. Alternatively, one could just set all_proxy for all URLs. See ENVIRONMENT section in curl(1). However, while setting git-config options, http.proxy is used for both HTTP and HTTPS URLs. See http.proxy in git-config(1).
g
guerda

If the command line way of configuring your proxy server doesn't work, you can probably just edit .gitconfig (in the root of your profile, which may hide both in C:\Documents and Settings and on some network drive) and add this:

[http]
    proxy = http://username:password@proxy.at.your.org:8080

YMMV though, this only covers the first step of the command line configuration. You may have to edit the system git configuration too and I have no idea where they hid that.


"You may have to edit the system git configuration too". No, usually not. The system and user config are combined; setting the proxy in one of the two should be enough.
In my case it's located on my Windows machine in [git home]\etc\gitconfig - after editing it works like a charm!
This is especially useful if the command line doesn't like your password due to special characters.
S
Steve Pitchers

As an alternative to using git config --global http.proxy address:port, you can set the proxy on the command line:

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

The advantage is the proxy is not persistently set. Under Bash you might set an alias:

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

S
Sk Hasanujjaman

If you are using ubuntu, then do the following ...

Step 1 : Install corkscrew

$ sudo apt-get install corkscrew

Step 2 : Write a script named git-proxy.sh and add the following

#!/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 $*

Step 3 : Make the script executable

$ chmod +x git-proxy.sh

Step 4 : Set up the proxy command for GIT by setting the environment variable

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

Now use the git commands,such as

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

You should probably use "$@" instead of $*. See your shell manual for more details.
E
Elly

There's something I noticed and want to share here:

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

The method above will not work for SSH URLs (i.e., git@github.com:<user name>/<project name>.git):

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

And things will not change if we set SSH over the HTTPS port (https://help.github.com/en/articles/using-ssh-over-the-https-port) because it only changes the port (22 by default) the SSH connection connects to.


A
Arpit Aggarwal

Faced same issue because of multiple .gitconfig files in windows, followed below steps to fix the same:

Step 1: Open Git BASH

Step 2: Look for .gitconfig, executing following command:

git config --list --global --show-origin

Step 3: Copy the below content in .gitconfig:

[http]
    proxy = http://YOUR_PROXY_USERNAME:YOUR_PROXY_PASSWORD@YOUR.PROXY.SERVER:YOUR.PROXY.SERVER.PORT
    sslverify = false
[https]
    proxy = http://YOUR_PROXY_USERNAME:YOUR_PROXY_PASSWORD@YOUR.PROXY.SERVER:YOUR.PROXY.SERVER.PORT
    sslverify = false
[url "http://github.com/"]
    insteadOf = git://github.com/

[user]
    name = Arpit Aggarwal
    email = aggarwalarpit.89@gmail.com

a
augustocbx

Try to put the following to the ~/.gitconfig file:

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

I tried all the stuff above. It wasn't until I added the https entry listed here that I was able to work.
a
ashutosh

For the git protocol (git://...), install socat and write a script such as:

#!/bin/sh

exec socat - socks4:your.company.com:$1:$2

make it executable, put it in your path, and in your ~/.gitconfig set core.gitproxy to the name of that script.


Works for windows with cygwin's SOCAT too. using socat STDIO PROXY:%proxy%:%1:%2,proxyport=%PROXYPORT%
Can use set GIT_PROXY_COMMAND=path\to\script to make GIT use the proxy without messing with git config.
Yes! Finally the right hint! I also needed authentication: exec socat STDIO PROXY:<proxy>:$1:$2,proxyport=<port>,proxyauth=<user>:<passwd> Found here: gist.github.com/sit/49288
S
Shak Daniel

I work on Windows XP at work(state/gov), so I did my research and found this here and it worked for me. Hope this helps :)

The http_proxy Environment Variable

If you use a proxy server or firewall, you may need to set the http_proxy environment variable in order to access some url from commandline. Example : Installing ppm for perl or applying rpm in linux ,updating ubuntu

Set the http_proxy variable with the hostname or IP address of the proxy server: http_proxy=http:// [proxy.example.org]

If the proxy server requires a user name and password, include them in the following form: http_proxy=http:// [username:password@proxy.example.org]

If the proxy server uses a port other than 80, include the port number: http_proxy=http:// [username:password@proxy.example.org:8080]

Windows XP

Open the Control Panel and click the System icon. On the Advanced tab, click on Environment Variables. Click New in the System variables panel. Add http_proxy with the appropriate proxy information (see examples above).

Linux, Solaris or HP-UX

Set the http_proxy environment variable using the command specific to your shell (e.g. set or export). To make this change persistent, add the command to the appropriate profile file for the shell. For example, in bash, add a line like the following to your .bash_profile or .bashrc file:

http_proxy=http:// [username:password@hostname:port]; export $http_proxy


tanx--> :) C:\Users\AT>echo %https_proxy% 172.20.4.55:808
b
blacelle

In addition of thse answers, I found helpful to consider these 2 points:

One may need to enforce an authentication scheme:

[http]
    # https://github.com/git/git/blob/master/Documentation/config.txt
    proxyAuthMethod = anyauth|basic|digest|negotiate|ntlm

Also, typically with NTLM authentication schema, one may need to provide explicitely the AD domain.

In git bash:

echo %userdomain%

And update the http.proxy accordingly:

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

Anyway, investigation may be helped by adding CURL logs:

export GIT_CURL_VERBOSE=1

I needed to add proxyAuthMethod using git config --global http.proxyAuthMethod basic. Git gersion 2.8.3 didn't send in any authentication until I set this config parameter.
f
fangxing

If you have tsocks or proxychains installed and configured, you can

$ tsocks git clone <you_repository>

or

$ proxychains git clone <you_repository>

to make it shorter, I created a symbol link /usr/bin/p for proxychains, so I can use it like this

p git clone <you_repository>

and I can use it to proxy any command,

p <cmd-need-be-proxied>

by the way, proxychains is not updated for a long time, you may wanna try proxychians-ng


A
Add080bbA

Setting git proxy on terminal

if

you do not want set proxy for each of your git projects manually, one by one

always want to use same proxy for all your projects

Set it globally once

git config --global http.proxy username:password@proxy_url:proxy_port
git config --global https.proxy username:password@proxy_url:proxy_port

if you want to set proxy for only one git project (there may be some situations where you may not want to use same proxy or any proxy at all for some git connections)

//go to project root
cd /bla_bla/project_root
//set proxy for both http and https
git config http.proxy username:password@proxy_url:proxy_port
git config https.proxy username:password@proxy_url:proxy_port

if you want to display current proxy settings

git config --list 

if you want to remove proxy globally

git config --global --unset http.proxy
git config --global --unset https.proxy

if you want to remove proxy for only one git root

//go to project root
cd /bla-bla/project_root
git config --unset http.proxy
git config --unset https.proxy

https.proxy is invalid config 🙃. Only the configuration item named http.proxy exists. see: git-scm.com/docs/git-config/2.9.5#Documentation/…
h
hannad rehman

here is the proxy setting

git config --global http.proxy http://<username>:<pass>@<ip>:<port>
git config --global https.proxy http://<username>:<pass>@<ip>:<port>

W
Wael Almadhoun

I followed the most of the answers which was recommended here. First I got the following error:

fatal: unable to access 'https://github.com/folder/sample.git/': schannel: next InitializeSecurityContext failed: Unknown error (0x80092012) - The revocation function was unable to check revocation for the certificate.

Then I have tried the following command by @Salim Hamidi

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

But I got the following error:

fatal: unable to access 'https://github.com/folder/sample.git/': Received HTTP code 407 from proxy after CONNECT

This could happen if the proxy server can't verify the SSL certificate. So we want to make sure that the ssl verification is off (not recommended for non trusted sites), so I have done the following steps which was recommended by @Arpit but with slight changes:

1.First make sure to remove any previous proxy settings:

git config --global --unset http.proxy

2.Then list and get the gitconfig content

git config --list --show-origin

3.Last update the content of the gitconfig file as below:

[http]
sslCAInfo = C:/yourfolder/AppData/Local/Programs/Git/mingw64/ssl/certs/ca-bundle.crt
sslBackend = schannel
proxy = http://proxyuser:proxypwd@proxy.server.com:8080
sslverify = false
[https]
proxy = http://proxyuser:proxypwd@proxy.server.com:8080
sslverify = false

I
Ishtdeep Hora

I have tried all the above answers and nothing worked for me, as there was a proxy password encoding issues.

This command worked:

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

Do not enter the password in your command. It will dynamically ask for when you try to connect to any git repo.


Thanks, this worked for me. Although the git config setting should be http.proxy
username is not enough.Password also required.
use this code {git config --global http.proxy proxyuser:proxypwd@proxy.server.com:8080}
C
Community

For windows users: if git config or set http_proxy= doesn't work, this answer may help:

replace the git:// protocol of the git repository with http://. Note, you'll have to set the http_proxy first, anyways.


J
Janac Meena

After tirelessly trying every solution on this page, my work around was to use and SSH key instead!

Open Git Bash $ ssh-keygen.exe -t rsa -C Open your Git provider (Github, Bitbucket, etc.) Add copy the id_rsa.pub file contents into Git provider's input page (check your profile)


f
fatemeh sadeghi

an alternative to using a proxy is to use SSH In the git configs, configure the origin remote on the ssh address.Then use the ssh-keygen command, which gives you a public key that you can set in your GitLab or Gitab account settings and login accordingly done...

1-verify which remotes are using by running git remote -v in your Git client.

2-if this is http(s) url ,changed it to ssh address , run: git remote set-url <remote name, e.g. origin> <new SSH URL> for example

git remote set-url git@gitlab.com:example/myproject.git

3-to generate ssh key for login ,run : ssh-keygen -o, this command generate public(id_rsa.pub file) and private keys.

4-copy public key contents. (from id_rsa.pub file)

5-go to gitlab/githup/.... profile section > setting/ssh-key . create new ssh key and paste public key contents