如何设置和移除 Git 配置代理设置
Git 允许用户配置代理设置,以便通过代理服务器访问代码库。这在受限网络环境中尤其有用。本指南介绍如何在 Git 中配置和移除 HTTPS 和 SSH 连接的代理设置。
Git 使用 http.proxy 设置来配置 HTTP 或 HTTPS 代理。
-
打开终端或命令提示符。
-
使用以下命令设置 Git 代理:
1git config --global http.proxy http://proxyuser:[email protected]:port
2
3
-
将 proxyuser 替换为您的代理用户名。
-
将 proxypassword 替换为您的代理密码(如果需要)。
-
将 proxy.server.com 替换为代理服务器地址。
-
将 port 替换为端口号。
- 如果使用 HTTPS 代理,请使用:
1git config --global https.proxy https://proxyuser:[email protected]:port
2
3
运行以下命令检查代理是否已设置:
1git config --global --get http.proxy
2
3
如果设置正确,系统将显示代理地址。
Git 使用 SSH 进行安全连接。要使用 SSH 代理,请修改 SSH 配置文件。
- 打开 SSH 配置文件(如果不存在,请创建):
1nano ~/.ssh/config
2
3
- 添加以下行:
1Host github.com
2ProxyCommand nc -X connect -x proxy.server.com:port %h %p
3
4
-
将 proxy.server.com 和 port 替换为您的代理详细信息。
-
对于 Windows,请使用:
1Host github.com
2ProxyCommand connect -H proxy.server.com:port %h %p
3
4
-
保存并退出文件。
-
测试 SSH 连接:
1ssh -T git@github.com
2
3
移除 HTTP 和 HTTPS 代理设置:
1git config --global --unset https.proxy
2
3
验证移除是否成功:
1git config --global --get http.proxy
2git config --global --get https.proxy
3
4
如果没有返回任何内容,则表示代理已成功移除。
- 打开 SSH 配置文件:
1nano ~/.ssh/config
2
3
-
找到并移除或注释掉 ProxyCommand 命令行。
-
保存文件并测试:
1ssh -T git@github.com
2
3
-
确保代理服务器详细信息正确无误。
-
如果使用身份验证,请检查密码中的特殊字符(如有必要,请对其进行编码)。
-
使用以下命令测试连接:
1curl -x http://proxy.server.com:port https://github.com
2
3
- 如果设置应用于整个系统,请使用 --system 而不是 --global:
1git config --system --unset http.proxy
2
3
- 手动检查 Git 配置文件:
1nano ~/.gitconfig
2
3
并移除代理条目。
在 Git 中设置和移除代理对于在防火墙或公司网络后工作至关重要。按照本指南操作,您可以高效地配置和排除 HTTPS 和 SSH 连接的代理设置故障。
如果您遇到任何问题,请验证您的网络设置和代理凭据。
欲了解更多信息,您还可以阅读以下中文页面: https://gist.github.com/laispace/666dd7b27e9116faece6