Diff and Merge Tools

Updated · 9 min read
mediumGit 2.53gitdifftoolmergetoolvscodeintellijinterview

git difftool and git mergetool are commands that open an external program to show a diff or to resolve a merge conflict, instead of printing text in the terminal. A diff tool shows two versions side by side, and a merge tool shows the base, both sides and the result, often with buttons to take one side or the other. Git doesn't include such a program itself. It knows how to start many popular ones, such as VS Code, IntelliJ IDEA, Meld, KDiff3 and Vimdiff, and lets you define any other.

Comparing two contracts line by line on paper is possible, but laying them side by side on a desk, with changes highlighted, is much faster. git diff is the paper. A diff tool is the desk.

Syntax

Terminal
git difftool [--tool=<tool>] [-y] [-d] [<diff options>]
git mergetool [--tool=<tool>] [-y] [<file>...]
Command or optionDescription
git difftoolIt is used to show each changed file in the diff tool. It accepts the same arguments as git diff.
git difftool --staged / main...featureIt is used to compare staged changes, branches, commits, as with git diff.
-d (--dir-diff)It is used to open all changed files at once, as two folders.
-y (--no-prompt)It is used to skip the "Launch ...?" question for each file.
git mergetoolIt is used to open each conflicted file in the merge tool.
--tool=<name>It is used to pick a tool for this run.
git difftool --tool-helpIt is used to list the tools Git knows, and which are installed.

Setting Up a Tool

Choose the tool once, with diff.tool and merge.tool. For tools Git knows, the name is enough. For others, give the command:

Shell
git config --global diff.tool vscode
git config --global difftool.vscode.cmd 'code --wait --diff "$LOCAL" "$REMOTE"'
git config --global merge.tool vscode
git config --global mergetool.vscode.cmd 'code --wait "$MERGED"'

For IntelliJ IDEA, the idea command must be on your PATH (Tools > Create Command-line Launcher, or the JetBrains Toolbox setting). Many people simply resolve conflicts inside the IDE instead, which does the same without git mergetool.

Add git config --global difftool.prompt false to stop Git asking before each file.

The Four File Variables

Git doesn't open your files directly. For each file, it writes temporary copies and passes their paths to the tool in variables:

VariableHoldsUsed by
$LOCALThe "before" version (for merges: your branch, ours)diff and merge
$REMOTEThe "after" version (for merges: the other branch, theirs)diff and merge
$BASEThe merge base, the common ancestormerge
$MERGEDThe file in your working tree, where the result must be savedmerge

A merge tool shows $LOCAL, $BASE and $REMOTE, and you save your decision into $MERGED.

The files a merge tool receives

Example 1: A Diff Tool of Your Own

To show how the variables work, without a window this page can't display, Navika defines a tiny command-line "tool" that counts the lines before and after:

Terminal
mkdir fee-tracker
cd fee-tracker
git init --quiet
printf 'Term 1 fee: $12,000\nLate fee: $250\n' > fees.txt
git add fees.txt
git commit --quiet -m "Add the fees"
echo 'Hostel fee: $9,000' >> fees.txt
git config difftool.count.cmd 'echo "$(grep -c "" "$LOCAL") lines before, $(grep -c "" "$REMOTE") lines after"'
git difftool --tool=count -y
Output
2 lines before, 3 lines after

Explanation: Git found one changed file, wrote the committed version to a temporary file ($LOCAL), passed the working-tree file as $REMOTE, and ran the command. A real tool does the same, but opens a window with the two files side by side. grep -c "" counts lines.

Example 2: A Merge Tool

Now a conflict. Navika's own "tool" resolves every conflict by taking the other branch's version, cp "$REMOTE" "$MERGED". trustExitCode tells Git that the tool's exit code says whether the resolution worked:

Terminal
git commit --quiet -am "Add the hostel fee"
git switch --quiet -c feature/late-fee HEAD~1
sed -i.bak 's/\$250/$300/' fees.txt
rm fees.txt.bak
git commit --quiet -am "Raise the late fee"
git switch --quiet main
sed -i.bak 's/\$250/$275/' fees.txt
rm fees.txt.bak
git commit --quiet -am "Raise the late fee differently"
git merge --quiet feature/late-fee
Output
Auto-merging fees.txt
CONFLICT (content): Merge conflict in fees.txt
Automatic merge failed; fix conflicts and then commit the result.
Terminal
git config mergetool.take-theirs.cmd 'cp "$REMOTE" "$MERGED"'
git config mergetool.take-theirs.trustExitCode true
git config mergetool.keepBackup false
git mergetool --tool=take-theirs -y
Output
Merging:
fees.txt
 
Normal merge conflict for 'fees.txt':
  {local}: modified file
  {remote}: modified file
Terminal
git status -s
cat fees.txt
Output
M  fees.txt
Term 1 fee: $12,000
Late fee: $300

Explanation: git mergetool listed the conflicted file, described the conflict (both sides modified it), and ran the tool. Because the tool succeeded, Git staged the result (M ), just as git add would after a manual resolution. Note that "take theirs" replaced the whole file with the other branch's version, so the hostel fee line from main is gone: exactly the danger described in "Resolving Merge Conflicts". A real merge tool lets you choose per change. Commit to finish the merge as usual.

mergetool.keepBackup false stops Git from leaving fees.txt.orig backup files after each resolution.

Comparing Whole Folders: -d

git difftool -d copies the two versions of all changed files into two temporary folders and opens the tool once on both. Tools with a folder view (Meld, Beyond Compare, VS Code with an extension, IntelliJ IDEA) then show a tree of every change, which is the most comfortable way to review a large branch: git difftool -d main...feature.

Common Mistakes

A tool that returns immediately. Editors started without "wait" (code without --wait) return at once, so Git thinks you are done. Use the waiting option.

Saving the result in the wrong panel. The merge result must be saved to $MERGED, the working-tree file. Saving $LOCAL or $REMOTE changes only a temporary copy.

Forgetting to commit after git mergetool. It stages the resolution, but the merge is finished only by git commit (or git merge --continue).

.orig files committed by accident. Set mergetool.keepBackup false, or add *.orig to .gitignore.

Quoting the variables wrongly. Paths can contain spaces. Write "$LOCAL" with double quotes inside single-quoted settings, as in the examples.

Under the Hood

git difftool is a wrapper around git diff: it runs the diff with an external helper (GIT_EXTERNAL_DIFF), which for each file receives the old and new versions and launches your tool. Versions that exist only in the repository (a commit or the index) are written to temporary files first. git mergetool looks for files with conflict stages (1, 2 and 3 in the index, see "The Index and Refs"), writes each stage to a temporary file ($BASE, $LOCAL, $REMOTE), runs the tool on them with $MERGED as the output, and runs git add on the file if the tool succeeded.

Interview Corner

What is the difference between git diff and git difftool?

Both compare the same versions with the same arguments. git diff prints the diff in the terminal. git difftool opens an external visual tool for each changed file (or all at once with -d).

What does git mergetool do?

It opens each conflicted file in a configured merge tool, passing the base, local and remote versions and the output file. If the tool reports success, Git stages the result. You then commit to finish the merge.

How do you configure VS Code as Git's diff and merge tool?

Set diff.tool and merge.tool to a name, and define difftool.<name>.cmd as code --wait --diff "$LOCAL" "$REMOTE" and mergetool.<name>.cmd as code --wait "$MERGED".

What are $LOCAL, $REMOTE, $BASE and $MERGED?

Paths Git passes to the tool: the "before" or ours version, the "after" or theirs version, the common ancestor (for merges), and the working-tree file where the merge result must be saved.

Practice

Challenge: compare staged changes. Commit the merge, then add Exam fee: $800 to fees.txt and stage it. Run the count diff tool on the staged changes only.

What does Git print? Before you open the solution, decide which option tells git difftool to compare the staging area with the last commit.

Show solution
Terminal
git commit --quiet --no-edit
echo 'Exam fee: $800' >> fees.txt
git add fees.txt
git difftool --tool=count -y --staged
Output
2 lines before, 3 lines after

--staged works exactly as with git diff: the last commit's version (two lines, after the "take theirs" resolution) against the staged version (three lines).

Checkpoint. Configure your favorite editor or IDE as diff and merge tool. Create a conflict in a practice repository, resolve it with git mergetool, and review a branch with git difftool -d main...<branch>.

Quick check: Diff and merge tools

4 questions
1Which setting chooses the program git difftool opens?
2In a merge tool, where must the result be saved?
3What does git mergetool do after the tool succeeds?
4Which option opens all changed files at once, as two folders?

Key Takeaways

Quick reference

CommandIt is used to
git config --global diff.tool <name>Choose the diff tool
git config --global merge.tool <name>Choose the merge tool
git difftool -yOpen changed files without asking
git difftool -d main...<branch>Review a branch as two folders
git mergetoolResolve conflicts in the merge tool
git difftool --tool-helpList known tools

Was this tutorial helpful?

Lesson 80 of 106