Reputation: 11747
Today I was trying to use the git mergetool
on the Windows command prompt and realized that it was defaulting to use Vim, which is cool, but I'd prefer VS Code.
How can I have Visual Studio Code function as my GUI for handling merge conflicts (or even as a diffing tool) for Git?
Is it possible to setup VS Code to get visuals for a three-way merge?
Upvotes: 200
Views: 119330
Reputation: 11747
Update: As of Visual Studio Code 1.70 Three-way merge with improvements were added. Visuals and further explanations are available if that's of interest to you 😉.
As of Visual Studio Code 1.13 Better Merge was integrated into the core of Visual Studio Code.
The way to wire them together is to modify your .gitconfig
and you have two options.
To do this with command line entries, enter each of these: (Note: if on Windows Command Prompt replace '
with "
. Thanks to Iztok Delfin and e4rache for helping clarify this.)
git config --global merge.tool vscode
git config --global mergetool.vscode.cmd 'code --wait --merge $REMOTE $LOCAL $BASE $MERGED'
git config --global diff.tool vscode
git config --global difftool.vscode.cmd 'code --wait --diff $LOCAL $REMOTE'
To do this by pasting some line in the .gitconfig
with Visual Studio Code.
Run git config --global core.editor 'code --wait'
from the command line.
From here you can enter the command git config --global -e
. You will want to paste in the code in the "Extra Block" below.
[user]
name = EricDJohnson
email = [email protected]
[gui]
recentrepo = E:/src/gitlab/App-Custom/Some-App
# Comment: You just added this via 'git config --global core.editor "code --wait"'
[core]
editor = code --wait
# Comment: Start of "Extra Block"
# Comment: This is to unlock Visual Studio Code as your Git diff and Git merge tool
[merge]
tool = vscode
[mergetool "vscode"]
# Comment: Original way before three-way merge shown commented out
# cmd = code --wait $MERGED
# Comment: For "Three-way merge"
cmd = code --wait --merge $REMOTE $LOCAL $BASE $MERGED
[diff]
tool = vscode
[difftool "vscode"]
cmd = code --wait --diff $LOCAL $REMOTE
# Comment: End of "Extra Block"
Now from within your Git directory with a conflict run git mergetool
and, tada, you have Visual Studio Code helping you handle the merge conflict! (Just make sure to save your file before closing Visual Studio Code.)
For further reading on launching code
from the command line, look in this documentation.
For more information in git mergetool
check out this documentation.
Upvotes: 379
Reputation: 2720
On top of the excellent existing answer, you should open VS Code in a new window by adding -n
to the command line.
So your git config --global --edit
looks something like this.
[merge]
tool = vscode
[mergetool "vscode"]
cmd = code --new-window --wait $MERGED
[diff]
tool = vscode
[difftool "vscode"]
cmd = code --new-window --wait --diff $LOCAL $REMOTE
Upvotes: 31
Reputation: 351
I had to replace the double quotes with simple quotes:
git config --global difftool.vscode.cmd 'code --wait --diff $LOCAL $REMOTE'
for it to work properly (with double quotes, $LOCAL and $REMOTE are replaced by their values).
This is needed if you are using Git Bash for Windows instead of Windows Command Prompt.
Upvotes: 35
Reputation: 3181
In case if someone want to resolve it in Visual Studio, another option would be doing it through Visual Studio: Team Explorer -> click on Home icon => Settings button => expand Git section => click on Global Settings
Upvotes: 5
Reputation: 12347
Using the manual you can find an interesting argument:
git difftool --help
-x <command>, --extcmd=<command>
Specify a custom command for viewing diffs. git-difftool ignores the configured defaults and runs $command $LOCAL $REMOTE when this option is specified.
Additionally, $BASE is set in the environment.
With this information you can easily use the following command without touching the git configuration:
git difftool -x "code --wait --diff"
Similar question here
Upvotes: 11