仅对某些git urls/domains使用代理?

rob*_*son 53 git proxy github

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

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

我正在使用凉亭,它需要在我们的防火墙和github上都需要物品,所以我不能将它作为每个项目设置.它需要是某种全局配置选项.有什么想法吗?

Von*_*onC 64

为了增加另一种可能性,您可以通过以下git config http.proxy方式定义代理.

git config --global http.proxy http://mydomain\\myusername:mypassword@myproxyserver:proxyport
Run Code Online (Sandbox Code Playgroud)

但真正的是,从git1.8.5(2013年10月)开始,你可以设置每个网址的http设置.

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

[http]
   sslVerify = true
[http "https://weak.example.com/"]
   sslVerify = false
Run Code Online (Sandbox Code Playgroud)

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


请参阅commit 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
Run Code Online (Sandbox Code Playgroud)

只有<section>指定,您才能获得该部分中所有变量的列表及其适用于给定URL的值.例如

$ git config --get-urlmatch http https://weak.example.com
http.sslverify false
Run Code Online (Sandbox Code Playgroud)

所有细节都在提交6a56993b中:

http.<url>.*::
Run Code Online (Sandbox Code Playgroud)

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

  • 方案(例如,httpsin https://example.com/).
  • 主机/域名(例如,example.comin https://example.com/).
  • 端口号(例如,8080in http://example.com:8080/).
  • 路径(例如,repo.gitin https://example.com/repo.git).
  • 用户名(例如,userin https://user@example.com/repo.git)

上面的列表按优先顺序排序; 与配置密钥的路径匹配的URL优先于与其用户名匹配的URL.
例如,如果URL是https://user@example.com/foo/bar配置密钥匹配,https://example.com/foo则优先于配置密钥匹配https://user@example.com.

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

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

  • 如何使用像 *.mycompany.com 这样的通配符来匹配所有子域? (2认同)

Von*_*onC 57

我通常使用环境变量:

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

当访问GitHub repo时,git会选择它.

注意:

  • 双方http_proxyhttps_proxy必须使用http://代理(无URL https://).
  • 始终使用FQN(完全合格的名称)proxydomain(不依赖于它的简称)

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

  • no_proxy=.my.company,localhost,127.0.0.1,::1,用于访问任何地址类似myrepo.my.company或本地主机的仓库.

你可以定义,NO_PROXY或者no_proxy没关系.

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


Art*_*f3x 33

正如一些人在这里提到的,你可以通过指定一个URL和代理设置来实现,但这是为我修复此问题的用例.感谢上面的VonC!

请注意,我正在指定Git服务器的根目录.您已克隆的git仓库的完整路径不是必需的.您可以使用单个条目来处理整个服务器.

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

[http]
    proxy = http://my.proxy.net:8080
[https]
    proxy = http://my.proxy.net:8443
[http "http://my.internalgitserver.com/"]
    proxy = ""
Run Code Online (Sandbox Code Playgroud)

  • 别客气.+1.请注意,您可能已将Windows环境变量"NO_PROXY"设置为"my.internalgitserver.com",这可能已足以不使用已定义的代理:http://stackoverflow.com/a/27229920/6309 (3认同)
  • 更好的是 - 分发脚本或直接 git 命令来应用上述内容。即`git config --global http.proxy http://myproxy.net:8080`,对于第一个,等等。然后你甚至不需要它们来找到放置或更新文件的正确位置。 (2认同)

jwe*_*ich 14

您可以专门为每个遥控器调整各种配置选项.假设我们有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
Run Code Online (Sandbox Code Playgroud)

这将更改本地存储库配置(.git/config)中每个远程的部分.

如果愿意,您还可以调整全局配置选项.由于在全局配置文件($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
Run Code Online (Sandbox Code Playgroud)

如果你想看看已经设置了什么:

git config --path --get-urlmatch https://example.com/repo1.git
git config --path --get-urlmatch https://example.com/repo2.git
Run Code Online (Sandbox Code Playgroud)


cri*_*fan 8

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

配置git代理

  • 对所有人
    • 添加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

笔记

添加代理后,使用

cat ~/.gitconfig

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

[http]
        proxy = socks5://127.0.0.1:1086
Run Code Online (Sandbox Code Playgroud)

或者

[http "https://github.com"]
        proxy = socks5://127.0.0.1:1086
Run Code Online (Sandbox Code Playgroud)