homebrew 镜像源是 git 仓库的 remote url,备份与恢复即记录和还原 brew、homebrew-core、homebrew-cask 的 origin 地址及 homebrew_bottle_domain 环境变量;可通过命令导出为文本文件,再用 git remote set-url 和 echo export 命令恢复。
homebrew 镜像源本身不是独立配置文件,而是 git 仓库的远程地址(remote url),分别存于 brew、homebrew-core、homebrew-cask 这几个本地 git 仓库中。因此,“备份”和“恢复”本质上是记录和还原这些仓库当前使用的 remote 地址。
查看当前镜像源设置
执行以下命令,可分别查出三处关键源的当前 remote 地址:
git -C "$(brew --repo)" remote get-url origin git -C "$(brew --repo homebrew/core)" remote get-url origin git -C "$(brew --repo homebrew/cask)" remote get-url origin
⚠️ 注意:
homebrew-cask需已安装过(如运行过brew install --cask xxx)才能查到;未安装时该目录不存在。
你还可以顺带检查二进制瓶(bottles)的域名设置:
echo $HOMEBREW_BOTTLE_DOMAIN
备份当前镜像源(推荐做法)
把上面四条信息保存为纯文本,例如存为 brew-sources-backup.txt:
brew repo: https://mirrors.ustc.edu.cn/brew.git core repo: https://mirrors.ustc.edu.cn/homebrew-core.git cask repo: https://mirrors.ustc.edu.cn/homebrew-cask.git bottle domain: https://mirrors.ustc.edu.cn/homebrew-bottles/bottles
也可以用一行命令快速导出(复制粘贴即可):
{
echo "# brew repo"; git -C "$(brew --repo)" remote get-url origin
echo "# core repo"; git -C "$(brew --repo homebrew/core)" remote get-url origin
echo "# cask repo"; git -C "$(brew --repo homebrew/cask)" remote get-url origin 2>/dev/null || echo "(not installed)"
echo "# bottle domain"; echo "${HOMEBREW_BOTTLE_DOMAIN:-[not set]}"
} > ~/Desktop/brew-sources-backup-$(date +%Y%m%d).txt
生成的文件可存桌面或 iCloud 同步目录,便于后续找回。
恢复镜像源(按备份内容重设)
假设你备份的是中科大源(USTC),恢复只需依次执行:
git -C "$(brew --repo)" remote set-url origin https://mirrors.ustc.edu.cn/brew.git git -C "$(brew --repo homebrew/core)" remote set-url origin https://mirrors.ustc.edu.cn/homebrew-core.git git -C "$(brew --repo homebrew/cask)" remote set-url origin https://mirrors.ustc.edu.cn/homebrew-cask.git echo 'export HOMEBREW_BOTTLE_DOMAIN=https://mirrors.ustc.edu.cn/homebrew-bottles/bottles' >> ~/.zprofile source ~/.zprofile
✅ 若用的是清华源,把 URL 中
mirrors.ustc.edu.cn替换为mirrors.tuna.tsinghua.edu.cn/git/homebrew/(注意路径差异)。
执行完后,运行 brew update 测试是否生效——成功则看到更新日志,无报错即表示恢复完成。
一键还原为官方源(应急用)
如果想彻底退回默认,运行以下三行:
git -C "$(brew --repo)" remote set-url origin https://github.com/Homebrew/brew git -C "$(brew --repo homebrew/core)" remote set-url origin https://github.com/Homebrew/homebrew-core git -C "$(brew --repo homebrew/cask)" remote set-url origin https://github.com/Homebrew/homebrew-cask unset HOMEBREW_BOTTLE_DOMAIN sed -i '' '/HOMEBREW_BOTTLE_DOMAIN/d' ~/.zprofile 2>/dev/null source ~/.zprofile
再执行 brew update 即可同步回 GitHub 官方源。
不复杂但容易忽略











