在 macOS 上配置 Git 使用 VS Code 处理合并冲突,需先确保 code CLI 可用,再运行 git config --global merge.tool vscode 等三条命令启用 VS Code 为合并工具,并可选配置 diff.tool 实现差异对比。
在 macos 上让 git 在遇到合并冲突时自动调用 vs code 来处理,关键不是“默认编辑器”,而是明确配置 git 的 merge tool 和 diff tool。vs code 自带的 code --wait 命令已支持作为外部合并工具,无需额外插件。
确认 VS Code CLI 已正确安装
这是前提——Git 要能通过命令行启动 VS Code:
- 打开终端,执行:
code --version,有输出即说明 CLI 可用; - 若提示
command not found,请先在 VS Code 中按Cmd+Shift+P,输入Shell Command: Install 'code' command in PATH并回车执行; - 重启终端再验证。
配置 Git 使用 VS Code 为合并工具
运行以下三条命令一次性完成核心配置(适用于所有仓库):
git config --global merge.tool vscodegit config --global mergetool.vscode.cmd 'code --wait "$MERGED"'git config --global mergetool.vscode.trustExitCode true
其中 $MERGED 是 Git 内置变量,代表待解决冲突的文件路径;--wait 确保 Git 会等待你关闭 VS Code 编辑器后才继续流程。
可选:同时配置 diff 工具(查看差异时也用 VS Code)
方便对比修改内容,命令如下:
git config --global diff.tool vscodegit config --global difftool.vscode.cmd 'code --wait --diff "$LOCAL" "$REMOTE"'
之后执行 git difftool 就会用 VS Code 打开左右分屏对比视图。
实际使用时的操作流程
当执行 git merge 遇到冲突后:
- 终端会提示类似
Auto-merging xxx.js; CONFLICT (content): Merge conflict in xxx.js; - 直接运行:
git mergetool; - VS Code 会自动打开冲突文件,内置的合并编辑器会高亮冲突块(>>>>>>),你只需手动选择保留哪部分或编辑成最终版本;
- 保存文件并关闭 VS Code 窗口,Git 会自动标记该文件为已解决;
- 最后执行
git add . && git commit完成合并。


















