macOS 上 Git 的几种常见来源在 macOS 中,Git 通常有 三种来源:
Xcode Command Line Tools 自带 Git(最常见)
Homebrew 安装 Git
官网 dmg 安装 Git(较少使用)
其中:
✅ Xcode 自带 Git:最省事、官方推荐
✅ Homebrew Git:版本新、可控性强
❌ 官网 dmg:不推荐,路径和环境变量较乱
方式一:通过 Xcode Command Line Tools 安装 Git(推荐)1️⃣ 直接触发安装(最快)
1git --version
如果你未安装 Command Line Tools,会弹出提示:
The xcode-select command requires the command line developer tools
点击 Install 即可
2️⃣ 手动安装 Command Line Tools
1xcode-select --install
安装完成后,Git 会自动可用
3️⃣ Git 实际安装位置(重点)
Xcode 自带 Git 不在 /usr/bin/git 本体中,而是在
1/usr/bin/git (这是一个 shim)
真实路径通常是
1/Library/Developer/CommandLineTools/usr/bin/git
你可以用下面命令确认 (请看上图)
123which git# orgit --exec-path
4️⃣ 为什么不用手动配环境变量?
原因是:
macOS 默认的 $PATH 中 已经包含 /usr/bin
/usr/bin/git 会自动指向 Xcode 的 Git
❌ 不需要手动 export PATH
✅ 结论:
使用 Xcode 自带 Git,不需要任何环境变量配置
方式二:通过 Homebrew 安装 Git(适合进阶用户)1️⃣ 安装 Homebrew(如果没有)
1/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
Apple Silicon(M1/M2/M3)安装完成后,brew 路径一般是
1/opt/homebrew/bin
2️⃣ 使用 Homebrew 安装 Git
1brew install git
3️⃣ Homebrew Git 的安装路径
Apple Silicon
1/opt/homebrew/bin/git
Intel Mac
1/usr/local/bin/git
4️⃣ 配置环境变量(⚠️ 很多人卡在这)
如果执行:
1git --version
发现还是 Xcode 的 Git,说明 PATH 优先级不对
✅ 正确做法(zsh)
编辑配置文件
1nano ~/.zshrc
添加(Apple Silicon)
1export PATH="/opt/homebrew/bin:$PATH"
或(Intel)
1export PATH="/usr/local/bin:$PATH"
生效
1source ~/.zshrc
5️⃣ 验证 Git 来源
1which git
期望输出
1/opt/homebrew/bin/git
Xcode Git vs Homebrew Git 的区别
对比项
Xcode 自带 Git
Homebrew Git
安装成本
⭐⭐⭐⭐⭐
⭐⭐⭐
稳定性
⭐⭐⭐⭐⭐
⭐⭐⭐⭐
版本更新
随系统
可随时更新
环境变量
不需要
需要
推荐人群
普通开发 / 前端
后端 / Git 高级用法
常见环境变量与坑点总结❌ 误区 1:手动改 /usr/bin/git
/usr/bin 是系统保护目录,不能也不该修改
❌ 误区 2:Xcode Git 需要 export PATH
完全不需要,macOS 已经帮你配好了
❌ 误区 3:brew 安装了 git,但用的还是旧版本
99% 是 PATH 顺序问题
✅ 推荐 PATH 顺序(Apple Silicon)
123/opt/homebrew/bin/usr/local/bin/usr/bin
推荐的最终组合方案(最稳)开发环境推荐:
✅ 安装 Xcode Command Line Tools
✅ 默认使用系统 Git
❌ 不重复安装 Git
❌ 不随意改 PATH
如果你确实需要新版本 Git:
再用 Homebrew
明确 PATH 优先级
快速自检清单1234git --versionwhich gitxcode-select -pecho $PATH
macOS 上最省心的 Git = Xcode Command Line Tools 自带 Git Homebrew Git = 更灵活,但一定要处理好 PATH