Using VSCode as git mergetool and difftool

Kenichi Shibata
Jun 22 · 3 min read

If you are still using WinMerge in 2019 to eyeball the difference between two similar text files. VSCode can now help you do the same and more.

WinMerge copyright sourceforge

But since you are using VSCode as your editor.

You might as well VSCode as your new tool for viewing diffs using the code -d <file1> <file2>.

vscode opens using the cli

Chances are you are using git as your source control. If you are you can also use vscode as your difftool and your mergetool. Add the below configuration to your gitconfig to start

# Add this to you gitconfig
# Comment: Start of "Extra Block"
# Comment: This is to unlock VSCode as your git diff and git merge tool
[merge]
tool = vscode
[mergetool "vscode"]
cmd = code --wait $MERGED
[diff]
tool = vscode
[difftool "vscode"]
cmd = code --wait --diff $LOCAL $REMOTE
# VSCode Difftool
## End of extra block

Now you may use vscode as your git difftool

$ vi main.go
$ git difftool
diff --cc README.md
index 54f96a6,b197bb6..0000000
--- a/README.md
+++ b/README.md
@@@ -21,10 -21,9 +21,16 @@@ go get -u github.com/kenichi-shibata/ku
Usage
-------
```
++<<<<<<< HEAD
+# downloads v1.14.3 or -k <version> or from config file ~/.kube/kubectl/config
+kubectl-switch download
+kubectl-switch -k v.1.11.9
+# list available versions
++=======
+ +# downloads v1.14.3 or -k <version> or from config file ~/.kube/kubectl/config
+ +kubectl-switch downloadzs
+ +kubectl-switch -k v.1.11.9
++>>>>>>> downloadz
ls ~/.kube/kubectl/
```
Config
Viewing (1/1): 'main.go'
Launch 'vscode' [Y/n]? y

This will try to open all the diffs one at a time. Close one diff and keep on saying y to continue for multiple files.

One step further you can also use the same feature as your git mergetool

$ vi README.md
$ git add .
$ git commit[list-setup bec971e] downloadz
1 file changed, 3 insertions(+), 3 deletions(-)
$ git pullr origin master
From github.com:kenichi-shibata/kubectl-switch
* branch master -> FETCH_HEAD
First, rewinding head to replay your work on top of it...
Applying: downloadz
Using index info to reconstruct a base tree...
M README.md
.git/rebase-apply/patch:13: trailing whitespace.
+kubectl-switch downloadzs
warning: 1 line adds whitespace errors.
Falling back to patching base and 3-way merge...
Auto-merging README.md
CONFLICT (content): Merge conflict in README.md
...

$ git mergetool
Merging:
README.md
Normal merge conflict for 'README.md':
{local}: modified file
{remote}: modified file
Now merging files are now one click away

Same as the difftool this will try to open all the merge one at a time. Close one merge and keep on saying y to continue for multiple files.

You can now use vscode as your mergetool and difftool

If you enjoyed this story, please click the 👏 button to support its author and join the community:

Faun

The Must-Read Publication for Aspiring Developers & DevOps Enthusiasts

Kenichi Shibata

Written by

Git Ninja! Linux Nerd. Javascript evangelist. AWS Architect. Software engineer. Kubernetes Mentor and Architect

Faun

Faun

The Must-Read Publication for Aspiring Developers & DevOps Enthusiasts