Skip to content

Instantly share code, notes, and snippets.

@evantoli
Last active November 14, 2024 06:03
Show Gist options
  • Save evantoli/f8c23a37eb3558ab8765 to your computer and use it in GitHub Desktop.
Save evantoli/f8c23a37eb3558ab8765 to your computer and use it in GitHub Desktop.
Configure Git to use a proxy

Configure Git to use a proxy

In Brief

You may need to configure a proxy server if you're having trouble cloning or fetching from a remote repository or getting an error like unable to access '...' Couldn't resolve host '...'.

Consider something like:

git config --global http.proxy http://proxyUsername:[email protected]:port

Or for a specific domain, something like:

git config --global http.https://domain.com.proxy http://proxyUsername:[email protected]:port
git config --global http.https://domain.com.sslVerify false

Setting http.<url>.sslVerify to false may help you quickly get going if your workplace employs man-in-the-middle HTTPS proxying. Longer term, you could get the root CA that they are applying to the certificate chain and specify it with either http.sslCAInfo or http.sslCAPath.

See also the git-config documentation, especially the following sections if you're having HTTPS/SSL issues

  • http.sslVerify
  • http.sslCAInfo
  • http.sslCAPath
  • http.sslCert
  • http.sslKey
  • http.sslCertPasswordProtected

In Detail

Configure the proxy

You can configure these globally in your user ~/.gitconfig file using the --global switch, or local to a repository in its .git/config file.

Setting a global proxy

Configure a global proxy if all access to all repos require this proxy

git config --global http.proxy http://proxyUsername:[email protected]:port

URL specific proxy

If you wish to specify that a proxy should be used for just some URLs that specify the URL as a git config subsection using http.<url>.key notation:

git config --global http.https://domain.com.proxy http://proxyUsername:[email protected]:port

Which will result in the following in the ~/.gitconfig file:

[http]
[http "https://domain.com"]
	proxy = http://proxyUsername:[email protected]:port

Handle subsequent SSL protocol errors

If you're still having trouble cloning or fetching and are now getting an unable to access 'https://...': Unknown SSL protocol error in connection to ...:443 then you may decide to switch off SSL verification for the single operation by using the -c http.sslVerify=false option

git -c http.sslVerify=false clone https://domain.com/path/to/git

Once cloned, you may decide set this for just this cloned repository's .git/config by doing. Notice the absence of the --global

git config http.sslVerify false

If you choose to make it global then limit it to a URL using the http.<url>.sslVerify notation:

git config --global http.https://domain.com.sslVerify false

Which will result in the following in the ~/.gitconfig file:

[http]
[http "https://domain.com"]
	proxy = http://proxyUsername:[email protected]:port
	sslVerify = false

Show current configuration

To show the current configuration of all http sections

git config --global --get-regexp http.*

If you are in a locally cloned repository folder then you drop the --global and see all current config:

git config --get-regexp http.*

Unset a proxy or SSL verification

Use the --unset flag to remove configuration being specific about the property -- for example whether it was http.proxy or http.<url>.proxy. Consider using any of the following:

git config --global --unset http.proxy
git config --global --unset http.https://domain.com.proxy

git config --global --unset http.sslVerify
git config --global --unset http.https://domain.com.sslVerify

@HosseinMohammadii
Copy link

thanks bro

@taozuhong
Copy link

export http_proxy=127.0.0.1:8080
export https_proxy=127.0.0.1:8080

@refayathaque
Copy link

This has been immensely helpful for me and my team, thank you so much :)

@xundeenergie
Copy link

I use a long time this solution... but with a big stomach pain... my credentials are in cleartext stored...

Usually i store all my passworts with pass. The standard unix password-manager. So i can call my passwords on shell with
pass show key
and it returns the password for key to stdout.

Now i need a possibility to call this everytime, git uses the proxy.
The pseudocode is:

[http "https://github.com"]
	proxy = http://proxyUsername:$(pass show github)@proxy.server.com:port
	sslVerify = false

How can i run a system-call in git at THIS specific place?

@LittleControl
Copy link

Thanks a lot

@nagarajasr
Copy link

my repo remote origin url contains a username and password for the repo. in this case, the global proxy configuration which is domain based does not work. any suggestions if a wildcard can be used?

https.https://git.mydomain.com.proxy=http://proxyUser:[email protected]:proxyPort
remote.origin.url=https://gitUsername:[email protected]/scm/myproject/myrepo

@ahdung
Copy link

ahdung commented Dec 21, 2021

How to auto follow IE proxy?

@poplar-at-twilight
Copy link

Thanks for this documentation!
💖🎉

@yonoel
Copy link

yonoel commented Apr 10, 2022

Thanks for this documentation!
💖🎉

@superloika
Copy link

This works! Thank you!

@victorovento
Copy link

Thanks!

@klauswoolhouse
Copy link

klauswoolhouse commented Aug 31, 2022

Thank you for sharing the code. It's quite useful. I am engaged in marketing research. Now, I use residential rotating proxies because proxy servers based on data centers no longer cope with the necessary tasks, and more reliable proxies are needed. Now the traffic passes through the provider and disguises itself under a different name, which is very convenient. Still, I had a problem using a proxy in some applications, and your code is very useful for my work in this case.

@MwauStephen
Copy link

Thank you for this detailed documentation

@YunaH2324
Copy link

I'm wanting to make a personal proxy, but I've never coded anything on any site before. I'm really confused on how to use this site, and where to put coding things. Long story short, I'm really confused and need help :')

@Makhsum
Copy link

Makhsum commented Dec 22, 2022

Спасибо

@YaroslavKharchenko0
Copy link

This worked for me:

git config --global http.proxy ""

Thanks

@nichozhan
Copy link

nichozhan commented Sep 2, 2023

Or you can use it only for current command, like this:

git clone https://github.com/your-repo.git -c "https.proxy=socks5://127.0.0.1:7890" -c "http.proxy=socks5://127.0.0.1:7890"

Thanks.

@steve3535
Copy link

Hi, thanks for the doc !
Indeed, leverage the built-in proxy feature of git is so much better and secure than just exporting https_proxy or http_proxy as env. variables.
However, @ozbillwang is right: that works only for remotes configured with http/https.
for git+ssh, I found out that corkscrew is a glorified netcat.
In other words, we can rely on netcat and have the connection goes through successfully. Though I tested only with Squid, I felt it is a more agnostic approach.

for git+ssh

  • Option 1: set it globally in ~/.ssh/config

    Host github.com
    hostname github.com
    User git
    Identityfile /home/steve/tutogit/creds/key
    ProxyCommand nc --proxy proxyserver_ip:proxyserver_port %h %p

  • Option 2: set it per project
    In your local repo:
    git config core.sshCommand "ssh -i /path/private_key -o 'ProxyCommand nc --proxy proxyserver_ip:proxyserver_port %h %p'"

If you need the troubleshoot, pass -v switch to the command git config or add LogLevel DEBUG in .ssh/config file.

  • Option 3: one shot for the CLI lovers:
    git -c core.sshCommand="ssh -v -i /home/steve/tutogit/creds/key -o 'ProxyCommand nc --proxy proxyserver_ip:proxyserver_port %h %p'" fetch kwakousteve

@ZohaibAhmad786
Copy link

ZohaibAhmad786 commented Nov 1, 2023

Guyz do let me know how i am gonna set proxy for 1 repository

let me explain it more what i want.

i am using vpn to access the bitbucket server and clone that repo my lead told me that to clone that i need to set proxy in gitconfig
what i did then and clone the repo like

[http]
proxy = http://xx.xx.xx.xxx:8080
sslVerify = false

What i want it that i want to clone other repo's without set & unsetting this config each time.

@steve3535
Copy link

Hello, @ZohaibAhmad786
then you are already doing the right thing.
Just set those settings in .gitconfig file in the repository folder. (not gitconfig under /etc or the one in your home)
That way, the settings will only be local to your specific repository and shouldnt affect any other.

@steve3535
Copy link

Initially, you could clone the repo it by passing the proxy at the command line:

git config --local http.proxy "http://xx.xx.xx.xx:80"
git config --local http.sslVerify false

@ZohaibAhmad786
Copy link

Initially, you could clone the repo it by passing the proxy at the command line:

git config --local http.proxy "http://xx.xx.xx.xx:80" git config --local http.sslVerify false

Thanks for helping me @steve3535 💖🎉

@tokland
Copy link

tokland commented Feb 20, 2024

@taozuhong Typically this would be equivalent:

export ALL_PROXY=127.0.0.1:8080

@NikolayXHD
Copy link

@steve3535 A huge thank you for the explanation about git+ssh!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment