我想使用WSL(Windows上的Bash)Git和VSCode而不是Git for Windows来避免多个Git安装。
我创建了一个简单的bat脚本,通过重定向WSL中的git命令来模拟git.exe
comportment。它在CMD中运行良好,但在VSCode中运行不正常。此外,WSL是我在VSCode中的默认终端。
VSCode settings.json:
{
"git.path": "D:\\tools\\git.bat",
"terminal.integrated.shell.windows": "C:\\Windows\\Sysnative\\bash.exe"
}
和git.bat:
@echo off
bash -c 'git %*'
有什么想让VSCode与WSL Git一起工作吗?
我为自己创建了一个小工具来解决这个问题,并且hosted it on GitHub。
基本的git功能似乎很有用,比如查看更改和提交。
可以下载即用型二进制文件from the Releases page。
其中一个问题是输入路径需要从Windows表示(C:\Foo\Bar
)转换为WSL(/mnt/c/Foo/Bar
)中的Linux路径,然后再返回git输出中的路径。
例如,VSCode中的Git插件使用该命令
git rev-parse --show-toplevel
找到git存储库的根目录,但是使用WSL git,这当然会返回一个需要在Windows上为VSCode转换的Linux路径。
提供bash exec的完整路径:
git.bat:
@echo off
c:\windows\sysnative\bash.exe -c "git %*"
你可以做的是首先尝试wslpath
,如果失败,你尝试一个正常的git
命令。它并不理想但它有效。
自VS Code 1.34(2019年4月)以来,已经引入了远程扩展以开发成WSL:https://code.visualstudio.com/docs/remote/wsl。
基本上,VS Code的服务器实例启动到WSL中,允许您使用Windows上客户端实例的所有WSL工具(例如git)。
谢谢你指出@Noornashriq Masnon!
我发现了一个适合我的解决方案:see link
下载文件wslgit.exe并在settings.json中配置它
{
"terminal.integrated.shell.windows": "C:\\WINDOWS\\System32\\bash.exe",
"git.path": "C:\\Users\\<username>\\wslgit.exe"
}