Working with GIT while, behind proxy

Recently I had some issues in making GIT working while I was in a proxy enabled network.

Below are the steps that helped me to resolve the issue:

  1. Set global proxy information in GIT
  2. Check the updated setting in .gitconfig
  3. Set proxy information for Terminal
  4. Check proxy information for Terminal
  5. Making it handy

The steps in detail:

  1. Set global proxy information in GIT

Open your terminal and execute the below commands. Note, that if you have any special characters like ‘@’ in either username or in password, you need URL encode it. Do not change the ‘@’ symbol before the proxy IP.

This is the standard syntax:

git config –global https.proxy https://username:password@proxyserverIP:port

Most of the time, in an enterprise environment, your username would be something as username@company.com

After URL encoding it will become,

git config --global https.proxy https://username%40company.com:password@proxyserverIP:port

Similarly, set http proxy:

git config --global http.proxy http://username%40company.com:password@proxyserverIP:port

Note that, the “@” symbol has been changed to %40

2. Check the updated setting in .gitconfig

Once you execute the above commands, validate it in .gitconfig. You should see it in your home folder. Remember, it is a hidden file.

If everything is good, you should see something similar as below in your .gitconfig file.

[https]
 proxy = https://username%40company.com:password@proxyserverIP:port
[http]
 proxy = http://username%40company.com:password@proxyserverIP:port

 

You can also check the current settings by executing the commands in terminal:

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

If something is wrong, unset the git proxy and start again.

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

 

3. Set proxy information for Terminal

In terminal, execute the below commands :

http_proxy=username:password@proxyserverIP:port
https_proxy=username:password@proxyserverIP:port

Note that, this time @company.com is omitted.

4. Check proxy information for Terminal

Make sure to check the proxy set in Terminal

echo $http_proxy
echo $https_proxy

In case something is wrong, unset the proxy and start again.

unset http_proxy
unset https_proxy

Try to clone the repo from Terminal using the standard syntax.

git clone http://repo/reponame.git

In case the command again, fails try closing all running applications and restart the system.

5. Making it handy

If you often switch between proxied and non-proxied environments, it is better to convert these steps in to handy scripts.

ProxySet.sh

http_proxy=username:password@proxyserverIP:port
https_proxy=username:password@proxyserverIP:port
git config --global http.proxy http://username%40company.com:password@proxyserverIP:port
git config --global https.proxy https://username%40company.com:password@proxyserverIP:port

ProxyUnset.sh

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

You need to make sure that, both of these script files are executable. Once you have your own copy of the scripts, execute below command:

chmod a+x ProxySet.sh
chmod a+x ProxyUnset.sh

Save these files in your home folder and, when it is the time to switch, just drag and drop the .sh file to terminal and hit enter. Alternatively, you can set ‘Terminal.app’ as the default application to run the file and just double click it.

Advertisements
Working with GIT while, behind proxy