本文最后更新于 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
|
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
|
其他
- linux下也可以通过
export ALL_PROXY=127.0.0.1:7890
,配置全局代理。
connect
命令来自git,位置在C:\Program Files\Git\mingw64\bin\connect.exe
。
ubuntu下可以通过sudo apt install connect-proxy
安装。
connect
命令参考
1 2 3 4 5 6 7 8 9
|
connect -H 127.0.0.1:10808 github.com 443
connect -S 127.0.0.1:10808 github.com 443
|
参考
- https://github.com/larryhou/connect-proxy
- https://git-scm.com/docs/git-config/2.16.6
- https://gist.github.com/alphamarket/e0ed48f8755bebdc7451b758bc6828fa
- https://gist.github.com/ozbillwang/005bd1dfc597a2f3a00148834ad3e551