26
votes

I would like to use WSL (Bash on Windows) Git with VSCode instead of Git for Windows to avoid multiple Git installations.

I created a simple bat script to emulate git.exe comportment by redirecting git commands in WSL. It works nicely in CMD but not with VSCode. Also, WSL is my default terminal in VSCode.

VSCode settings.json:

{
    "git.path": "D:\\tools\\git.bat",
    "terminal.integrated.shell.windows": "C:\\Windows\\Sysnative\\bash.exe"
}

and git.bat:

@echo off
bash -c 'git %*'

Any idea to make VSCode working with WSL Git ?

4
The odd thing to me is that this approach works for ruby, rubocop, and every other executable I've tried. I don't understand why it does not work for git.Tejay Cardon
Here is the official solution: <code.visualstudio.com/docs/remote/wsl>. Essentially, you just need to install the Remote Development Extension Pack. The only quirk I have experienced is that it will start you off in bash even though you execute code . in zsh.noornashriq

4 Answers

19
votes

I created a small tool to solve this for myself, and hosted it on GitHub.

Basic git functionality seems to work, like viewing changes and committing.

A ready-to-use binary can be downloaded from the Releases page.

One of the problems is that the input paths need to be translated from the Windows representation (C:\Foo\Bar) to the Linux paths in WSL (/mnt/c/Foo/Bar), and back again for paths in the output of git.

For example, the Git plugin in VSCode uses the command

git rev-parse --show-toplevel

to find the root directory of the git repository, but with WSL git this of course returns a Linux path that needs to be translated for VSCode on Windows.

18
votes

Since VS Code 1.34 (April 2019) a remote extension has been introduced to develop into WSL: https://code.visualstudio.com/docs/remote/wsl.

Basically, a server instance of VS Code is started into WSL, allowing you to use all the WSL tools (e.g. git) from your client instance on Windows.

Thank you for pointing that out @Noornashriq Masnon!

2
votes

Provide the full path for the bash exec :

git.bat :

@echo off
c:\windows\sysnative\bash.exe -c "git %*"
2
votes

What you can do is to first try wslpath and if that fails you try a normal git command. It's not ideal but it works.

See: Use WSL git inside VS Code from Windows 10 17046