Configure Git to use with a proxy and switching back

git-logo-2color

Sometimes it can be useful and even necessary to configure Git to use a proxy in the global configuration.
It’s a easy as (https://git-scm.com/docs/git-config#git-config-httpproxy) :
git config --global http.proxy host:port

This configuration will then be written in ~/.gitconfig.

You will then have something like that :

[user]
	name = Me
	email = me@company.com
[http]
	proxy = host:port

Then one day you want to switch and use a different proxy, or none.

But it’s not obvious to configure, regarding the official documentation (https://git-scm.com/docs/git-config).

I could simply unset the proxy if I want to use none, like this :
git config --global --unset http.proxy

I also tried to create aliases to switch to another configuration, like :

  1. git config --global alias.proxy-local 'config --local http.proxy host:port'
  2. git config --global alias.noproxy-local 'config --local http.proxy none'

So I can use ‘git proxy-local’ or ‘git noproxy-local’ depending of the project.

But it could be more practical. By searching further I found a way to configure the proxy depending on the URL. Within the configuration we can use this format to configure options : http.<url>.* (https://git-scm.com/docs/git-config#git-config-httplturlgt).
Some examples are also documented like this :

; HTTP
[http]
	sslVerify
[http "https://weak.example.com"]
	sslVerify = false
	cookieFile = /tmp/cookie.txt

So that way you can configure a proxy by default, and for some URL use another proxy or none :

; Use a proxy by default
[http]
	proxy = host:port
; Use no proxy on internal server
[http "http://myserver.mycompany/"]
	proxy = 

That’s it !

Publicités

Laisser un commentaire

Entrez vos coordonnées ci-dessous ou cliquez sur une icône pour vous connecter:

Logo WordPress.com

Vous commentez à l'aide de votre compte WordPress.com. Déconnexion /  Changer )

Photo Google+

Vous commentez à l'aide de votre compte Google+. Déconnexion /  Changer )

Image Twitter

Vous commentez à l'aide de votre compte Twitter. Déconnexion /  Changer )

Photo Facebook

Vous commentez à l'aide de votre compte Facebook. Déconnexion /  Changer )

Connexion à %s