Sub topic 12
Resolving with git mergetool
This chapter assumes you can read conflict markers, run git status during a merge, and finish a merge with git add and git commit. It covers git mergetool, which opens a visual tool for each conflicted file.
What git mergetool does
git mergetool runs after a merge has stopped with conflicts. For each unmerged path, it starts a configured merge program. The program shows the versions of the file side by side, and you build the final result in it.
Git gives the tool four files, taken from the index stages you saw with git ls-files -u:
| Name | Stage | Content |
|---|---|---|
| BASE | 1 | The common ancestor (the merge base) |
| LOCAL | 2 | The current branch's version ("ours") |
| REMOTE | 3 | The version from the branch being merged ("theirs") |
| MERGED | working tree | The file with conflict markers. This is where the result is saved. |
Choosing a tool
Run git mergetool --tool-help to list the tools Git supports and which ones it found on your machine. Then set one:
git config --global merge.tool vimdiff
git config --global mergetool.keepBackup falseCommon choices are vimdiff, meld, kdiff3, and vscode (Git 2.38 or later can use VS Code with a custom command). If merge.tool is not set, Git tries to pick one that is installed.
By default, Git leaves a backup such as config.txt.orig after each resolved file. Setting mergetool.keepBackup to false stops that. If you keep backups, add *.orig to .gitignore so they are not committed by accident.
Using it on a conflict
Reproduce the conflict from earlier in this tutorial, where both branches changed timeout in config.txt. Then run:
git mergetoolGit lists the files and asks before opening each one. In the tool:
- Compare LOCAL, REMOTE, and BASE to see what each side changed.
- Edit the MERGED pane to the content you want. Most tools have buttons or commands to take a hunk from either side.
- Save and quit.
If the tool exits successfully, Git runs git add on that file for you. This marks the conflict resolved. When every file is done, finish the merge:
git commitCommon problems
- You quit without saving. Git asks whether the merge was successful. Answer
nand the file stays unresolved. Rungit mergetoolagain. - The tool is not found. Git prints that the tool is not available. Install it, or choose another with
git config merge.tool. - Leftover
.origfiles. These are backups. Remove them withgit cleanafter checkinggit status, or disable them as shown above. - A bad resolution.
git mergetooldoes not check that the result is correct. Review it withgit diff --cachedand run your tests before committing.
Exercise
After running git mergetool and saving your edits in the tool, what do you still need to do to complete the merge, and why do you not need to run git add yourself?
Show answer
Run git commit to create the merge commit. You do not need git add because Git stages each file automatically when the tool exits successfully. If you quit the tool without success, the file stays unresolved.