9

Background

The work computer that I have, I occasionally carry it at home. Now, we have a proxy server at work, and I have configured git to use it by doing git config --global http.proxy http://proxy.company.com. So when I get back at home, I do not require a proxy, so I need to unset the http.proxy. This is okay if I have to do it once in a while, but right now I need to do this every day: set the proxy, when I get to work, go home and unset the proxy, next day set it at work again.

What I require

A way to bypass the http.proxy that has been set, individually in every command. Something like a --no-proxy option:

git --no-proxy pull

I do not want to specify the proxy in every command, like:

git --proxy=http://proxy.company.com

because I do more gitting at work than I do at home.

3
  • For this reason, on my laptop i set on/off the proxy by OS (ubuntu gnome) and i haven't configured anything on git. Commented Jan 3, 2017 at 9:03
  • I don't think you can do that on Windows though. (Incidentally, how did you do it? HTTP_PROXY variable?) Commented Jan 3, 2017 at 9:11
  • Yeah http_proxy & https_proxy variables. Commented Jan 6, 2017 at 8:35

3 Answers 3

22

You can either do exactly what you asked by using

git -c http.proxy= clone https://github.com/foo/bar.git

This will set the proxy to an empty value for this command and thus not use a proxy.


If it is not about cloning, but about fetching, pushing, pulling and so on, you can also add two remotes to your repository and then set remote.<name>.proxy accordingly. Then you use one remote at work and the other at home. As the commits are the same you should not have to download a commit twice even if it is on different remote tracking branches.

Sign up to request clarification or add additional context in comments.

8 Comments

The -c option was what I was looking for. And the using different remotes is something I hadn't thought of. Haven't tried it yet, but thanks for that.
providing http.proxy= will result in a error: Missing value for 'http.proxy'
In which Git version? It worked for me and obviously for OP.
How about updating your ancient (4 years old) Git version instead of downvoting a perfectly valid answer?
It is http.proxy, not http_proxy
|
3

I am not sure if it will suit your needs, but you can set up proxy for specific urls in such way:

[http "<matching url>"]
    proxy = <url>

For example: I am behind proxy at work and want to access projects on github, so I add following section to my ~/.gitconfig (note, that I use fake username "proxy" in matching url):

[http "https://[email protected]/"]
    proxy = https://10.144.1.10:8080/

From now on, whenever I want to access any server without proxy - I do it as usual. When I want to access server over proxy, I add fake "proxy" username in front.

This will stall when I am behind proxy, but work otherwise:

$ git clone https://github.com/project/path.git

This will work behind proxy:

$ git clone https://[email protected]/project/path.git

To make fetch, pull and push work - you will need to add 2 remotes (one for proxy, one without proxy) and use accordingly.

2 Comments

Yes, this is a valid solution, but I think I like the -c option more.
Instead of using an address, I used an empty string and it worked proxy =
1

I solved a similar situation by writing a Bash script that simply turns the proxy on and off by setting the http.proxy configuration variable:

#!/bin/sh

proxy="http://host:port"

if [[ $(git config --global http.proxy) ]]; then
    git config --global --unset http.proxy
    echo "Git is not using a proxy"
else
    git config --global http.proxy $proxy
    echo "Git is using the proxy at $proxy"
fi

I called the script flip_git_proxy and put it in the path. Now, each time I start a new Bash session I simply run:

At work:

$ flip_git_proxy
Git is using the proxy at http://host:post

At home:

$ flip_git_proxy
Git is not using a proxy

2 Comments

Well, I wanted a way to ignore the proxy during every command and only for the duration of the command. Anyways, I don't get the point of this script. Wouldn't I rather just do git config --global --unset http.proxy? Why do I need a script for that? Plus, I'd need another script for Windows, and I don't like cmd enough, and I don't know enough of PowerShell.
@JohnRed It doesn't really matter if you ignore the proxy only for the duration of certain commands, since the only time Git uses the network is during clone, fetch and push so you might as well set it at the beginning of your session. Personally, I found that running that simple script is faster than composing the commands to set/unset the proxy. Finally, that script works with Bash on Windows.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.