Sub topic 12
Resolving with git mergetool
This chapter assumes you can start a merge, read conflict markers, and finish a merge by hand with git add and git commit. It also assumes you know what HEAD and a branch are. The earlier chapters on markers and manual resolution cover these. Here you will hand the editing step to a visual merge tool.
What git mergetool does
git mergetool does not resolve conflicts. It opens a merge tool of your choice for each unmerged file, one at a time. You edit the result in that tool. Git then checks the outcome and stages the file for you.
Run it while a merge is in progress and files are listed as unmerged:
git merge feature
# CONFLICT (content): Merge conflict in config.txt
git mergetoolThe command only works on paths that Git still lists as unmerged. If you have already staged a file, git mergetool skips it.
The four files a tool receives
For each conflicted file, Git extracts the three versions it keeps in the index. The stages are the ones you saw with git ls-files -u. It writes them to temporary files next to the original. Git then starts your tool with all four paths.
| Name | Source | Meaning |
|---|---|---|
| `$BASE` | stage 1 | The merge base, the common ancestor |
| `$LOCAL` | stage 2 | Your current branch ("ours") |
| `$REMOTE` | stage 3 | The branch being merged ("theirs") |
| `$MERGED` | working tree | The file with conflict markers. This is the file you save the result into |
The temporary files have names such as config_BASE_4821.txt and config_LOCAL_4821.txt. The $MERGED file is the real config.txt. Only changes saved there count.
Choosing and configuring a tool
List the tools Git recognizes and which of them it finds installed:
git mergetool --tool-helpNames such as vimdiff, meld, kdiff3, opendiff and bc appear in the list. Set one as the default:
git config --global merge.tool meldYou can pick a tool for a single run without changing your configuration:
git mergetool --tool=vimdiffIf merge.tool is not set, Git picks a tool it can find and prints a notice saying so. Git also has merge.guitool, which is used when you run git mergetool --gui. This lets you keep a terminal tool as the default and a graphical one for harder cases.
Defining a custom tool
Git has no built-in entry for every editor. For other tools, you define a command with mergetool.<name>.cmd. Git runs it through the shell, so $BASE, $LOCAL, $REMOTE and $MERGED are available. The following setup uses Visual Studio Code:
git config --global merge.tool vscode
git config --global mergetool.vscode.cmd 'code --wait --merge "$REMOTE" "$LOCAL" "$BASE" "$MERGED"'
git config --global mergetool.vscode.trustExitCode falseThe --wait flag matters. Without it, code returns at once and Git decides the merge is finished before you have edited anything. Quote the variables, because temporary paths can contain spaces.
The argument order depends on the tool. VS Code's --merge option takes the two sides, then the base, then the output file. Check your tool's documentation before copying a command written for another one.
Walking through a session
Recreate the conflict from the earlier chapters if you need to:
git init -b main demo && cd demo
printf 'timeout = 10\n' > config.txt
git add config.txt && git commit -m "Add config"
git switch -c feature
printf 'timeout = 60\n' > config.txt
git commit -am "Raise timeout to 60"
git switch main
printf 'timeout = 30\n' > config.txt
git commit -am "Raise timeout to 30"
git merge feature
git mergetoolGit prints a short summary, then asks you to continue:
Merging:
config.txt
Normal merge conflict for 'config.txt':
{local}: modified file
{remote}: modified file
Hit return to start merge resolution tool (meld):Press Enter. The tool opens with the $LOCAL and $REMOTE versions, usually the base as well, and the $MERGED result. Edit the result to the value you want, for example timeout = 45. Save the file and quit the tool.
Git then compares the file to what it wrote before launching the tool. If the file changed, Git treats the merge as successful. If your tool does not report an exit status reliably, Git asks "Was the merge successful?" and you answer y or n. When the answer is yes, Git runs git add on the file for you.
Then finish as in a manual merge:
git status
git commitThe merge tool never creates the merge commit. You still run git commit yourself.
Exit codes and trusting the tool
By default, Git decides success in one of two ways:
- If
mergetool.<tool>.trustExitCodeistrue, Git treats exit status 0 as success and any other status as failure. - Otherwise Git checks whether the
$MERGEDfile was modified. If it was, it asks you to confirm.
For well-behaved tools, set trustExitCode to true to skip the prompt. Some tools return 0 even when you close the window without saving, so test yours before trusting it.
Backup files and prompts
By default, Git saves the original conflicted file as config.txt.orig before the tool runs. These files are not tracked, but they clutter git status output. Two common settings:
git config --global mergetool.keepBackup false
git config --global mergetool.prompt falsekeepBackup false removes the .orig file when the merge succeeds. prompt false skips the "Hit return to start" question for each file. If you prefer to keep backups, add *.orig to a global ignore file instead.
Working on some files only
You can name paths, which is useful when a merge touches many conflicted files and you want to handle one tool-friendly file now:
git mergetool config.txtYou can also resolve some files by hand and others in the tool. Both paths end the same way: the file is staged with no markers left.
Undoing a bad tool session
If you saved a wrong result and have not committed, you can restore the original conflict markers for one file:
git checkout --merge -- config.txtThis recreates the conflicted state from the index. It only works while the file is still unmerged. After git add, Git has recorded the resolved version, so the command may no longer apply cleanly. To start the whole merge over, use git merge --abort as covered earlier.
Special conflict types
The tool does not always open an editor. For delete and rename conflicts, Git asks a question in the terminal:
Deleted merge conflict for 'old.txt':
{local}: deleted
{remote}: modified file
Use (m)odified or (d)eleted file, or (a)bort?Answer m to keep the file with the modifications, d to delete it, or a to stop. Binary files are passed to the tool like any other, but a text diff tool cannot merge them meaningfully. For binary conflicts, choose one side with git checkout --ours or --theirs.
During a rebase
$LOCAL and $REMOTE follow the same swap as ours and theirs. During a rebase, $LOCAL is the branch you are rebasing onto, and $REMOTE is the commit being replayed. If a tool's left and right panes seem backward, check whether you are in a rebase. After each file, run git rebase --continue instead of git commit.
Common errors
- The tool opens and closes at once. The command probably returns before the editor exits. Add the tool's wait flag, such as
--wait. - "The merge tool X is not available." The executable is not on your
PATH. Check withcommand -v meldor setmergetool.<tool>.path. - Markers still in the file after staging. You answered "yes" too early. Run
git diff --check, fix the file, and stage it again. - Panes look swapped. You are in a rebase, or your custom command lists the arguments in the wrong order.
- Stray
.origfiles. Setmergetool.keepBackup falseor ignore them.
Exercise
You run git mergetool and your editor opens and closes instantly. Git then asks "Was the merge successful?" even though you edited nothing. What is the likely cause, and what should you do before answering?
Show answer
The editor command returned before you could edit, usually because it needs a wait flag (for VS Code, code --wait --merge ...). Answer n, fix the mergetool.<name>.cmd setting, and run git mergetool again. The file stays unmerged until it is resolved and staged.
Exercise
After resolving in a merge tool and committing, how can you check that no conflict markers were committed, and which command checks for them before the commit?
Show answer
Before committing, run git diff --check, which reports leftover conflict markers and whitespace errors. After committing, you can search the files, for example with git grep -n '^<<<<<<<'.