问题
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 git
ting at work than I do at home.
回答1:
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.
回答2:
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://proxy@github.com/"]
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://proxy@github.com/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.
回答3:
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
来源:https://stackoverflow.com/questions/41439747/how-to-bypass-the-http-proxy-that-i-have-set-using-git-config