# 如何配置 git merge tool？

当文件进入冲突状态时，可以配置一个好用的 git merge tool 来解决冲突。

当前版本的 vscode 虽然能标注 git conflict 状态，但还未能实现 three-way merge 功能（#[5770](https://github.com/Microsoft/vscode/issues/5770)），文本以老牌对比工具 [Beyond Compare](http://www.scootersoftware.com/support.php?zz=kb_vcs_osx#git) 为例，介绍如何配置 git merge tool。

## 1、命令行中的配置

首先从 Beyond Compare 菜单中选择 **Install Command Line Tools**，然后运行以下配置命令：

```
git config --global merge.tool bc3
git config --global mergetool.bc3 trustExitCode true
git config --global mergetool.keepBackup false # 关闭生成 .orig 备份文件
```

设置完成后，即可使用 Beyond Compare Text Merge 功能打开待解决冲突的文件：

```
git mergetool <文件名>
```

## 2、Three-way Merge 界面

在 Beyond Compare 的 merge 界面中，「上左」与「上右」面板为合并发生冲突的两次提交版本，「上中」为前次历史版本，「下方」为合并后的代码。

「下方」面板中，「红色！」表示当前行代码发生了冲突，点击「蓝色⇨」或「紫色⇨」，可以分别选择上方需要的代码。合并完成后，保存并关闭窗口，文件会被自动标记为已解决状态，添加至暂存区。

![Beyond Compare Merge](https://3165488068-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LXckDMRu1yEn1owbGIc%2F-LXiOauRjy1ek570sGSz%2F-LXiOcFI_vLOYU39arz2%2Fbc-three-way.png?generation=1549114184218844\&alt=media)

## 3、Source Tree 中的配置

打开 Source Tree，配置外部对比、合并工具为 Beyond Compare。

![](https://3165488068-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LXckDMRu1yEn1owbGIc%2F-LXiOauRjy1ek570sGSz%2F-LXiOcFKtiYTVQRzGnZn%2Fsourcetree-config.png?generation=1549114184430905\&alt=media)

在 SourceTree 中右键选中冲突的文件，使用指定工具解决冲突

![](https://3165488068-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LXckDMRu1yEn1owbGIc%2F-LXiOauRjy1ek570sGSz%2F-LXiOcFMYkmG0DpUS6-h%2Fsourcetree-context.png?generation=1549114183775478\&alt=media)


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://gb.yekai.net/questions/mergetool.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
