Configure Visual Studio Code as a default git editor, diff tool, or merge tool

Masud Afsar
Geek Culture
Published in
3 min readApr 13, 2022

--

Configure visual studio code as a default git editor, diff tool, or merge tool
Photo by Pankaj Patel on Unsplash

I’m using Visual Studio Code as a fixed tool for a lot of opportunities like opening and editing files and sources quickly because it’s light and fast for me. After a while, I look at VS Code and think, about why I’m not using it as a git tool to write commit messages, get a diff between files, or resolve merge conflicts.

So I found VS Code can be configured as a really convenient tool inside the git.

Run VS Code from the command line

Before you can use VS Code as a git editor you must ensure you can access and run it from the command line. You can test the access VS Code by running the CLI command below.

$ code --version
1.66.0
e18005f0f1b33c29e81d732535d8c0e47cafb0b5
x64

If the command is not found you must introduce VS Code to your operating system to run it from CLI:

  • Windows: you must add the location of the VS Code installation to the PATH variable in the Environment Variables setting.
  • Mac OS: Press CMD+Shift+P keys and type Shell Command: Install 'Code' command in PATH in the command palette.
  • Linux: You must install VS Code from the .deb or .rpm packages file.

Use VS Code instead of nano as the default git editor

The default git editor is Nano, I used to use vim as a git editor until I found VS Code is convenient for that. you can configure VS Code as the default editor with the command below:

$ git config --global core.editor "code --wait --new-window"

This command opens VS Code in a new window and git waits until it saves contents and closes the window. You can remove VS Code by command below:

$ git config --global --unset core.editor

Use VS Code as git default diff tool and merge tool

The default git diff tool is vimdiff, and you can set it by commands below:

$ git config --global diff.tool vscode
$ git config --global difftool.vscode.cmd 'code --wait --diff $LOCAL $REMOTE'

You can unset these configs by commands below:

$ git config --global --unset diff.tool
$ git config --global --unset difftool.vscode.cmd

Git doesn’t have a default merge tool, you can use vimdiff for the merge tool too, or set VS Code as default by the command below:

$ git config --global merge.tool vscode
$ git config --global mergetool.vscode.cmd 'code --wait $MERGED'

You can unset these configs like diff tools by using the --unset flag after the config command.

My global git config file after these changes is like this:

$ cat ~/.gitconfig
...
[merge]
tool = vscode
[core]
editor = code --wait --new-window
[difftool "vscode"]
cmd = code --wait --diff $LOCAL $REMOTE
[diff]
tool = vscode
[mergetool "vscode"]
cmd = code --wait $MERGED
...

You can copy all these lines into your git config files in your home directory (~/.gitconfig) to set all commands above.

Conclusion

I always prefer to use the default CLI tools like vim to work with git and other command-line tools because sometimes you need to work with git and bash in a headless server with only default tools installed on it, so you need to know how to work with these tools at first. On the other hand, GUI tools help us to do things faster and not get confused in complex situations; VS Code is one of the best free tools you can use as an editor and comparing tools in your development environment.

Resources

--

--

Masud Afsar
Geek Culture

Software Engineer and Full-stack Web Developer - I'm eager to learn and talk about JavaScript, TypeScript, Software Architecture, and Clean code principles.