丝滑git配置(简单但有效)

本文最后更新于 2024年6月5日 晚上

一般解决方案

查看 git 配置:

1
git config --list

先取消掉可能的全局配置:

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

配置:

1
2
git config --global http.proxy http://127.0.0.1:7890
git config --global https.proxy https://127.0.0.1:7890

配置后再去查看,结果如下(如何科学上网大伙应该心照不宣):

1
2
http.proxy=http://127.0.0.1:7890
https.proxy=https://127.0.0.1:7890

也可以直接在windows 系统上打开配置文件:

1
2
3
4
5
6
# linux
/etc/gitconfig
# windows
C:\Users\<用户名>\.gitconfig
# 只适用于特定仓库。
位于仓库目录下的 .git\config 文件

文件内容例如:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
[user]
name = k3ppf0r
email = 2094xx@qq.com
[credential "https://git.weixin.qq.com"]
provider = generic
[gui]
recentrepo = C:/Users/20942/Desktop/CVE-2024-27956

[difftool "sourcetree"]
cmd = "'' "
[mergetool "sourcetree"]
cmd = "'' "
trustExitCode = true

[http]
postBuffer = 524288000
proxy = http://127.0.0.1:7890
[https]
proxy = https://127.0.0.1:7890

配置完后即可轻松完成hexo deploy等部署。

我的理解:
貌似只用配http? 相当于git请求都会走本地的7890端口,从而用代理去访问,虽然本地明文,但我觉得这个方案已经可以满足需求了。

1
2
3
# 测试是否设置成功
ssh -T git@github.com
# Hi k3ppf0r! You've successfully authenticated, but GitHub does not provide shell access.

设置ssh 代理

配置

https代理存在一个局限,那就是没有办法做身份验证,每次拉取私库或者推送代码时,都需要输入github的账号和密码,非常痛苦。
设置ssh代理前,请确保你已经设置ssh key。可以参考在 github 上添加 SSH key 完成设置
更进一步是设置ssh代理。只需要配置一个config就可以了。

1
2
3
4
# Linux、MacOS
vi ~/.ssh/config
# Windows
到C:\Users\your_user_name\.ssh目录下,新建一个config文件(无后缀名)

将下面内容加到config文件中即可

对于windows用户,代理会用到connect.exe,你如果安装了Git都会自带connect.exe,如我的路径为C:\APP\Git\mingw64\bin\connect

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#Windows用户,注意替换你的端口号和connect.exe的路径
ProxyCommand "C:\APP\Git\mingw64\bin\connect" -S 127.0.0.1:51837 -a none %h %p

#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

保存后文件后测试方法如下,返回successful之类的就成功了。

1
2
3
# 测试是否设置成功
ssh -T git@github.com
# Hi k3ppf0r! You've successfully authenticated, but GitHub does not provide shell access.

之后都推荐走ssh拉取代码,再github 上选择clone地址时,选择ssh地址,入下图。这样git push 与 git clone 都可以直接走代理了,并且不需要输入密码。

原理部分

代理服务器就是你的电脑和互联网的中介。当您访问外网时(如http://google.com) , 你的请求首先转发到代理服务器,然后代理服务器替你访问外网,并将结果原封不动的给你的电脑,这样你的电脑就可以看到外网的内容啦。
路径如下:

你的电脑->代理服务器->外网
外网->代理服务器->你的电脑

参考


丝滑git配置(简单但有效)
https://k3ppf0r.github.io/2024/05/14/杂/丝滑git配置/
作者
k3ppf0r
发布于
2024年5月14日
许可协议