Git配置代理

本文最后更新于 2025年4月15日 下午

配置git使用代理克隆代码

问题来源

git clone代码时,遇到unable to access '...',Couldn't resolve host '...', 可以配置代理解决。
遇到unable to access 'https://...': Unknown SSL protocol error in connection to ...:443, 可以关闭sslVerify解决

命令行配置

1. 配置git的http代理

查看代理

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

配置代理

1
2
3
4
5
6
# 格式 http://proxyUsername:proxyPassword@proxy.server.com:port
# 全局代理
git config --global http.proxy http://127.0.0.1:7890
# 域名代理
git config --global http.https://github.com.proxy http://http://127.0.0.1:7890
git config --global http.https://github.com.sslVerify false

取消配置

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

2. 使用GIT_SSH_COMMAND 环境变量配置git的ssh代理

1
2
3
4
5
6
7
8
9
10
# 临时配置代理
export GIT_SSH_COMMAND='ssh -o ProxyCommand="connect -S 127.0.0.1:7890 %h %p"'
# 配置全局
git config --global core.sshCommand 'ssh -o ProxyCommand="connect -S 127.0.0.1:7890 %h %p"'
# 配置本次命令
git clone -c=core.sshCommand 'ssh -o ProxyCommand="connect -S 127.0.0.1:7890 %h %p"' git@github.com:user/repo.git
# 配置本地仓库
git config core.sshCommand 'ssh -o ProxyCommand="connect -S 127.0.0.1:7890 %h %p"'
# 配置全局仓库
git config --global core.sshCommand 'ssh -o ProxyCommand="connect -S 127.0.0.1:7890 %h %p"'

配置文件

1. http克隆

修改~/.ssh/config配置文件
windows:C:\Users\用户名\.gitconfig
linux:~/.gitconfig

1
2
3
[http]
[http "https://github.com"]
proxy = http://127.0.0.1:7890

2. ssh代理

修改~/.ssh/config配置文件
windows

windows下创建配置文件

$sshDir = "$env:USERPROFILE\.ssh"
$configFile = Join-Path $sshDir "config"

# Create .ssh directory if it doesn’t exist
if (-not (Test-Path $sshDir)) {
New-Item -Path $sshDir -ItemType Directory | Out-Null
}

# Create config file if it doesn’t exist
if (-not (Test-Path $configFile)) {
New-Item -Path $configFile -ItemType File | Out-Null
}

notepad $env:USERPROFILE\.ssh\config

1
2
3
4
5
Host github.com
Hostname github.com
ServerAliveInterval 55
ForwardAgent yes
ProxyCommand connect -H 127.0.0.1:7890 %h %p

linux:
mkdir ~/.ssh && vim ~/.ssh/config

1
2
3
4
5
Host github.com
Hostname github.com
ServerAliveInterval 55
ForwardAgent yes
ProxyCommand nc -x 127.0.0.1:7890 %h %p

其他

  1. linux下也可以通过export ALL_PROXY=127.0.0.1:7890,配置全局代理。
  2. connect命令来自git,位置在C:\Program Files\Git\mingw64\bin\connect.exe
    ubuntu下可以通过sudo apt install connect-proxy安装。
  3. connect命令参考
1
2
3
4
5
6
7
8
9
#  connect.exe [-dnhst45] [-p local-port]
# [-H proxy-server[:port]] [-S [user@]socks-server[:port]]
# [-T proxy-server[:port]]
# [-c telnet-proxy-command]
# host port
# -H 指定http代理服务
connect -H 127.0.0.1:10808 github.com 443
# -S 指定SOCKS 代理服务器
connect -S 127.0.0.1:10808 github.com 443

参考

  1. https://github.com/larryhou/connect-proxy
  2. https://git-scm.com/docs/git-config/2.16.6
  3. https://gist.github.com/alphamarket/e0ed48f8755bebdc7451b758bc6828fa
  4. https://gist.github.com/ozbillwang/005bd1dfc597a2f3a00148834ad3e551

Git配置代理
https://blog.cook369.xyz/2025/04/15/git-proxy/
作者
likp
发布于
2025年4月15日
更新于
2025年4月15日
许可协议