Skip to content

Instantly share code, notes, and snippets.

@mterrel
Created July 3, 2018 21:44
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mterrel/0effeb433213e5b014324c34d43a33bc to your computer and use it in GitHub Desktop.
Save mterrel/0effeb433213e5b014324c34d43a33bc to your computer and use it in GitHub Desktop.
Use WSL git with VS Code
# 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
{
"git.path": "C:\\Tools\\wslgit.cmd"
}
@echo off
:: Place in same directory as git.ps1
SET "psgit=%~dp0git.ps1"
PowerShell -NoProfile -ExecutionPolicy Bypass -File "%psgit%" %*
@mterrel
Copy link
Author

mterrel commented Jul 3, 2018

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:

  • Put wslgit.cmd and git.ps1 in the same directory on your local system
  • In your VS Code settings.json, set git.path to the absolute path of wslgit.cmd.
  • [Optional] If you add the directory with git.ps1 to your PATH, you should be able to use git from your Windows command line.

You are likely to have problems with this solution if:

  • Your paths contain spaces or other special characters.

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 in git.path, so that's why there's a seemingly pointless wingit.cmd file that you set your git.path to.

@mattoni
Copy link

mattoni commented Jul 10, 2018

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