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,前面已经写了。

一句话总结

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

相关推荐
淘矿人6 小时前
从0到1:用Claude启动你的第一个项目
开发语言·人工智能·git·python·github·php·pygame
lpfasd1236 小时前
Git/Gitee/GitHub 3 个安全凭证详解
git·gitee·github
李日灐10 小时前
< 7 > Linux 开发工具:git 版本控制器 和 cgdb/gdb 调试器
linux·运维·服务器·开发语言·git·调试器·gdb/cgdb
网络点点滴10 小时前
NPM 和 package.json 文件简介
前端·npm·json
青木96010 小时前
前后端开发调试运行技巧
linux·服务器·前端·后端·npm·uv
Rabbit_QL10 小时前
npm 不是“前端的包管理器“—它是 Node.js 的
前端·npm·node.js
草履虫君10 小时前
原电脑只运行了:npm install -g openclaw 要把它迁移到一个新电脑,怎么操作,菜鸟教程
经验分享·ai·npm
Gust of wind11 小时前
idea结合git和Gitee的初步使用
git·gitee·intellij-idea
夜七少eleanor11 小时前
【Git】2026全图文详解安装教程
git
海边的Kurisu11 小时前
从零开始的Git生活 | 刚实习同学的噩梦 And 参与开源不可缺的一环
git·生活