Using VS Code as default Git editor, difftool and mergetool

avatar
Borislav Hadzhiev

Last updated: Apr 6, 2024
5 min

banner

# Table of Contents

  1. Using VS Code as your default Git editor
  2. Hint: Waiting for your editor to close the file... message in VS Code
  3. Setting VS Code as your Git difftool and mergetool

# Using VS Code as your default Git editor

To use VS Code as your default Git editor:

  1. Open your terminal (e.g. cmd on Windows or bash or zsh on macOS and Linux).
  2. Run the following command.
shell
git config --global core.editor "code --wait"

set vscode as default git editor

The command sets VS Code as your default Git editor.

If you get an error that the command git or code is not recognized or not found, you have to install git and vscode and add them to your PATH environment variable.

If you aren't able to run the git or code commands (e.g. git --version and code --version):

  • on Windows: make sure git and VS Code are added to your PATH environment variable or rerun the installer and select Add to PATH during the installation.
  • on macOS: select Shell Command: Install 'Code' command in path from the Command Palette.
  • on Linux: Install Code using the .deb or .rpm packages.

We specified the --wait option when running the command.

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

set vscode as default git editor

If you want to open a new session of VS Code when you commit or issue another git command, set the --new-window option.

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

setting new window option

When the --new-window option is set, a new instance of VS Code opens when you issue a git command, e.g. git commit.

The default behavior is to open an "edit commit message" tab in your current VS Code instance.

The --wait option is used to wait for the window to close before returning to the terminal.

In other words:

  1. Once you start a commit, it opens in VS Code, so you can set the commit message.
  2. Once you save the file and close it, you return to your terminal.

You can run the following command to verify VS Code has been set as your default Git editor.

shell
git config --global -e

verify vs code is set as default git editor

The command opens your global .gitconfig file in your default Git editor (VS Code).

If the file opens in VS Code, then you have configured VS Code as your Git editor successfully.

# Hint: Waiting for your editor to close the file... message in VS Code

Notice that the terminal shows a message: that it waits for your editor to close the file before returning.

hint: Waiting for your editor to close the file...

You have to close the file that you are currently editing for the terminal to return and complete the command.

If you don't close the file, your terminal will be stuck in the "Waiting for your editor to close the file..." state.

If your terminal hangs, try to close the entire VS Code window (not just the tab) and reopen it.

Using the --new-window option is convenient because it always opens a new VS Code instance that you can completely close once you're done.

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

setting new window option

If I stage the changes in a git repository with git add . and then run git commit, I can edit my commit message in VS Code.

shell
git add . git commit

git commit opens vs code

This is what the edit commit message screen looks like in VS Code.

edit commit message in vs code

Once you enter your commit message, save the file and close it to return to the terminal.

You can also use the integrated VS Code terminal to issue git commands.

Git can be configured to use terminal-based editors like VIM or graphical IDEs like VS Code or Sublime text.

If you configure Git with a graphical IDE, then the "Waiting for your editor to close the file..." message will be shown until you complete editing the corresponding file and close it.

If your terminal hangs with the message, try to issue the code --version command to make sure VS Code is installed and set up correctly.

shell
code --version

If you get a version number back, then VS Code is set up correctly.

If your terminal still hangs with the message even after closing the spawned VS Code instance, try to wrap the command in single quotes instead of double quotes.

shell
git config --global core.editor 'code --wait'

And here is the equivalent command for opening a new VS Code instance each time.

shell
git config --global core.editor 'code --new-window --wait'

# Setting VS Code as your Git difftool and mergetool

To set VS Code as your Git difftool and mergetool:

  1. Issue the following command to edit your global git config.
shell
git config --global -e

verify vs code is set as default git editor

  1. Add the following lines of code to set VS Code as your difftool and mergetool.
.gitconfig
[diff] tool = default-difftool [difftool "default-difftool"] cmd = code --wait --diff $LOCAL $REMOTE [merge] tool = code [mergetool "code"] cmd = code --wait --merge $REMOTE $LOCAL $BASE $MERGED

set vscode as default git difftool and mergetool

When the --diff option is passed to VS Code, it compares two files side by side.

The next time you use the git difftool command, VS Code will open as your default difftool editor.

The syntax for the command is:

shell
git difftool <commit>^ <commit>

git difftool command

I passed 2 commit hashes to the git difftool command and VS Code opened the diff side by side.

using vscode as default git difftool

We also set VS Code as the default mergetool.

If you run into a merge conflict outside of VS Code, Git will automatically open VS Code where you can resolve the conflict.

Here are examples of commands where you can use Git with VS Code:

  • git rebase -i HEAD~4 - using interactive rebases in VS Code.
  • git commit - specify your commit message in VS Code.
  • git add -p and press e - interactively marking files for staging in VS Code.
  • git difftool <commitA> <commitB> - opens a git diff directly in VS Code.

I've also written an article on how to change the Git user or the GitHub account in VS Code.

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.