设置Git走本地代理,加速GitHub下载速度(Git HTTPS方式代理)

在平时使用Git从GitHub上clone项目等,经常是几十KB/S,速度简直“令人发指”,即便使用工具全局代理也没用。但是用网页端走代理从GitHub上下载速度就很快速,这是因为git命令并不会直接走全局代理,需要通过git config配置,使用Git软件或者CMD都可以,也可编辑配置文件.gitconfig来修改,命令如下:

只对github进行代理,不会影响国内仓库(推荐):

端口号依据自己使用的工具情况修改

# socks5协议,1080端口修改成自己的本地代理端口
git config --global http.https://github.com.proxy socks5://127.0.0.1:1080
git config --global https.https://github.com.proxy socks5://127.0.0.1:1080

# http协议,1081端口修改成自己的本地代理端口
git config --global http.https://github.com.proxy https://127.0.0.1:1081
git config --global https.https://github.com.proxy https://127.0.0.1:1081

全局代理:

# socks5协议,1080端口修改成自己的本地代理端口
git config --global http.proxy socks5://127.0.0.1:1080
git config --global https.proxy socks5://127.0.0.1:1080

# http协议,1081端口修改成自己的本地代理端口
git config --global http.proxy http://127.0.0.1:1081
git config --global https.proxy https://127.0.0.1:1081

取消代理及查看配置:

# 取消代理设置
git config --global --unset http.proxy
git config --global --unset https.proxy

其他命令

# 查看所有配置
git config -l
# 查看版本
git --version
# Windows版本更新
git update-git-for-windows

配置文件.gitconfig的位置一般为:C:\Users\用户名\.gitconfig,或直接用神器Everything搜索即可。

git log退出方法

使用git log之后无法回到主页面,如下图所示,最后只能暴力关闭git bash。
解决方法其实很简单,输入字母Q即可退出🤣。

Windows 下 Git SSH 连接方式配置 Socks 代理

Git 已经对 HTTPS 方式的代理支持的很好了,设置 http 和 socks 代理都是支持的,如果你需要使用 SSH 方式,需要如下配置。
~/.ssh文件夹下新建config文件,配置如下代码,两个方法任选其一,端口根据各自软件修改。

方法一

Host github.com *.github.com
    #设置代理端口-S为sock5端口,-H为http端口
    ProxyCommand connect -S http://127.0.0.1:10808 %h %p
	#配置私钥文件路径
	IdentityFile ~/.ssh/id_rsa 
    User git

方法二

# 这里的 -a none 是 NO-AUTH 模式,参见https://bitbucket.org/gotoh/connect/wiki/Home 中的 More detail 一节。
ProxyCommand connect -S 127.0.0.1:10808 -a none %h %p

Host github.com
  User git
  Port 22
  Hostname github.com
  # 配置私钥文件路径
  IdentityFile ~/.ssh/id_rsa
  TCPKeepAlive yes

Host ssh.github.com
  User git
  Port 443
  Hostname ssh.github.com
  # 配置私钥文件路径
  IdentityFile ~/.ssh/id_rsa
  TCPKeepAlive yes

Git GUI客户端推荐

TortoiseGit,Windows端开源软件。功能很全的GUI版本,近乎Git的命令都有在GUI上提现。相比Git自带的GUI版本,还是好用许多的。软件有中文语言包,安装即可。(GitGUI也有人制作了汉化文件,按要求将汉化文件添加到指定位置,即可使用,很方便。)