Skip to content

Instantly share code, notes, and snippets.

@chengr4
Forked from evantoli/GitConfigHttpProxy.md
Last active September 4, 2020 21:12
Show Gist options
  • Save chengr4/7e6728ea20bd4e7bad63aaf80ab6a910 to your computer and use it in GitHub Desktop.
Save chengr4/7e6728ea20bd4e7bad63aaf80ab6a910 to your computer and use it in GitHub Desktop.
Configure Git to use a proxy

設置 Git 來使用 proxy

狀況

如果從 remote repository clone 或 fetch 時遇到問題,或者遇到諸如以下錯誤,例如 unable to access '...' Couldn't resolve host '...' 則可能需要設置 proxy

例如:

git config --global http.proxy http://proxyUsername:[email protected]:port

又或者特殊的 domain,例如:

git config --global http.https://domain.com.proxy http://proxyUsername:[email protected]:port
git config --global http.https://domain.com.sslVerify false

設定 http.<url>.sslVerify to false 能幫助你快速實現,假如你的 workplace 使用 man-in-the-middle HTTPS proxying。
Longer term, you could get the root CA that they are applying to the certificate chain and specify it with either http.sslCAInfo or http.sslCAPath.

另請參見git-config documentation ,尤其是以下內容 部分(有關HTTPS / SSL問題)。

  • http.sslVerify
  • http.sslCAInfo
  • http.sslCAPath
  • http.sslCert
  • http.sslKey
  • http.sslCertPasswordProtected

In Detail

設置 proxy

您可以使用 --global 配置在 ~/.gitconfig 的file, 或者 local 在 .git/config file.

設定 global proxy

如果對所有 repos 的所有 access 都需要此proxy,請設置 global proxy

git config --global http.proxy http://proxyUsername:[email protected]:port

URL specific proxy

If you wish to specify that a proxy should be used for just some URLs that specify the URL as a git config subsection using http.<url>.key notation:

git config --global http.https://domain.com.proxy http://proxyUsername:[email protected]:port

Which will result in the following in the ~/.gitconfig file:

[http]
[http "https://domain.com"]
	proxy = http://proxyUsername:[email protected]:port

Handle subsequent SSL protocol errors

If you're still having trouble cloning or fetching and are now getting an unable to access 'https://...': Unknown SSL protocol error in connection to ...:443 then you may decide to switch off SSL verification for the single operation by using the -c http.sslVerify=false option

git -c http.sslVerify=false clone https://domain.com/path/to/git

Once cloned, you may decide set this for just this cloned repository's .git/config by doing. Notice the absence of the --global

git config http.sslVerify false

If you choose to make it global then limit it to a URL using the http.<url>.sslVerify notation:

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

Which will result in the following in the ~/.gitconfig file:

[http]
[http "https://domain.com"]
	proxy = http://proxyUsername:[email protected]:port
	sslVerify = false

Show current configuration

To show the current configuration of all http sections

git config --global --get-regexp http.*

If you are in a locally cloned repository folder then you drop the --global and see all current config:

git config --get-regexp http.*

Unset a proxy or SSL verification

Use the --unset flag to remove configuration being specific about the property -- for example whether it was http.proxy or http.<url>.proxy. Consider using any of the following:

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

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment