设置Git代理(Git's proxy)
对于无法直接访问的网址,通常需要为git设置代理,实现间接访问。
设置Git代理(Git's proxy)
写在前面
正文内容,加了--global 是全局配置;要单独为某一 项目/仓库 配置代理,需要进入项目目录,并去掉命令里的 --global。
来自git官方文档:Override the HTTP proxy, normally configured using the http_proxy, https_proxy, and all_proxy environment variables (see curl(1)
).
覆盖HTTP代理,通常使用HTTP_proxy、https_proxy和all_proxy环境变量进行配置(参见curl(1)
)。
设置HTTP/HTTPS协议代理
设置 HTTP 代理
设置代理的命令格式:
1
2
git config --global http.proxy [协议类型://][用户名[:密码]@]代理地址[:端口号][/路径]
git config --global http.proxy [protocol://][user[:password]@]proxyhost[:port][/path]
设置Git的 HTTP proxy
,常用命令:
1
2
git config --global http.proxy 代理地址:端口号
git config --global http.proxy http://代理地址:端口号
例如:
1
git config --global http.proxy 127.0.0.1:8889
如果要使用的Git代理需要身份验证,请使用以下命令配置HTTP代理:
1
git config --global http.proxy 用户名:密码@代理地址:端口号
设置 HTTPS 代理
设置Git的 HTTPS proxy
,常用命令:
1
2
git config --global https.proxy 代理地址:端口号
git config --global https.proxy https://代理地址:端口号
例如:
1
git config --global https.proxy 127.0.0.1:8889
如果要使用的Git代理需要身份验证,请使用以下命令配置HTTPS代理:
1
git config --global https.proxy 用户名:密码@代理地址:端口号
检查当前代理配置
1
2
3
4
git config --list
git config --global --list
git config --global --get http.proxy
git config --global --get https.proxy
取消代理
取消 HTTP proxy
1
git config --global --unset http.proxy
取消 HTTPS proxy
1
git config --global --unset http.proxy
设置SSH协议代理
为什么配置ssh代理
类似如下仓库地址,git使用SSH协议进行连接:
1
git@github.com:JiuYu77/JiuYu77.github.io.git
由于 Git 通过 SSH进行连接,因此需要在 ssh配置文件 ~/.ssh/config
(没有则新建即可)中,通过ProxyCommand
配置代理。
假设 socks 代理端口为1080,http/https 代理端口为8889,也有可能使用同一个端口。
Linux 配置
1
2
3
4
5
Host github.com
Hostname github.com
User git
Port 22
ProxyCommand nc -x 127.0.0.1:1080 %h %p
1
2
3
4
5
# ProxyCommand 使用示例:
ProxyCommand nc -x 127.0.0.1:1080 %h %p # socks
ProxyCommand nc -X 4 -x 127.0.0.1:1080 %h %p
ProxyCommand nc -X 5 -x 127.0.0.1:1080 %h %p
ProxyCommand nc -X connect -x 127.0.0.1:8889 %h %p
nc
是一个软件,后面是 nc 命令的参数。-X
是协议,取值为4、5 或 connect,socks协议使用4或5,connect协议对应 http 或 https,默认为 socks协议。-x
后面是ip和port。1080
和8889
是端口号,改为自己使用的端口,与使用的协议对应。
Windows 配置
1
2
3
4
5
Host github.com
Hostname github.com
User git
Port 22
ProxyCommand connect -S 127.0.0.1:1080 %h %p
-S
为socks,-H
为 http。1080
是端口号,需替换为实际代理端口。
设置Git协议的代理
如果远程仓库的链接是如下形式,则为Git协议:
1
git://github.com/test/learngit.git
1
2
git config --global core.gitProxy "git-proxy"
git config --global socks.proxy "localhost:1080"
本文由作者按照 CC BY 4.0 进行授权