VSCode: use WSL Git instead of Git for Windows

佐手、 提交于 2020-05-25 09:27:07

问题


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 ?


回答1:


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.




回答2:


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!




回答3:


Provide the full path for the bash exec :

git.bat :

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



回答4:


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




回答5:


I found a solution that works for me : see link

Download the file wslgit.exe and configure it in settings.json

{
  "terminal.integrated.shell.windows": "C:\\WINDOWS\\System32\\bash.exe",
  "git.path": "C:\\Users\\<username>\\wslgit.exe"
}


来源:https://stackoverflow.com/questions/44441830/vscode-use-wsl-git-instead-of-git-for-windows

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!