/ powershell

Long Paths with Git on Windows 10

I had a problem recently with long paths while writing a script to clone some git repositories on Windows 10. Here's my solution.

Path limits

In Windows 10 you can remove the 260 character path limit with the following Group Policy: Computer Configuration > Administrative Templates > System > Filesystem > Enable NTFS long paths
But that only removes the limitation in Windows itself. Many programs like the Windows version of Git still enforce this limitation.

Bash on Windows (WSL)

Okay, so why not use Bash on Windows for everything? Well, I prefer to use PowerShell for complex scripting. I don't want to convert it all to bash because of Git operations!

Instead, I converted this part of the script:

Set-Location $dest
git pull --all
git submodule update

to this:

Set-Location $dest
bash -c 'git pull --all'
bash -c 'git submodule update'

When calling bash -c from PowerShell your current path automatically gets translated. If you're in C:\src\ and run bash -c 'pwd' it will output /mnt/c/src.

Manually translating paths

When cloning repositories in this script I needed to specify the full path to clone to. How do we translate those paths? Here's the way I came up with:

Function Convert-PathToWSL([string]$Path) {
    $wslPath = '/mnt/'
    $wslPath += $Path.Substring(0,1).ToLower() + '/'
    $wslPath += $Path.Substring(3).Replace('\','/')
    Return $wslPath

This converts C:\src\ to /mnt/c/src/.

Now I can run Git commands that use a path:

bash -c "git clone --recursive $($r.clone_url) $(Convert-PathToWSL $dest)"

That was easy (sort of)

My PowerShell script works with long paths by using the Linux version of Git via Bash on Windows / WSL. Pretty cool!

An easier way

After writing this post I was searching for my issue and came across this:
git config --system core.longpaths true which allows long paths on Windows.

It was still fun

How have you over-engineered something and found out there is a much simpler solution?

Long Paths with Git on Windows 10
Share this