GitHub clone 不动、pip 卡住、brew 转圈?一份指南搞定

AI 工具兴起后,命令行正在回归。越来越多的工具有了 CLI 版本,像 Claude Code、Codex、OpenCode......等等等等。

终端用多了,你会发现一个尴尬的问题------即使使用了代理,命令行还是原生态,GitHub clone 不动、pip 卡住、brew 转圈,这些问题常常发生。

这篇就是来解决这个问题的。

方案一:用国内镜像源

不走海外,直连国内镜像,速度最快,但需要配置。

GitHub → Gitee

GitHub 在国内访问不稳定,最好的办法是先导入 Gitee,再从 Gitee clone。

去 gitee.com,点击「从 GitHub 导入仓库」,填入 GitHub 地址,几秒钟后就有了一个 Gitee 镜像。之后:

bash 复制代码
git clone https://gitee.com/你的用户名/repo.git

导入是自动同步的,不需要手动更新。

如果只是临时需要,不想导入,还有一个更简单的方法--------depth 1,只拉最新一个 commit,体积只有完整仓库的百分之一:

bash 复制代码
git clone --depth 1 https://github.com/xxx/repo.git

pip 用清华镜像

arduino 复制代码
pip config set global.index-url https://pypi.tuna.tsinghua.edu.cn/simple

国内常用的还有阿里云镜像:

arduino 复制代码
pip config set global.index-url https://mirrors.aliyun.com/pypi/simple/

如果某个包在镜像里找不到,加上 --no-cache-dir 再试,或暂时切回官方源:

css 复制代码
pip install --no-cache-dir xxx

Anaconda 或 Miniconda 也适用:

arduino 复制代码
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free/
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main/
conda config set show_channel_urls yes

npm 用淘宝镜像

arduino 复制代码
npm config set registry https://registry.npmmirror.com

如果遇到奇怪的包依赖问题,可以装 cnpm:

ini 复制代码
npm install -g cnpm --registry=https://registry.npmmirror.com

之后用 cnpm install 代替 npm install。

Homebrew 用清华镜像

bash 复制代码
# 替换 brew.git
git -C "$(brew --repo)" remote set-url origin https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/brew.git

# 替换 homebrew/core.git
git -C "$(brew --repo homebrew/core)" remote set-url origin https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/homebrew-core.git

# 替换 bottles
echo 'export HOMEBREW_BOTTLE_DOMAIN=https://mirrors.tuna.tsinghua.edu.cn/homebrew-bottles' >> ~/.bash_profile
source ~/.bash_profile

第一次 brew install 会下载 bottles,之后就快了。

方案二:让命令行走代理

代理有了,但命令行工具默认不走代理,所以 git clone、npm install 还是慢。需要手动指定。

先确认代理端口

每个工具的端口不同,常见的是:

  • Surge:1080
  • Clash:7890
  • ShadowsocksX:1086

端口号以你自己客户端里显示的为准。

Git 走代理

arduino 复制代码
git config --global http.proxy socks5://127.0.0.1:1080
git config --global https.proxy socks5://127.0.0.1:1080

如果代理只提供 HTTP 代理而不是 SOCKS5:

arduino 复制代码
git config --global http.proxy http://127.0.0.1:1087
git config --global https.proxy http://127.0.0.1:1087

如果是 SSH 方式 clone(git@github.com:xxx/repo.git),git config 不生效,需要在 ~/.ssh/config 里加:

perl 复制代码
Host github.com
    ProxyCommand nc -X socks5 -x 127.0.0.1:1080 %h %p

npm 走代理

arduino 复制代码
npm config set proxy http://127.0.0.1:1087
npm config set https-proxy http://127.0.0.1:1087

SOCKS5 代理:

arduino 复制代码
npm config set proxy socks5://127.0.0.1:1080
npm config set https-proxy socks5://127.0.0.1:1080

Homebrew 走代理

arduino 复制代码
export ALL_PROXY=socks5://127.0.0.1:1080
brew install xxx

嫌每次都输麻烦,可以写到 ~/.bash_profile 里:

ini 复制代码
alias brew="ALL_PROXY=socks5://127.0.0.1:1080 brew"

方案一和方案二怎么选

简单说:能上镜像就先上镜像,镜像没有的再走代理。

镜像的优势是不需要代理,配置一次一直用。代理的优势是所有流量都能加速,包括那些没有国内镜像的工具。

如果两个都用,那是最稳的组合。

几个常见的坑

端口号不固定。 有些代理客户端每次启动会换端口,最好在客户端里把 SOCKS 端口设成固定的。

TUN 模式和全局模式的区别。 开启 TUN 模式可以代理所有系统流量,包括命令行工具。但如果 TUN 有问题,全局模式下命令行才走得通,不过所有流量都走代理,访问国内服务反而更慢。可以用「绕过大陆」规则解决这个矛盾。

SSH 不走 HTTP 代理。 如果你用 SSH 方式 clone GitHub,需要单独配置 ~/.ssh/config,前面已经写了。

一句话总结

网络问题的本质是搞清流量走了哪条路。让它走该走的路,就快了。

相关推荐
不老刘14 小时前
Antigravity 的 New Worktree 模式:给 AI 代理一个安全的“平行工位”
人工智能·git·ai编程·antigravity
光谱SpectrAI14 小时前
Windows 也中招:Git Bash 和 Node 仍信任泄露的 eMudhra 证书
git
YIN_尹14 小时前
【git原理与使用】git初识与安装
大数据·git·elasticsearch
Leo_Mr19 小时前
Vue3 项目配置 Git 提交规范(企业通用方案:husky + commitlint + cz-git + lint-staged)
git
jyOverQ20 小时前
Git rebase 和 merge 有什么区别?什么时候该用哪个?
git
晗夜未央20 小时前
跨仓库 Skill 换台电脑就失效?用 Git remote 校验项目
人工智能·git·python
Lsetea20 小时前
Git访问HTTPS证书不受信任:用URL级sslCAInfo限定私有CA范围
git·https·开发工具·ssl证书·ca证书
এ慕ོ冬℘゜1 天前
Git 基础入门|和 SVN 对比 + 四大区域 + 常用命令详解
git·svn
DevLoom_2 天前
macOS 命令行 curl/git 报 SSL 证书错误?eMudhra 事件完整修复指南
git·macos·ssl
keyipatience2 天前
Git学习(4)Git分布式优势与实战配置全解析
git·学习