update homebrew

Posted on By ᵇᵒ

前言

Homebrew 默认的更新源存放在 GitHub 上,导致墙内用户访问缓慢,一般来说我们会更倾向选择国内提供的更新源,推荐中科大以及清华大学提供的更新源。

替换更新源

# 替换 brew.git:
$ cd "$(brew --repo)"
# 中科大:
$ git remote set-url origin https://mirrors.ustc.edu.cn/brew.git
# 清华:
$ git remote set-url origin https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/brew.git

# 替换 homebrew-core.git:
$ cd "$(brew --repo)/Library/Taps/homebrew/homebrew-core"
# 中科大:
$ git remote set-url origin https://mirrors.ustc.edu.cn/homebrew-core.git
# 清华:
$ git remote set-url origin https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/homebrew-core.git

# 替换 homebrew-bottles(zsh用户使用 ~/.zshrc):
# 中科大:
$ echo 'export HOMEBREW_BOTTLE_DOMAIN=https://mirrors.ustc.edu.cn/homebrew-bottles' >> ~/.bash_profile
$ source ~/.bash_profile
# 清华:
$ echo 'export HOMEBREW_BOTTLE_DOMAIN=https://mirrors.tuna.tsinghua.edu.cn/homebrew-bottles' >> ~/.bash_profile
$ source ~/.bash_profile

# 替换 homebrew-cask:
$ cd "$(brew --repo)/Library/Taps/homebrew/homebrew-cask" 
# 中科大:
$ git remote set-url origin https://mirrors.ustc.edu.cn/homebrew-cask.git
# 清华:
$ git remote set-url origin https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/homebrew-cask.git

# 应用生效:
$ brew update

重置更新源

如果想换回官方源,只需要删除环境变量的 HOMEBREW_BOTTLE_DOMAIN 设置并重置 homebrew git remote url:

# 重置 brew.git:
$ cd "$(brew --repo)"
$ git remote set-url origin https://github.com/Homebrew/brew.git

# 重置 homebrew-core.git:
$ cd "$(brew --repo)/Library/Taps/homebrew/homebrew-core"
$ git remote set-url origin https://github.com/Homebrew/homebrew-core.git

# 应用生效:
$ brew update

诊断 homebrew

homebrew 安装目录是基于 git 版本管理,如果 homebrew 折腾出了问题,可以很方便的使用 git 命令重置 homebrew:

# 诊断Homebrew的问题:
$ brew doctor

# 重置brew.git设置:
$ cd "$(brew --repo)"
$ git fetch
$ git reset --hard origin/master

# homebrew-core.git同理:
$ cd "$(brew --repo)/Library/Taps/homebrew/homebrew-core"
$ git fetch
$ git reset --hard origin/master

# 应用生效:
$ brew update  

升级 homebrew

# 升级 homebrew
$ brew upgrade

# 清理旧有软件安装包
$ brew cleanup