仅对某些 git url/域使用代理?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16067534/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 15:58:35  来源:igfitidea点击:

Only use a proxy for certain git urls/domains?

gitgithubproxy

提问by robdodson

Is it possible to configure git to use a proxy only for specific domains?

是否可以将 git 配置为仅对特定域使用代理?

I'd like to use our corporate proxy to access Github but leave it off for accessing our own internal git repos.

我想使用我们的公司代理访问 Github,但不要使用它来访问我们自己的内部 git 存储库。

I'm using bower and it needs to require items both within our firewall and on github so I can't do this as a per project setting. It needs to be some kind of global configuration option. Any thoughts?

我正在使用 bower,它需要在我们的防火墙内和 github 上都需要项目,所以我不能按照每个项目设置来执行此操作。它需要是某种全局配置选项。有什么想法吗?

回答by VonC

To add another possibility, you can define a proxy through the git config http.proxy.

要添加另一种可能性,您可以通过git config http.proxy.

git config --global http.proxy http://mydomain\myusername:mypassword@myproxyserver:proxyport

But what is really neat is, starting git1.8.5 (October 2013), you can set http settings per url.

但真正巧妙的是,从 git1.8.5 (October 2013) 开始,您可以为每个 url 设置 http 设置

The "http.*" variables can now be specified per URL that the configuration applies.
For example,

http.*现在可以按配置应用的 URL 指定" " 变量
例如,

[http]
   sslVerify = true
[http "https://weak.example.com/"]
   sslVerify = false

would flip http.sslVerifyoff only when talking to that specified site.

http.sslVerify只有在与指定站点交谈时才会关闭。



See commit d4770964d5:

请参阅提交 d4770964d5

$ git config --bool --get-urlmatch http.sslVerify https://good.example.com
true
$ git config --bool --get-urlmatch http.sslVerify https://weak.example.com
false

With only <section>specified, you can get a list of all variables in the section with their values that apply to the given URL. E.g

使用 only<section>指定,您可以获得该部分中所有变量的列表,以及它们适用于给定 URL 的值。例如

$ git config --get-urlmatch http https://weak.example.com
http.sslverify false


All the details are in commit 6a56993b:

所有细节都在提交 6a56993b 中

http.<url>.*::

Any of the http.* options above can be applied selectively to some urls.
For a config key to match a URL, each element of the config key is compared to that of the URL, in the following order:

上面的任何 http.* 选项都可以有选择地应用于某些 url。
对于匹配 URL 的配置键,配置键的每个元素与 URL 的元素进行比较,按以下顺序:

  • Scheme(e.g., httpsin https://example.com/).
  • Host/domain name(e.g., example.comin https://example.com/).
  • Port number(e.g., 8080in http://example.com:8080/).
  • Path(e.g., repo.gitin https://example.com/repo.git).
  • User name(e.g., userin https://[email protected]/repo.git)
  • 方案(例如,httpshttps://example.com/)。
  • 主机/域名(例如,example.comhttps://example.com/)。
  • 端口号(例如,8080in http://example.com:8080/)。
  • 路径(例如,repo.gitin https://example.com/repo.git)。
  • 用户名(例如,userin https://[email protected]/repo.git

The list above is ordered by decreasing precedence; a URL that matches a config key's path is preferred to one that matches its user name.
For example, if the URL is https://[email protected]/foo/bara config key match of https://example.com/foowill be preferred over a config key match of https://[email protected].

All URLs are normalized before attempting any matching (the password part, if embedded in the URL, is always ignored for matching purposes) so that equivalent urls that are simply spelled differently will match properly.

Environment variable settings always override any matches.
The urls that are matched against are those given directly to Git commands.
This means any URLs +visited as a result of a redirection do not participate in matching.

上面的列表按优先级降序排列;匹配配置键路径的 URL 优先于匹配其用户名的 URL。
例如,如果 URL 是https://[email protected]/foo/bar的配置键匹配,https://example.com/foo则优先于 的配置键匹配https://[email protected]

在尝试任何匹配之前,所有 URL 都被规范化(密码部分,如果嵌入在 URL 中,为了匹配目的总是被忽略),以便简单拼写不同的等效 url 将正确匹配。

环境变量设置始终覆盖任何匹配项
匹配的 URL 是直接提供给 Git 命令的 URL。
这意味着由于重定向而访问的任何 URL 不参与匹配。

回答by VonC

I usually use the environment variables:

我通常使用环境变量:

  • http_proxy=http://username:password@proxydomain:port
  • https_proxy=http://username:password@proxydomain:port
  • http_proxy=http://username:password@proxydomain:port
  • https_proxy=http://username:password@proxydomain:port

That is picked up by git when accessing GitHub repo.

这是 git 在访问 GitHub 存储库时获取的。

Note:

笔记:

  • both http_proxyand https_proxymust use the http://url of the proxy (no https://).
  • always use the fqn (full qualified name)of proxydomain(don't rely on its short name)
  • 双方http_proxyhttps_proxy必须使用http://代理(无URL https://)。
  • 始终使用FQN(完全合格的名称)proxydomain(不依赖于它的简称)

But for internal repo, all I have to do is define and export one more environment variable:

但是对于内部存储库,我所要做的就是定义并导出另一个环境变量:

  • no_proxy=.my.company,localhost,127.0.0.1,::1, for accessing any repo with an address like myrepo.my.companyor localhost.
  • no_proxy=.my.company,localhost,127.0.0.1,::1, 用于访问具有类似myrepo.my.company或 localhost的地址的任何存储库。

You can define NO_PROXYor no_proxy, it doesn't matter.

你可以定义NO_PROXYor no_proxy,没关系。

But just in case, I always set HTTP_PROXY, HTTPS_PROXY, http_proxy, https_proxy, NO_PROXYand no_proxy.

但为了以防万一,我总是设置HTTP_PROXY, HTTPS_PROXY, http_proxy, https_proxy,NO_PROXYno_proxy

回答by Artif3x

As some have mentioned here, you can do this by specifying a URL along with the proxy settings, but Here's the use case that fixed this for me. Thanks to VonC above!

正如某些人在此处提到的,您可以通过指定 URL 和代理设置来完成此操作,但这是为我解决此问题的用例。感谢上面的 VonC!

Notice below that I'm specifying the root of the Git server. The full path to the git repo you've cloned isn't required. You can use a single entry to take care of the entire server.

请注意,我在下面指定了 Git 服务器的根目录。您克隆的 git 存储库的完整路径不是必需的。您可以使用单个条目来管理整个服务器。

Add the following to your .gitconfig file. On my windows system, I'm using %userprofile%\.gitconfig

将以下内容添加到您的 .gitconfig 文件中。在我的 Windows 系统上,我使用 %userprofile%\.gitconfig

[http]
    proxy = http://my.proxy.net:8080
[https]
    proxy = http://my.proxy.net:8443
[http "http://my.internalgitserver.com/"]
    proxy = ""

回答by jweyrich

You can adjust various configuration options for each remote specifically. Let's say we have 2 remotes, named originand upstreamrespectively. You adjust the proxy for each doing the following:

您可以专门为每个遥控器调整各种配置选项。假设我们有 2 个遥控器,分别命名为originupstream。您可以执行以下操作来调整每个代理:

git config --path remote.origin.proxy http://user:pass@proxy_for_origin:8080
git config --path remote.upstream.proxy http://user:pass@proxy_for_upstream:8080

This will change the sections of each remote inside your local repositoryconfiguration (.git/config).

这将更改本地存储库配置 ( .git/config)中每个遥控器的部分。

You can also adjust the global configurationoptions if you wish. Since it doesn't make sense to reference a remote name in the global config file ($HOME/.gitconfig), you can use url-matching(IIRC, supported since Git 1.8.5). Example:

如果您愿意,您还可以调整全局配置选项。由于在全局配置文件 ( $HOME/.gitconfig) 中引用远程名称没有意义,因此您可以使用url-matching(IIRC,自 Git 1.8.5 起支持)。例子:

[http "https://example.com/repo1.git"]
    proxy = http://user:pass@proxy1:8080
[http "https://example.com/repo2.git"]
    proxy = http://user:pass@proxy2:8080

If you want to see what's been set:

如果您想查看已设置的内容:

git config --path --get-urlmatch https://example.com/repo1.git
git config --path --get-urlmatch https://example.com/repo2.git

回答by crifan

assume your proxy is (like my ss) is : socks5://127.0.0.1:1086

假设您的代理(如我的ss)是:socks5://127.0.0.1:1086

config git proxy

配置git代理

  • for all
    • add: git config --global http.proxy socks5://127.0.0.1:1086
    • remove: git config --global --unset http.proxy
  • only for specific domain, eg: https://github.com
    • add: git config --global http.https://github.com.proxy socks5://127.0.0.1:1086
    • remove: git config --global --unset http.https://github.com.proxy
  • 对所有人
    • 添加git config --global http.proxy socks5://127.0.0.1:1086
    • 删除git config --global --unset http.proxy
  • 仅针对特定域,例如:https: //github.com
    • 添加git config --global http.https://github.com.proxy socks5://127.0.0.1:1086
    • 删除git config --global --unset http.https://github.com.proxy

Note

笔记

After added proxy, using

添加代理后,使用

cat ~/.gitconfig

cat ~/.gitconfig

can see related global config:

可以看到相关的全局配置:

[http]
        proxy = socks5://127.0.0.1:1086

or

或者

[http "https://github.com"]
        proxy = socks5://127.0.0.1:1086

回答by Anurag Singh

I had a similar problem, where i am behind my corporate proxy. I basically have two kinds of repositories:

我有一个类似的问题,我在我的公司代理后面。我基本上有两种存储库:

  1. External- which need proxy
  2. Internal- which don't need proxy
  1. 外部 - 需要代理
  2. 内部 - 不需要代理

I had to set a proxy in global config, which would act as the default, if not otherwise specified in local config.

我必须在全局配置中设置一个代理,如果没有在本地配置中另外指定,它将作为默认值。

So below are the commands for configuration:

所以下面是配置命令:

set global config with proxy

使用代理设置全局配置

git config --global --add http.proxy "http://username:password@proxydomain:port"
git config --global --add https.proxy "https://username:password@proxydomain:port"

then move to your local directory that contains your .git folder and for which you don't need proxy

然后移动到包含 .git 文件夹且不需要代理的本地目录

cd "C:\Users\username\directory_without_proxy\"

set local config with empty proxy

使用空代理设置本地配置

git config --local --add http.proxy ""
git config --local --add https.proxy ""

It could be done the other way too. That is, you keep the global config as empty and local config with your proxy settings.

也可以用另一种方式来完成。也就是说,您使用代理设置将全局配置保持为空和本地配置。

Just to double check you can use below command to list down the config settings for global and local respectively:

为了仔细检查,您可以使用以下命令分别列出全局和本地的配置设置:

git config --global --list
git config --local --list

回答by TheWhiteRabbit

On Windows , Simply [note without password] following worked for me

在 Windows 上,只需 [note without password] 以下对我有用

git config --global http.proxy http://mydomain\myusername:@myproxyserver:proxyport

git config --global https.proxy http://mydomain\myusername:@myproxyserver:proxyport