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 字

Git的奇技淫巧

Git 常用命令集合,Fork 自 tips 项目 Git 是一个分布式版本管理工具,版本管理工具就是大家在写东西的时候都用过 回撤这个功能,但是回撤只能回撤几步,假如想要找回我三天之前的修改,光用回撤是找不回来的。而版本管理工具能记录每次的修改,只要提交到版本仓库,就可以找到之前任何时刻的状态(文本状态)。 下面的内容就是列举了常用的 Git 命令和一些小技巧,可以通过页面内查找的方式 Ctrl/Command+f 进行快速查找。 开卷必读 如果之前未使用过 Git,可以学习 Git 小白教程入门 一定要先测试命令的效果后,再用于工作环境中,以防造成不能弥补的后果!到时候别拿着砍刀来找我 所有的命令都在 git version 2.7.4 (Apple Git-66) 下测试通过 统一概念: 工作区:改动(增删文件和内容) 暂存区:输入命令:git add 改动的文件名,此次改动就放到了 ‘暂存区’ 本地仓库(简称:本地):输入命令:git commit 此次修改的描述,此次改动就放到了本地仓库,每个 commit,我叫它为一个版本。 远程仓库(简称:远程):输入命令:git push 远程仓库,此次改动就放到了远程仓库(GitHub 等) commit-id:输出命令:git log,最上面那行 commit xxxxxx,后面的字符串就是 commit-id 如果喜欢这个项目,欢迎 Star、提交 Pr、反馈问题😊 展示帮助信息 git help -g The command output as below: The common Git guides are: attributes Defining attributes per path cli Git command-line interface and conventions core-tutorial A Git core tutorial for developers cvs-migration Git for CVS users diffcore Tweaking diff output everyday A useful minimum set of commands for Everyday Git glossary A Git Glossary hooks Hooks used by Git ignore Specifies intentionally untracked files to ignore modules Defining submodule properties namespaces Git namespaces repository-layout Git Repository Layout revisions Specifying revisions and ranges for Git tutorial A tutorial introduction to Git tutorial-2 A tutorial introduction to Git: part two workflows An overview of recommended workflows with Git 'git help -a' and 'git help -g' list available subcommands and some concept guides. See 'git help <command>' or 'git help <concept>' to read about a specific subcommand or concept. 回到远程仓库的状态 抛弃本地所有的修改,回到远程仓库的状态。 ...

2025年8月18日 · 5 分钟 · 990 字

git工作流

git clone // 到本地 git checkout -b xxx 切换至新分支xxx (相当于复制了remote的仓库到本地的xxx分支上 修改或者添加本地代码(部署在硬盘的源文件上) git diff 查看自己对代码做出的改变 git add 上传更新后的代码至暂存区 git commit 可以将暂存区里更新后的代码更新到本地git git push origin xxx 将本地的xxxgit分支上传至github上的git (如果在写自己的代码过程中发现远端GitHub上代码出现改变) git checkout main 切换回main分支 git pull origin master(main) 将远端修改过的代码再更新到本地 git checkout xxx 回到xxx分支 git rebase main 我在xxx分支上,先把main移过来,然后根据我的commit来修改成新的内容 (中途可能会出现,rebase conflict —–》手动选择保留哪段代码) git push -f origin xxx 把rebase后并且更新过的代码再push到远端github上 (-f —》强行) 原项目主人采用pull request 中的 squash and merge 合并所有不同的commit 远端完成更新后 git branch -d xxx 删除本地的git分支 git pull origin master 再把远端的最新代码拉至本地

2025年7月23日 · 1 分钟 · 72 字

设置代理解决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 字

git无法签出代码

Q: 在Windows10上使用git clone代码的时候抛出了异常,重试了几次都是相同的错误:下载的目录里除了.git没有任何东西。 A: 源代码是在linux上编写的,但是在Windows10上拉取代码却出现了问题。根据提示可以看出代码clone是成功的,但是checkout的时候出现了错误。那么问题很可能是不通系统下文件属性或策略导致的。 然后在Git文档上找到一个关于NTFS保护机制的配置,core.protectNTFS: If set to true, do not allow checkout of paths that would cause problems with the NTFS filesystem, e.g. conflict with 8.3 “short” names. Defaults to true on Windows, and false elsewhere. Windows系统下默认值是true,也就是说不符合NTFS策略的文件不会被签出,设置为false后可以关闭保护机制。 git config core.protectNTFS false

2025年7月21日 · 1 分钟 · 43 字