配置代理以使用 Git

John Wachira 2023年1月30日
  1. Git 中支持的代理协议
  2. 在 Git 命令中使用 HTTP 代理
  3. 在 Git 命令中使用 HTTPS 代理
  4. 在 Git 配置文件中永久配置代理
  5. 删除 Git 存储库的代理设置
  6. 在 Git 中配置 SOCKS 代理
配置代理以使用 Git

本文将讨论配置 Git 以使用不同的代理。在公司防火墙后面工作时通常很难访问 Git。

我们将介绍 Git 支持的一些代理,如何配置它们,以及如何将代理提供给我们的命令行以供一次性使用。让我们直接开始吧。

Git 中支持的代理协议

Git 支持以下代理:

  1. HTTP
  2. HTTPS
  3. SOCKS4
  4. SOCKS5

在 Git 命令中使用 HTTP 代理

我们通常编辑 Git 配置文件以使用代理。但是,Git 允许我们将代理提供给终端以便快速使用。

这是命令行上的 HTTP 代理示例。

$ git config --global http.proxy http://username:password@proxy_server.com:port

在具有特定域的场景中,我们将运行如下所示的命令。

$ git config --global http.http://specific_domain.com.proxy http://username:password@proxy_server.com:port

请注意,我们在 http.proxy 之间添加了我们的域,并且我们提供了完整的代理 URL。完整的 URL 使 Git 可以自动识别和运行协议。

在 Git 命令中使用 HTTPS 代理

在我们在防火墙后面工作的场景中,以下是我们将如何运行 HTTPS 代理协议的命令。

$ git config --global http.https://specific_domain.com.proxy http://username:password@proxy_server.com:port

URL 介于 http.proxy 之间,但请注意从 http://specific_domain.comhttps://specific_domain.com 的更改。

你可以禁用 SSL 验证以避免出现问题,如下所示。

$ git config --global http.https://specific_domain.com.sslVerify false

在 Git 配置文件中永久配置代理

我们可以将上述设置永久存储在 Git 配置文件中。我们使用 --global 开关为所有用户和连接设置配置。

使用 --Global 开关设置全局代理

如果我们需要所有 Git 操作通过代理,我们运行:

$ git config --global http.proxy http://username:password@proxy_server.com:port

为特定域设置代理

如果我们希望特定域通过我们的代理连接,我们将运行以下命令。

$ git config --global http.https://domain.com.proxy http://username:password@proxy_server.com:port

禁用 HTTPS 验证

如果你收到此错误消息 无法访问 'https://...': Unknown SSL protocol error in connection to ...:443,你可以关闭 SSL 验证,如下所示。

$ git -c http.sslVerify=false clone https://domain.com/example.git

或者,你可以一起禁用它,如下所示。

$ git config http.sslVerify false

删除 Git 存储库的代理设置

我们使用下面的命令列出连接到我们存储库的所有代理。

$ git config --get-regexp http.*

我们可以使用以下任何语法来删除我们的代理。

$ git config --unset http.proxy
$ git config --unset http.https://domain.com.proxy

或者

$ git config --unset http.sslVerify
$ git config --unset http.https://domain.com.sslVerify

如下所示,我们可以添加 global 开关以从我们所有的存储库中删除代理设置。

$ git config --global --unset http.proxy
$ git config --global --unset http.https://domain.com.proxy

在 Git 中配置 SOCKS 代理

如前所述,Git 支持 socks5://socks4:// 协议。

运行以下命令来配置 SOCKS 协议。

$ git config --global http.proxy socks5://proxy_server.com:port

对于特定域,运行:

$ git config --global http.https://domain.com.proxy socks5://proxy_server.com:port
作者: John Wachira
John Wachira avatar John Wachira avatar

John is a Git and PowerShell geek. He uses his expertise in the version control system to help businesses manage their source code. According to him, Shell scripting is the number one choice for automating the management of systems.

LinkedIn

相关文章 - Git Config