Use Visual Studio Code as git editor

Most of the time the Git integration of Visual Studio Code will do fine. But for other functions of Git the command-line seems more efficient. Once you use git over the command-line you’ll find yourself sometimes using a terminal text editor. Which might be fine for you. But if you’re the integrated terminal in Visual Studio Code it might feel a bit awkward. In this case you can set Visual Studio Code as the default editor for git.

During this example we will change the global git configuration. You can change the configuration on a repository level too. To do this; make sure you’re in the git repository and lose the --global option.

Set Visual Studio code as git editor

To set Visual Studio Code as default editor you have to run the following command.

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

This adds the following configuration to your global git config.

[core]
    editor = code --wait

By passing the --wait to the code command you tell Visual Studio Code to wait for you to close the file before returning to the terminal.

Set Visual Studio code as git mergetool

Besides an editor you can set a merge tool. Ofcourse you can use Visual Studio Code for this too. This is a two step process. First you have to define a new merge tool and secondly you need to set it as the default merge tool.

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

This adds the following configuration to your global git config.

[merge]
    tool = vscode
[mergetool "vscode"]
    cmd = code --wait $MERGED

Set Visual Studio code as git difftool

The same goes for setting the git difftool. You will have to run the following two commands.

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

This adds the following configuration to your global git config.

[diff]
    tool = vscode
[difftool "vscode"]
    cmd = code --wait --diff $LOCAL $REMOTE

Summary

You can change the git config to use Visual Studio Code instead of a terminal text editor. To do this in one step you can paste the following snippet in your git config file.

[core]
    editor = code --wait
[merge]
    tool = vscode
[mergetool "vscode"]
    cmd = code --wait $MERGED
[diff]
    tool = vscode
[difftool "vscode"]
    cmd = code --wait --diff $LOCAL $REMOTE

For more information read the Visual Studio Code docs.

See Also

6 Non-Programming Books for Programmers