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,前面已经写了。
一句话总结
网络问题的本质是搞清流量走了哪条路。让它走该走的路,就快了。