ChatGPT解决这个技术问题 Extra ChatGPT

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

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

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

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


C
Community

要添加另一种可能性,您可以define a proxy through the git config http.proxy

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

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

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

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

仅在与该指定站点交谈时才会关闭 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

仅指定

后,您可以获取该部分中所有变量的列表,其中包含适用于给定 URL 的值。例如

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

所有详细信息都在 commit 6a56993b 中:

http.<url>.*::

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

方案(例如,https://example.com/ 中的 https)。

主机/域名(例如,https://example.com/ 中的 example.com)。

端口号(例如,http://example.com:8080/ 中的 8080)。

路径(例如,https://example.com/repo.git 中的 repo.git)。

用户名(例如,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 都不参与匹配。


要使特定域不使用代理设置,您需要设置 http.proxy = ""
如何使用 *.mycompany.com 之类的通配符来匹配所有子域?
b
bbodenmiller

我通常使用环境变量:

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

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

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

笔记:

http_proxy 和 https_proxy 都必须使用代理的 http:// url(没有 https://)。

始终使用 proxydomain 的 fqn(全限定名)(不要依赖其短名称)

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

no_proxy=.my.company,localhost,127.0.0.1,::1,用于访问地址为 myrepo.my.company 或 localhost 的任何仓库。

您可以定义 NO_PROXYno_proxy,没关系。

但以防万一,我总是设置 HTTP_PROXYHTTPS_PROXYhttp_proxyhttps_proxyNO_PROXYno_proxy


就我而言,直到我尝试为 http 和 https 删除 http:// 后它才起作用。
no_proxy 不应该使用 * 来匹配,使用 .mycompany.com 代替会起作用。
@GuixingBai 你是对的。我已经相应地编辑了答案。
@xxxvodnikxxx 我的 answer below 有帮助吗?对于某台服务器,可以设置某台代理。
A
Artif3x

正如一些人在这里提到的,您可以通过指定 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 = ""

不客气。 +1。请注意,您可能已将 Windows 环境变量 NO_PROXY 设置为 my.internalgitserver.com,这足以不使用定义的代理:stackoverflow.com/a/27229920/6309
谢谢,VonC。我正在为各种代理工具(git、bower、npm)创建几个模板配置文件并将它们传递给团队成员。虽然您在 NO_PROXY 设置上工作是正确的,但在我的情况下这不是必需的,并且将所有这些都放在一个我可以分发的单个文件中的简单性就是我所追求的。
好点子。分发文件比环境变量更容易。
更好的是 - 分发脚本或直接 git 命令来应用上述内容。即git config --global http.proxy http://myproxy.net:8080,对于第一个等。然后您甚至不需要他们找到放置或更新文件的正确位置。
@LightCC:是的,那肯定行得通;但是,我的特定用例要求将用户的凭据添加到 URL:username:password@myproxy.net:8080,因此这会使脚本变得有些复杂,包括提示等。
j
jweyrich

您可以专门为每个遥控器调整各种配置选项。假设我们有 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

这将更改 本地存储库 配置 (.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

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

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

c
crifan

假设您的代理(如我的 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

添加: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 --取消设置 http.https://github.com.proxy

添加: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

或者

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

使用 socks5h 而不是 socks5 通过 SOCKS5 代理 (stackoverflow.com/a/16756248/5380322) 发送 DNS
A
Anurag Singh

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

外部 - 需要代理 内部 - 不需要代理

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

下面是配置命令:

使用代理设置全局配置

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

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

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

使用空代理设置本地配置

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

也可以通过其他方式完成。也就是说,您使用代理设置将全局配置保持为空和本地配置。

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

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

T
TheWhiteRabbit

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

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

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