Git代理设置
Git 的传输协议
| 协议类型 | 优点 | 缺点 |
| :——–| :—- | :—- |
| http | 1.可以使用用户名和密码授权,不需要生成ssh密钥后上传公钥,使用方便快速;
2.一般公司会开放80和433这两个http和http协议的端口,不会有安全限制; |1. 上传和下载速度慢
2. 每次push都需要输入口令;
| SSH (Secure Shell) | 1.部署和使用容易,SSH作为常用的网络验证验证授权的协议,非常普及; | 1.SSH协议一般使用的22端口在公司过不了防火墙;
2.不可匿名访问,不利于开源项目;
—
1. 设置HTTP代理
1.1 全局设置(不推荐)
#使用http代理
git config --global http.proxy http://127.0.0.1:58591
git config --global https.proxy https://127.0.0.1:58591
#使用socks5代理
git config --global http.proxy socks5://127.0.0.1:51837
git config --global https.proxy socks5://127.0.0.1:51837
1.2 单独代理github.com (推荐)
#使用socks5代理(推荐)
git config --global http.https://github.com.proxy socks5://127.0.0.1:51837
#使用http代理(不推荐)
git config --global http.https://github.com.proxy http://127.0.0.1:58591
1.3 取消代理
当你不需要使用代理时,可以取消之前设置的代理。
git config --global --unset http.proxy git config --global --unset https.proxy
2. 设置SSH代理(终极解决方案)
https代理存在一个局限,那就是没有办法做身份验证,每次拉取私库或者推送代码时,都需要输入github的账号和密码,非常痛苦。
提示: 设置ssh代理前,请确保你已经设置ssh key。可以参考在github 上添加 SSH key完成设置。
更进一步是设置ssh代理。只需要配置一个config就可以了。
Linux、MacOS、Windows(GitBash):
vi ~/.ssh/config
然后输入i进入编辑模式并且粘贴下面的内容:
#Windows用户,注意替换你的端口号和connect.exe的路径
ProxyCommand "C:\Program Files\Git\\mingw64\bin\connect.exe" -S 127.0.0.1:51837 -a none %h %p
#Linux和MacOS用户用下方这条命令,注意替换你的端口号
#ProxyCommand nc -v -x 127.0.0.1:51837 %h %p
Host github.com
User git
Port 22
Hostname github.com
# 注意修改路径为你的路径
IdentityFile "C:\Users\Your_User_Name\.ssh\id_rsa"
TCPKeepAlive yes
Host ssh.github.com
User git
Port 443
Hostname ssh.github.com
# 注意修改路径为你的路径
IdentityFile "C:\Users\Your_User_Name\.ssh\id_rsa"
TCPKeepAlive yes
然后按Esc退出编辑模式然后输入:wq保存并且退出vi编辑器。
保存后文件后执行下面代码进行测试,返回successful之类的就成功了。
# 测试是否设置成功
ssh -T git@github.com
之后都推荐走SSH拉取代码,再GitHub 上选择clone地址时,选择ssh地址,入下图。这样git push 与 git clone 都可以直接走代理了,并且不需要输入密码。

参考链接: Git设置代理解决被墙