Git 代理配置完全指南

这篇文章梳理 Git 中所有代理配置方式,从简单的 git config 到 SSH 协议的 SOCKS5 隧道,一份指南全搞定。 为什么需要代理 访问 GitHub、GitLab 等平台时,有时会遇到 git clone 速度极慢甚至超时的情况。尤其是在国内网络环境下,给 Git 配置代理几乎是必备技能。 flowchart LR A[你的电脑] --> B{需要代理?} B -->|直连通畅| C[直接连接 Git 服务器] B -->|速度慢/超时| D[代理服务器] D --> E[Git 服务器github.comgitlab.com] C --> E 配置方式总览 Git 提供多种配置代理的途径,优先级从高到低: 配置方式 持久性 作用范围 -c 命令行参数 单次命令 单仓库 git config --local 写入 .git/config 单个仓库 git config --global 写入 ~/.gitconfig 当前用户 环境变量 Shell 会话级 当前终端 graph TD A[Git 发起网络请求] --> B{有 -c 参数?} B -->|是| F[使用 -c 指定的代理] B -->|否| C{有 local 配置?} C -->|是| G[使用 local 代理] C -->|否| D{有 global 配置?} D -->|是| H[使用 global 代理] D -->|否| E{有环境变量?} E -->|是| I[使用环境变量代理] E -->|否| J[直连] 一、HTTP/HTTPS 代理 这是最常用的方式,适用于 https:// 协议的仓库。 1.1 配置命令 # 全局设置 git config --global http.proxy http://127.0.0.1:7890 git config --global https.proxy http://127.0.0.1:7890 # 仅对当前仓库设置 git config --local http.proxy http://127.0.0.1:7890 # 查看当前配置 git config --global --get http.proxy # 取消代理 git config --global --unset http.proxy git config --global --unset https.proxy 1.2 带认证的代理 # 用户名密码认证 git config --global http.proxy http://user:pass@127.0.0.1:7890 # 特殊字符需要 URL 编码,@ → %40,: → %3A git config --global http.proxy http://user:p%40ss@127.0.0.1:7890 1.3 针对特定域名 只想给 GitHub 走代理,公司内网 GitLab 直连?按域名精准控制: ...

2026年8月11日 · 4 分钟 · 715 字

设置代理解决github被墙

虽然有梯子,可以正常访问github页面,但是在发现“git clone”命令速度特别慢,有时还经常卡掉。可以通过设置git 代理,解决被墙问题。 git 有几种传输协议,在Github上主要用到的是Https和SSH协议。所以我们要做的是对git 命令的https 以及ssh流量做代理。 设置https 代理 Git代理有两种设置方式,分别是全局代理和只对Github代理,建议只对github 代理。代理协议也有两种,分别是使用http代理和使用socks5代理,建议使用socks5代理。注意下面代码的端口号需要根据你自己的代理端口设定,比如我的代理socks端口是51837。 全局设置(不推荐) #使用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 只对Github代理(推荐) #使用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 取消代理 git config --global --unset http.proxy git config --global --unset https.proxy 设置ssh代理(终极解决方案) https代理存在一个局限,那就是没有办法做身份验证,每次拉取私库或者推送代码时,都需要输入github的账号和密码,非常痛苦。 设置ssh代理前,请确保你已经设置ssh key。可以参考在 github 上添加 SSH key 完成设置 更进一步是设置ssh代理。只需要配置一个config就可以了。 # 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 #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之类的就成功了。 ...

2025年7月22日 · 1 分钟 · 128 字