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

一句话总结

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

相关推荐
CoovallyAIHub5 小时前
15K Star中文首发!$5部署一个会自我进化的私人Agent——NousResearch开源Hermes Agent
git·架构·github
无限进步_6 小时前
【C++】巧用静态变量与构造函数:一种非常规的求和实现
开发语言·c++·git·算法·leetcode·github·visual studio
降临-max6 小时前
Git 协同开发与冲突解决
笔记·git
高志小鹏鹏8 小时前
告别“修复 bug”:让别人一眼看懂你的 Commit
git·github·代码规范
Rabbit_QL12 小时前
【Git基础】03——Git 撤销与回退:改错了怎么办
大数据·git·elasticsearch
无限进步_12 小时前
【C++&string】寻找字符串中第一个唯一字符:两种经典解法详解
开发语言·c++·git·算法·github·哈希算法·visual studio
木下~learning12 小时前
零基础Git入门:Linux+Gitee实战指南
linux·git·gitee·github·虚拟机·版本控制·ubunt
zh_xuan12 小时前
修改远程仓库名以及和本地工程同步
git
读书札记202213 小时前
Git 配置用户名和邮箱 解决 fatal: unable to auto-detect email address 问题
git