Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

VSCode: use WSL Git instead of Git for Windows

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 ?

like image 304
Marc-Aurel Avatar asked Jun 08 '17 17:06

Marc-Aurel


People also ask

Does Git for Windows use WSL?

Git can be installed on Windows AND on WSL An important consideration: when you enable WSL and install a Linux distribution, you are installing a new file system, separated from the Windows NTFS C:\ drive on your machine.

How do I enable VS Code in WSL?

From VS Code Start VS Code. Press F1, select Remote-WSL: New Window for the default distro or Remote-WSL: New Window using Distro for a specific distro. Use the File menu to open your folder.

Is Git necessary for Visual Studio Code?

Visual Studio Code has git support built in. You will need to have git version 2.0. 0 (or newer) installed.


2 Answers

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!

like image 143
Marc-Aurel Avatar answered Oct 28 '22 02:10

Marc-Aurel


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.

like image 28
A.R.S. Avatar answered Oct 28 '22 03:10

A.R.S.