HOWTO · Git

配置代理以使用 Git

在本文中,我們將討論配置 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