Git/代理:修订间差异
来自康健生活
< Git
| (未显示同一用户的16个中间版本) | |||
| 第1行: | 第1行: | ||
== 标题文本 == | |||
查看代理信息 | |||
{{shc|<syntaxhighlight lang="bash"> | |||
git config --global -l</syntaxhighlight>|$|/home/kangkk}} | |||
== 全局代理 == | == 全局代理 == | ||
{{shc|<syntaxhighlight lang="bash"> | {{shc|<syntaxhighlight lang="bash"> | ||
git config --global http.proxy http://192.168.51.34:808 | git config --global http.proxy http://192.168.51.34:808 | ||
git config --global https.proxy https://192.168.51.34:808</syntaxhighlight>|$|/home/kangkk}} | git config --global https.proxy https://192.168.51.34:808 | ||
git config --global http.proxy 'socks5://192.168.51.34:808' | |||
git config --global https.proxy 'socks5://192.168.51.34:808'</syntaxhighlight>|$|/home/kangkk}} | |||
参考 | |||
{| class="wikitable" | |||
|- | |||
! Git协议 !! 代理协议 !! 配置文件 !! 配置 | |||
|- | |||
| HTTP(s) || HTTP || <nowiki>~/.gitconfig</nowiki> || {{shc|<syntaxhighlight lang="bash"> | |||
git config --global http.proxy http://127.0.0.1:1080</syntaxhighlight>|$|/home/kangkk}} | |||
|- | |||
| HTTP(s) || SOCKS || <nowiki>~/.gitconfig</nowiki> || {{shc|<syntaxhighlight lang="bash"> | |||
git config --global http.proxy socks5h://127.0.0.1:1080</syntaxhighlight>|$|/home/kangkk}} | |||
|- | |||
| SSH || HTTP || <nowiki>~/.ssh/config</nowiki> || {{shc|<syntaxhighlight lang="bash"> | |||
Host github.com | |||
ProxyCommand nc -v -X connect -x 127.0.0.1:1080 %h %p</syntaxhighlight>|$|/home/kangkk}} | |||
|- | |||
| SSH || SOCKS || <nowiki>~/.ssh/config</nowiki> || {{shc|<syntaxhighlight lang="bash"> | |||
Host github.com | |||
ProxyCommand nc -v -X 5 -x 127.0.0.1:1080 %h %p</syntaxhighlight>|$|/home/kangkk}} | |||
|} | |||
并且 ssh 代理还是要区分操作系统。 | |||
取消代理 | 参考 https://zhuanlan.zhihu.com/p/481574024 | ||
=== 取消代理 === | |||
{{shc|<syntaxhighlight lang="bash"> | {{shc|<syntaxhighlight lang="bash"> | ||
| 第10行: | 第42行: | ||
git config --global --unset https.proxy</syntaxhighlight>|$|/home/kangkk}} | git config --global --unset https.proxy</syntaxhighlight>|$|/home/kangkk}} | ||
=== 分流 | === 代理服务器鉴权 === | ||
{{shc|<syntaxhighlight lang="bash"> | |||
git config --global https.proxy https://username:password@proxy.abc.com:808 | |||
</syntaxhighlight>|$|/home/kangkk}} | |||
需要注意的是账号密码信息用 Percent-encoding(百分号编码) 编码格式转换,以避免特殊符号如 @ 等干扰命令字符串解析<ref>https://stackoverflow.com/questions/6172719/escape-character-in-git-proxy-password</ref>。 | |||
== 分流 == | |||
只对特定仓库进行代理 | 只对特定仓库进行代理 | ||
| 第27行: | 第66行: | ||
[http] | [http] | ||
proxy = socks5://192.168.51.34:1080 | proxy = socks5://192.168.51.34:1080 | ||
proxy = http://192.168.51.34: | proxy = http://192.168.51.34:808 | ||
[https] | [https] | ||
proxy = socks5://192.168.51.34:1080 | proxy = socks5://192.168.51.34:1080 | ||
proxy = | proxy = http://192.168.51.34:808 | ||
</syntaxhighlight>}} | </syntaxhighlight>}} | ||
{{shc|<syntaxhighlight lang="bash"> | {{shc|<syntaxhighlight lang="bash"> | ||
git config -l --global</syntaxhighlight>|$|/home/kangkk}} | git config -l --global</syntaxhighlight>|$|/home/kangkk}} | ||
可以通过域名使用 http.proxy 进行有条件的代理设置。例如: | |||
{{hc|head=~/.gitconfig|output=<syntaxhighlight lang="bash"> | |||
[http "https://example.com/"] | |||
http.proxy = socks5://127.0.0.1:1080 | |||
</syntaxhighlight>}} | |||
{{shc|<syntaxhighlight lang="bash"> | |||
git config -l --global</syntaxhighlight>|$|/home/kangkk}} | |||
== ssh == | |||
ssh 代理,先查看 DNS 解析 | |||
{{Shc_out|<syntaxhighlight lang="bash"> | |||
nslookup github.com</syntaxhighlight>|<syntaxhighlight lang="text">服务器: unifi.localdomain | |||
Address: 192.168.51.1 | |||
非权威应答: | |||
名称: github.com | |||
Address: 20.205.243.166 | |||
</syntaxhighlight>|$|/home/kangkk}} | |||
=== windows === | |||
配置文件 | |||
{{hc|head=~/.ssh/config|output=<syntaxhighlight lang="bash"> | |||
## github proxy | |||
# | |||
# 别名 | |||
Host github.com | |||
User git | |||
# 目标端口号 | |||
Port 22 | |||
# 目标主机名 | |||
HostName github.com | |||
# 指定的私钥地址 | |||
IdentityFile "C:\Users\kwy\.ssh\id_ed25519" | |||
TCPKeepAlive yes | |||
# git bash 包含 connect.exe | |||
# -S 使用 socket | |||
ProxyCommand "C:\Program Files\Git\mingw64\bin\connect.exe" -S 192.168.51.34:1080 -a none %h %p | |||
Host ssh.github.com | |||
User git | |||
Port 22 | |||
HostName ssh.github.com | |||
IdentityFile "C:\Users\kwy\.ssh\id_ed25519" | |||
TCPKeepAlive yes | |||
ProxyCommand "C:\Program Files\Git\mingw64\bin\connect.exe" -S 192.168.51.34:1080 -a none %h %p</syntaxhighlight>}} | |||
其中 <code>connect -S</code> 指定使用 socket 服务进行代理 | |||
测试联通 | |||
{{shc|<syntaxhighlight lang="bash"> | |||
ssh -T git@github.com</syntaxhighlight>|$|/home/kangkk}} | |||
=== Linux === | |||
{{Linenum|1}} 使用系统内置 nc 命令。 | |||
{{hc|head=/home/kwy/.ssh/config|output=<syntaxhighlight lang="bash"> | |||
Host github.com | |||
User git | |||
Port 22 | |||
Hostname github.com | |||
PreferredAuthentications publickey | |||
IdentityFile "/home/kwy/.ssh/id_ed25519" | |||
TCPKeepAlive yes | |||
ProxyCommand nc -X connect -x 192.168.51.34:808 %h %p | |||
Host ssh.github.com | |||
User git | |||
Port 22 | |||
HostName ssh.github.com | |||
PreferredAuthentications publickey | |||
IdentityFile "/home/kwy/.ssh/id_ed25519" | |||
TCPKeepAlive yes | |||
ProxyCommand nc -X connect -x 192.168.51.34:808 %h %p | |||
</syntaxhighlight>}} | |||
或者使用 socket 服务 <code>ProxyCommand nc -X 5 -x 192.168.51.34:1080 %h %p</code> | |||
{{Linenum|2}} 安装 connect 工具(windows 系统安装 git bash 自带 connect)。 | |||
2025年7月29日 (二) 16:03的最新版本
标题文本
查看代理信息
/home/kangkk
git config --global -l
全局代理
/home/kangkk
git config --global http.proxy http://192.168.51.34:808
git config --global https.proxy https://192.168.51.34:808
git config --global http.proxy 'socks5://192.168.51.34:808'
git config --global https.proxy 'socks5://192.168.51.34:808'
参考
| Git协议 | 代理协议 | 配置文件 | 配置 |
|---|---|---|---|
| HTTP(s) | HTTP | ~/.gitconfig | /home/kangkk git config --global http.proxy http://127.0.0.1:1080
|
| HTTP(s) | SOCKS | ~/.gitconfig | /home/kangkk git config --global http.proxy socks5h://127.0.0.1:1080
|
| SSH | HTTP | ~/.ssh/config | /home/kangkk Host github.com
ProxyCommand nc -v -X connect -x 127.0.0.1:1080 %h %p
|
| SSH | SOCKS | ~/.ssh/config | /home/kangkk Host github.com
ProxyCommand nc -v -X 5 -x 127.0.0.1:1080 %h %p
|
并且 ssh 代理还是要区分操作系统。
参考 https://zhuanlan.zhihu.com/p/481574024
取消代理
/home/kangkk
git config --global --unset http.proxy
git config --global --unset https.proxy
代理服务器鉴权
/home/kangkk
git config --global https.proxy https://username:password@proxy.abc.com:808
需要注意的是账号密码信息用 Percent-encoding(百分号编码) 编码格式转换,以避免特殊符号如 @ 等干扰命令字符串解析[1]。
分流
只对特定仓库进行代理
/home/kangkk
git config --global http.https://github.com.proxy http://192.168.51.34:808
git config --global https.https://github.com.proxy http://192.168.51.34:808
或者使用 socks
/home/kangkk
git config --global http.https://github.com.proxy socks5://192.168.51.34:1080
git config --global https.https://github.com.proxy socks5://192.168.51.34:1080
配置文件
~/.gitconfig
[http]
proxy = socks5://192.168.51.34:1080
proxy = http://192.168.51.34:808
[https]
proxy = socks5://192.168.51.34:1080
proxy = http://192.168.51.34:808
/home/kangkk
git config -l --global
可以通过域名使用 http.proxy 进行有条件的代理设置。例如:
~/.gitconfig
[http "https://example.com/"]
http.proxy = socks5://127.0.0.1:1080
/home/kangkk
git config -l --global
ssh
ssh 代理,先查看 DNS 解析
[jian@kang /home/kangkk]$
nslookup github.com
服务器: unifi.localdomain
Address: 192.168.51.1
非权威应答:
名称: github.com
Address: 20.205.243.166
windows
配置文件
~/.ssh/config
## github proxy
#
# 别名
Host github.com
User git
# 目标端口号
Port 22
# 目标主机名
HostName github.com
# 指定的私钥地址
IdentityFile "C:\Users\kwy\.ssh\id_ed25519"
TCPKeepAlive yes
# git bash 包含 connect.exe
# -S 使用 socket
ProxyCommand "C:\Program Files\Git\mingw64\bin\connect.exe" -S 192.168.51.34:1080 -a none %h %p
Host ssh.github.com
User git
Port 22
HostName ssh.github.com
IdentityFile "C:\Users\kwy\.ssh\id_ed25519"
TCPKeepAlive yes
ProxyCommand "C:\Program Files\Git\mingw64\bin\connect.exe" -S 192.168.51.34:1080 -a none %h %p
其中 connect -S 指定使用 socket 服务进行代理
测试联通
/home/kangkk
ssh -T git@github.com
Linux
1 使用系统内置 nc 命令。
/home/kwy/.ssh/config
Host github.com
User git
Port 22
Hostname github.com
PreferredAuthentications publickey
IdentityFile "/home/kwy/.ssh/id_ed25519"
TCPKeepAlive yes
ProxyCommand nc -X connect -x 192.168.51.34:808 %h %p
Host ssh.github.com
User git
Port 22
HostName ssh.github.com
PreferredAuthentications publickey
IdentityFile "/home/kwy/.ssh/id_ed25519"
TCPKeepAlive yes
ProxyCommand nc -X connect -x 192.168.51.34:808 %h %p
或者使用 socket 服务 ProxyCommand nc -X 5 -x 192.168.51.34:1080 %h %p
2 安装 connect 工具(windows 系统安装 git bash 自带 connect)。
No categories