mac brew 一键更新国内源以及一键恢复国外源

更新国内源 新建brew_change.sh 后执行chmod 755 brew_change.sh 后执行文件

#!/bin/bash

echo "🚀 正在切换 Homebrew 国内镜像 (清华源)..."

# 替换 brew.git
cd "$(brew --repo)"
git remote set-url origin https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/brew.git
git fetch origin
git reset --hard origin/master

# 替换 homebrew-core.git
cd "$(brew --repo homebrew/core)"
git remote set-url origin https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/homebrew-core.git
git fetch origin
git reset --hard origin/master

# 替换 homebrew-cask.git(可选)
cd "$(brew --repo homebrew/cask)"
git remote set-url origin https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/homebrew-cask.git
git fetch origin
git reset --hard origin/master

# 设置 bottles 镜像
if [ -n "$ZSH_VERSION" ]; then
    SHELL_CONFIG=~/.zshrc
elif [ -n "$BASH_VERSION" ]; then
    SHELL_CONFIG=~/.bash_profile
else
    SHELL_CONFIG=~/.profile
fi

grep -q "HOMEBREW_BOTTLE_DOMAIN" $SHELL_CONFIG || echo 'export HOMEBREW_BOTTLE_DOMAIN=https://mirrors.tuna.tsinghua.edu.cn/homebrew-bottles' >> $SHELL_CONFIG
source $SHELL_CONFIG

# 强制 brew update
brew update --verbose

echo "🎉 Homebrew 镜像切换成功!"
brew config | grep HOMEBREW_BOTTLE_DOMAIN

 

Homebrew 一键恢复官方源脚本

#!/bin/bash

echo "🔄 正在恢复 Homebrew 官方源..."

# 恢复 brew.git 官方源
cd "$(brew --repo)"
git remote set-url origin https://github.com/Homebrew/brew.git
git fetch origin
git reset --hard origin/HEAD

# 恢复 homebrew-core 官方源
cd "$(brew --repo homebrew/core)"
git remote set-url origin https://github.com/Homebrew/homebrew-core.git
git fetch origin
git reset --hard origin/HEAD

# 恢复 homebrew-cask 官方源
cd "$(brew --repo homebrew/cask)"
git remote set-url origin https://github.com/Homebrew/homebrew-cask.git
git fetch origin
git reset --hard origin/HEAD

# 移除 bottles 镜像环境变量
if [ -n "$ZSH_VERSION" ]; then
    SHELL_CONFIG=~/.zshrc
elif [ -n "$BASH_VERSION" ]; then
    SHELL_CONFIG=~/.bash_profile
else
    SHELL_CONFIG=~/.profile
fi

# 删除 HOMEBREW_BOTTLE_DOMAIN 配置行
sed -i '' '/HOMEBREW_BOTTLE_DOMAIN/d' $SHELL_CONFIG
source $SHELL_CONFIG

# 强制更新 brew
brew update --verbose

echo "✅ Homebrew 已恢复官方源!"
brew config | grep HOMEBREW_BOTTLE_DOMAIN || echo "当前已无国内镜像配置。"

 

 

 

 

 

 

 

posted on 2025-07-03 10:58  星河赵  阅读(610)  评论(0)    收藏  举报

导航