全部学科
Python全栈
python
NodeJS全栈
nodejs
小程序首页
📅 2026-05-11 6 分钟 ✍️ juanwangdev

git mergetool 使用合并工具

git mergetool 启动可视化合并工具,提供三栏视图帮助解决冲突。

基本用法

Bash
# 启动合并工具
git mergetool

# 对特定文件启动
git mergetool <file>

# 使用指定工具
git mergetool --tool=<tool>

常用合并工具

工具平台说明
vimdiffLinux/MacVim 三栏对比
VSCode全平台Visual Studio Code
meldLinux图形化对比工具
KDiff3Windows/Linux三方合并工具
TortoiseMergeWindowsTortoiseGit 内置

配置合并工具

Bash
# 设置默认合并工具
git config --global merge.tool vscode

# 设置 VSCode
git config --global mergetool.vscode.cmd 'code --wait $MERGED'

# 设置 meld
git config --global merge.tool meld

# 设置 vimdiff
git config --global merge.tool vimdiff

vimdiff 使用

Bash
# 启动 vimdiff
git mergetool

# 四栏显示
# 左上:BASE(共同祖先)
# 右上:LOCAL(当前分支)
# 左下:REMOTE(合入分支)
# 右下:MERGED(合并结果)

# 操作命令
# :diffg LO   取用 LOCAL 版本
# :diffg BA   取用 BASE 版本
# :diffg RE   取用 REMOTE 版本
# :wqa        保存退出

VSCode 使用

Bash
# 配置 VSCode
git config --global merge.tool vscode
git config --global mergetool.vscode.cmd 'code --wait $MERGED'

# 启动
git mergetool

# VSCode 显示
# 左侧:当前分支
# 右侧:合入分支
# 中间:合并结果

# 点击按钮选择:
# Accept Current Change    接受当前
# Accept Incoming Change    接受合入
# Accept Both Changes       接受两者
# Compare Changes           详细对比

meld 使用

Bash
# 安装 meld
# Linux: sudo apt install meld
# Mac: brew install meld

# 启动
git mergetool --tool=meld

# 三栏显示
# 左:当前分支
# 中:合并结果(可编辑)
# 右:合入分支

# 操作:拖拽变更到中间栏

mergetool 常用选项

选项说明
--tool=使用指定工具
--no-prompt不提示确认
-y假设所有文件使用同一工具

工具对比

工具优点缺点
vimdiff内置、强大需学 Vim
VSCode现代化、易用需安装配置
meld直观图形化Linux 为主
KDiff3三栏合并强大配置复杂

查看可用工具

Bash
# 查看支持的合并工具
git mergetool --tool-help

# 输出
'git mergetool --tool=<tool>' may be set to one of the following:
        vimdiff
        vimdiff2
        vimdiff3
        emerge
        meld
        ...

图形化工具适合复杂冲突,可视化对比更直观。

要点总结

  1. git mergetool 启动图形化合并工具
  2. 常用工具:vimdiff、VSCode、meld、KDiff3
  3. git config merge.tool 配置默认工具
  4. 三栏视图显示:当前、合并、合入版本
  5. 选择适合自己习惯的工具

📝 发现内容有误?点击此处直接编辑

← 上一篇 git abort 中止合并
下一篇 → 冲突标记解析
想查看更多题目和详细解析?
小程序提供完整的题库、模拟考试和详细解析
马上就来

长按或扫描二维码,立即体验

扫码体验小程序
马上就来
使用微信扫描二维码
立即体验完整题库