Created
July 3, 2018 21:44
-
-
Save mterrel/0effeb433213e5b014324c34d43a33bc to your computer and use it in GitHub Desktop.
Use WSL git with VS Code
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Run WSL git from Windows, mapping some paths between unix <-> Win | |
# To use with wslgit.cmd, place them in the same directory | |
Function ToUnix { | |
# If the arg has a \, then it's probably a path. Convert it. | |
if ($args[0] -match "\\") { | |
$mapped = wsl wslpath -u `'$($args[0])`' | |
} else { | |
$mapped = $args[0] | |
} | |
# Add single quotes around each arg for bash | |
"`'$mapped`'" | |
} | |
# Convert each arg with ToUnix | |
$mappedargs = $args | % { ToUnix $_ } | |
$out = wsl git $mappedargs | |
$gitExit = $LASTEXITCODE | |
# Mapping paths in the output is extremely difficult to get right | |
# in the general case. Luckily, VS Code seems happy with most of the | |
# output paths in unix format. However, it does depend on some | |
# responses mapping to Windows paths. Transforming single-word | |
# responses that look like paths seems to be sufficient for most | |
# common VS Code operations. | |
if ($out -is [string]) { | |
$words = -split $out | |
# Only map single words that have a /, but aren't a ref (which also has slashes) | |
if (($words.Length -eq 1) -and ($words[0] -match "/") -and !($words[0] -match "^refs/")) { | |
$out = wsl wslpath -w $words[0] | |
} | |
} | |
$out | |
exit $gitExit |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"git.path": "C:\\Tools\\wslgit.cmd" | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@echo off | |
:: Place in same directory as git.ps1 | |
SET "psgit=%~dp0git.ps1" | |
PowerShell -NoProfile -ExecutionPolicy Bypass -File "%psgit%" %* |
Getting the following error when vscode attempts a commit:
git commit --quiet --allow-empty-message --file - --all
C:\Users\matto\Documents\Development\vscode\git.ps1 : Cannot process argument because the value of argument "name" is
not valid. Change the value of the "name" argument and run the operation again.
+ CategoryInfo : InvalidArgument: (:) [git.ps1], PSArgumentException
+ FullyQualifiedErrorId : Argument,git.ps1
Any ideas? Other than that, it seems to be working.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This is a workaround to allow VS Code to use the version of git from your WSL installation (aka Bash on Windows). This is NOT a perfect solution, but seems to work well enough for the basic git operations I use most from VS Code. Depending on what you do in your workflow, you're somewhat likely to need to switch to a WSL bash shell to run some git commands.
Instructions:
wslgit.cmd
andgit.ps1
in the same directory on your local systemsettings.json
, setgit.path
to the absolute path ofwslgit.cmd
.git.ps1
to yourPATH
, you should be able to use git from your Windows command line.You are likely to have problems with this solution if:
Comments and suggestions welcome. I'm relatively new at PowerShell and batch files, so may have some rookie mistakes. I could not get VS Code to run
git.ps1
directly by setting it ingit.path
, so that's why there's a seemingly pointlesswingit.cmd
file that you set yourgit.path
to.