How do I use Git's difftool to merge conflicts?
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
git difftool is useful when you want to inspect conflicting changes visually, but it is not the tool that actually resolves the conflict state in Git. The practical workflow is usually: use difftool to understand the differences, then use mergetool or a normal editor to produce the final resolved file.
Configure a visual tool once
First, tell Git which external tool to use. meld, kdiff3, Beyond Compare, and vimdiff are common choices.
Once configured, the same UI can often be used both for inspecting differences and for resolving conflicts.
Use difftool to inspect the two sides
During an active merge conflict, Git records the current branch state in HEAD and the incoming branch state in MERGE_HEAD. You can compare them for a file like this:
That gives you a clean side-by-side comparison of the two versions. This is often easier to reason about than reading conflict markers directly in the file.
You can also list unresolved files first:
That helps you choose which file to inspect next.
Use mergetool or a normal editor to resolve
Understanding the difference is only half the job. To actually resolve the conflict, you still need to edit the file and stage it.
A merge-tool flow looks like this:
You can also edit the file yourself if you prefer. The key is that Git considers the conflict resolved only after the file is edited into a final form and staged.
A useful pre-merge workflow
git difftool is also valuable before you start the merge. If you compare the branches first, you can see where conflicts are likely to appear.
Directory diff mode is useful when many files changed and you want a broader visual overview before touching the merge itself.
That often makes the actual conflict resolution faster because you already understand the shape of the branch differences.
Know the limits of difftool
difftool is for comparison, not state transition. It does not by itself clear the merge conflict in Git. Even if the external tool lets you inspect both sides nicely, Git still needs the resolved file staged afterward.
That is why difftool and mergetool should be thought of as complementary:
- '
difftoolexplains the conflict' - '
mergetoolor manual editing resolves it'
Common Pitfalls
The biggest pitfall is expecting git difftool to mark a conflict resolved. It does not. You must still edit the file, git add it, and complete the merge.
Another issue is using MERGE_HEAD when there is no active merge in progress. That reference exists only during an actual merge conflict.
It is also easy to forget that your external tool must be installed and reachable on the system. Git configuration alone is not enough if the tool binary is missing.
Finally, do not rely entirely on a GUI if the file still contains semantic decisions you need to make manually. A merge tool helps, but it does not decide the final content for you.
Summary
- Use
git difftoolto inspect conflicting changes visually. - Compare
HEADandMERGE_HEADduring an active merge. - Use
git mergetoolor a normal editor to produce the final resolved file. - Always stage the resolved file with
git add. - Think of
difftoolas a comparison aid, not as the conflict-resolution step itself.
Related reading
- How do I use Notepad or other with msysgit?
- How do I view 'git diff' output with my preferred diff tool/ viewer?
- How do I view 'git diff' output with my preferred diff tool/ viewer?
- How do I view previous diff commits using Git?
- How do I work with a git repository within another repository?
- How do you attach a new pull request to an existing issue on github?
- How do you change the capitalization of filenames in Git?
- How do you copy and paste into Git Bash
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.