Published on

如何为Git设置代理

Authors
  • Name
    Twitter

在某些情况下,你可能需要为Git设置代理以便顺利进行远程操作。本文将指导你如何为Git配置代理,包括全局设置和针对特定仓库的设置。

全局代理设置

要为Git设置全局代理,请使用以下命令:

git config --global http.proxy http://proxyserver:port
git config --global https.proxy https://proxyserver:port

proxyserverport替换为你的代理服务器地址和端口。

例如,如果你使用本地运行的代理软件(如ClashX),通常的设置可能是:

git config --global http.proxy http://127.0.0.1:7890
git config --global https.proxy http://127.0.0.1:7890

针对特定仓库的代理设置

如果你只想为特定仓库设置代理,进入该仓库的目录,然后运行以下命令(不带--global选项):

git config http.proxy http://proxyserver:port
git config https.proxy https://proxyserver:port

验证代理设置

要检查当前的代理设置,使用以下命令:

git config --global --get http.proxy
git config --global --get https.proxy

如果是针对特定仓库,去掉--global选项。

使用SOCKS5代理

如果你的代理服务器支持SOCKS5协议,可以这样设置:

git config --global http.proxy socks5://proxyserver:port
git config --global https.proxy socks5://proxyserver:port

取消代理设置

要取消全局代理设置,使用以下命令:

git config --global --unset http.proxy
git config --global --unset https.proxy

对于特定仓库,在仓库目录中运行相同的命令,但不带--global选项。

临时使用代理

如果你只想在单个Git命令中使用代理,可以这样做:

git -c http.proxy=http://proxyserver:port clone https://github.com/example/repo.git