How do I view 'git diff' output with my preferred diff tool/ viewer?
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
If you do not enjoy reading unified diff output in the terminal, Git can launch an external diff tool instead. This is built into Git through git difftool, and it works with GUI tools such as Meld, Beyond Compare, KDiff3, or Visual Studio Code.
The usual setup is simple: tell Git which tool you want, optionally define the exact launch command, and then run git difftool whenever you want a visual comparison.
Use git difftool
git diff prints text directly in the terminal. git difftool asks Git to compare the same revisions, but open the comparison in an external program.
For example:
If a tool is configured, Git will launch it with temporary copies of the local and remote versions.
Configure a Preferred Tool
Some tools are known to Git by name. If yours is one of them, you can set it directly:
The second setting disables the "launch tool for each file" prompt, which is convenient for day-to-day use.
You can inspect which tools Git knows about:
Configure Visual Studio Code as a Diff Tool
If you prefer VS Code, define the exact launch command. Git exposes the two file paths as $LOCAL and $REMOTE.
Now run:
VS Code opens a side-by-side diff and waits until you close the window before returning control to Git.
Use a One-Off Tool Without Changing Global Config
You do not need to commit to one viewer forever. You can override the configured tool per command:
This is useful when you normally work in the terminal but want a graphical view for a large refactor.
Configure a Custom Tool
If Git does not recognize your preferred application by name, define a custom difftool command in your Git config.
You can place that in ~/.gitconfig or set it through git config --global.
Useful Variants
The same revision syntax from git diff works with git difftool:
These commands compare:
- working tree versus index
- index versus last commit
- one commit range for only one path
That makes git difftool practical both for quick checks and for deeper review work.
Common Pitfalls
- Configuring
diff.toolbut then runninggit diffand expecting a GUI. Usegit difftool. - Forgetting
--waitfor editors like VS Code can make Git think the tool finished immediately. - Omitting quotes around
$LOCALand $REMOTEcan break paths that contain spaces. - Setting
difftool.prompt falseglobally is convenient, but some developers prefer the prompt for large changesets. - Choosing a tool that is not installed on every machine can make shared setup docs confusing. Document platform differences clearly.
Summary
- Use
git difftoolwhen you want Git to open an external diff viewer. - Set
diff.toolto a supported tool name or define a custom command. - For VS Code,
code --wait --diff "$LOCAL" "$REMOTE"is a common setup. - Use
--tool=...for one-off comparisons without changing global config. - Keep quoting and
--waitcorrect so Git and the viewer stay in sync.
Related reading
- 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
- How do you fix a bad merge, and replay your good commits onto a fixed merge?
- How do you get git to always pull from a specific branch?
- How do you merge two Git repositories?
.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 courseNo course covers this one yet
Basis Lab writes one for you from a sentence about what you want to be able to do, then teaches it and checks you understood. Your first course is free.
Build my 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.