Skip to content

Instantly share code, notes, and snippets.

@evantoli
Last active April 12, 2024 14:11
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:proxyPassword@proxy.server.com:port

Or for a specific domain, something like:

git config --global http.https://domain.com.proxy http://proxyUsername:proxyPassword@proxy.server.com: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:proxyPassword@proxy.server.com: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:proxyPassword@proxy.server.com:port

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

[http]
[http "https://domain.com"]
	proxy = http://proxyUsername:proxyPassword@proxy.server.com: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:proxyPassword@proxy.server.com: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

@sugunpradeep
Copy link

Regarding git proxy settings via ssh. I could access my repository via https with the provided proxy settings. But could not access the same via ssh. Any suggestions Please.

@theasteve
Copy link

It looks as though this doesn't work for Github Pages. I'm able to push to a repo via git using this setting. However, I'm unable to deploy to Github. Is there information on how to Configure Github Pages to use a proxy?

@pamaxis
Copy link

pamaxis commented Nov 13, 2019

if having issues in Intellij IDEA and corp proxy try setting the system config as well as the global one:

git config --system http.proxy

:

@freifrauvonbleifrei
Copy link

@ people looking for an ssh solution: this here worked for me

@sidd3sh
Copy link

sidd3sh commented Jan 8, 2020

Thank you!

@chenrq2005
Copy link

Thanks! Great job! It worked for me.

@atulkumarpccs
Copy link

Great this resolves my problem :)

@ozbillwang
Copy link

ozbillwang commented Feb 12, 2020

https://gist.github.com/ozbillwang/005bd1dfc597a2f3a00148834ad3e551

@freifrauvonbleifrei

Thanks, the original solution is only for https access to github which is not good enough. Because you have to provide github username and password everytime, when you try to git push later.

If we need support ssh+git access, your way works.

Sample clone commands:

The clone command with protocol https

git clone https://github.com/kubernetes/kubernetes.git

The clone command with protocol ssh+git

git clone git@github.com:kubernetes/kubernetes.git

Notes:

If you only need access github by the way of ssh+git, you needn't set any proxy in ~/.gitconfig and run git config --global http.proxy ... and similar commands at all

Work with ssh config

So I have to set ssh config (~/ssh/config) with ProxyCommand properly, git clone start working with proxy.

Host github.com
    Hostname github.com
    ServerAliveInterval 55
    ForwardAgent yes
    ProxyCommand /usr/bin/corkscrew <replace_with_your_company_proxy_server> <3128> %h %p ~/.ssh/myauth

Work with http/https

update ~/.gitconfig

[http]
[http "https://github.com"]
	proxy = http://proxyUsername:proxyPassword@proxy.server.com:port
	sslVerify = false

sslVerify setting is not necessary.

@dalegaspi
Copy link

this has been useful information for me during this pandemic. thank you.

@duangsuse
Copy link

Thank you <3

@kvnloughead
Copy link

Thank you, @dipti219. I was having trouble figuring out how to fill in the blanks. When in doubt, don't!

@MaratZakirov
Copy link

Can I use this method to use git on server (to which I connect via VPN + SSH) which does not have internet access?

@csmper
Copy link

csmper commented Jun 18, 2020

Hi @evantoli I'm referring to a corporate proxy..

So I have my dotFiles hosted in github, and one of them is my global ~/.gitconfig, so that I can always have my aliases available to me. But I also have the proxy configuration on that file, even though that's hasen't been pushed. Looking like this:

~/.gitconfig

/* ....... aliases and stuff .... */

[http]
proxy = http://username:password@http.proxy:8080/
sslVerify = false
As you can already see in my configuration, I was able to mask the IP address by changing my /etc/hosts file, but I have no idea how would I hide/mask my username and password, which are plain text.

Any ideas ? ( And sorry for being to short explaining my self initially )

Also, I realize this might not be the right channel since this is more of a unix configuration files issue; but I'm sure this is a common issue among the party of this gist xD

Thanks in advance !

This was helpful to resolve my issue +1

@xiaoliuzi
Copy link

xiaoliuzi commented Aug 8, 2020

Before clone code, set proxy in terminal.:)

git config --global http.proxy 'socks5://127.0.0.1:1080' 
git config --global https.proxy 'socks5://127.0.0.1:1080'

@Janice9399
Copy link

git config --global http.proxy ""

It helps a lot. Thanks

@ahanafi
Copy link

ahanafi commented Dec 8, 2020

git config --global http.proxy myproxy.com:8080

what would "myproxy.com" be in my case?

ANSWER:

This worked for me:

git config --global http.proxy ""

So I guess you can leave it empty!

Thanks, it worked in my machine :D

@swarnalatha116
Copy link

git config --global http.proxy myproxy.com:8080

what would "myproxy.com" be in my case?

ANSWER:

This worked for me:

git config --global http.proxy ""

So I guess you can leave it empty!

This Worked for me:
git config --global http.proxy ""
git config --global https.proxy ""

Thanks

@lilduckyboi
Copy link

does anyone know how i can get this?

@leensabbouh
Copy link

thank u

@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:proxyPass@proxyServer.com:proxyPort
remote.origin.url=https://gitUsername:gitPassword@git.mydomain.com/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

@zhannicholas
Copy link

zhannicholas 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

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