DEV Community

Matej Bunček 🤔
Matej Bunček 🤔

Posted on

How to make WSL2 even faster, with fast git

So Windows 2004 update is here, and I was really excited about this update. Because it brings WSL2 as a production-ready product to our daily drivers. With all the excitement I downloaded it, and when I did cd into my project I quickly realized that something is wrong. Git performance was really, really slow.

So I started searching around web and I discovered this issue and it seems like this issue in only appearing in the /mnt directory. Sadly the only workaround I found was to move your projects into the WSL filesystem. But I knew I want something more pleasing. So I created a Symlink for it. Now, this took me a great 6 hours, to figure out all the things you need to do, so if you're stuck on the same issue, I hope my guide will help.

1. Create a script 📖

You need to create a batch script that will take care of the symlink. Unfortunately, you can't do a symlink from WSL to windows, which is my plan. Create a symlink from $HOME/Code to your User Directory. So you can start up CMD/PowerShell/Bash/WSL and cd right into code directory.

Open up notepad and paste the following lines in here. Make sure you'll change you usernames

@echo off
START /B "" pushd \\wsl$\<Your WSL Version>\
rmdir C:\Users\<Your Windows Username>\Code /s /q
mklink /d C:\Users\<Your Windows Username>\Code Z:\home\<Your WSL Username>\Code
Enter fullscreen mode Exit fullscreen mode

But how should I get my WSL version?

Well, it's fairly easy. Just open up your WSL, type cd $HOME and then explorer.exe .. In the location bar type cmd like so
Cmd in adress bar
This will open WSL Filesystem in Windows explorer. Now click on the adress bar again and you should see something like this
Alt Text
The part that we need is that Ubuntu-20.04 in my case but that could be fedora or alpine or Ubuntu in your case.

What this script does?

Basically it will just mount your WSL as a network drive, and then it will create Windows Symlink, which will work in both Windows and WSL. Unlike the WSL symlinks which work only in WSL.

When we have our script done go to File > Save as, and then choose a location where you know that your script will be safe, so you don't accidentally delete it, and name it mount_wsl.bat.

2. Create a schedules task with highest privileges ⏱

Now when we have our script we need to run it every time we boot into our machine. We also need to do that with the highest privileges because you need to be an admin to make a symlink from network drive to your base drive.

So go ahead and search for Task Scheduler. On the left sidebar you will see Task Scheduler Library. Click on it and on the right sidebar click on Create Task....

Now make sure you'll give a name to your task, then write a little description and Check "Run with highest privileges". Also make sure you'll change Configure for: To Windows10

Win10 Scheduler

Next click on Triggers tab and add new trigger. This will look fairly simple, Just make sure it's like this, and click OK.

Triggers tab

Next click on Actions add New and Select the .bat script we created earlier.

3. Profit 🤑

Now when we restart our computer the network drive will be available in "This PC" and Code directory symlink to WSL will be in your User Directory. With significantly faster I/O Operations and git. Enjoy ✌

Top comments (15)

Collapse
 
edanb profile image
Edan Bainglass • Edited

I'm a bit confused about the script. My repository is located on /mnt/....

How would I create a link accessible from Windows to my repository? I'm confused about what "Code" refers to in the script. I attempted the following...

@echo off
START /B "" pushd \wsl$\Ubuntu\
rmdir link /s /q
mklink /d link target

where

link = the intended location of the link folder (in Windows)
target = Z:/mnt/... (location of my repository in wsl)

This creates the link, but the link produces an error (attached).

My general goal is to get access to my repository (Linux-based work), but have Git behave at a "normal" pace, as it does on Windows. If the above is aimed to achieve this, I would really appreciate an assist.

Thanks in advance...

Collapse
 
themartes_ profile image
Matej Bunček 🤔

Hi! I would really like to help you, but I'm not using windows anymore :(

Collapse
 
edanb profile image
Edan Bainglass

Does my "recipe" sound like it should achieve the goal though? I appear to be having the same issue as Nabeel below, where the symbolic link is not visible from the WSL side. Was that not your experience, or are we missing a step? If you don't remember, that's okay. I'll try to figure it out and post a solution.

Thread Thread
 
themartes_ profile image
Matej Bunček 🤔

Your solution looks good to me.

I asked my collegues if he was solving something like this, and he said he's using this script. It will run windows git under /mnt and linux git at other dirs. Meaning it will keep the speed of native git at all directories gist.github.com/jasonboukheir/3fda...

Thread Thread
 
edanb profile image
Edan Bainglass

Unfortunately, this does not seem to work either. Even after creating the script, typing git still defaults to linux git. It doesn't see my script. Can you please connect me with your colleague for further inquiry?

Thread Thread
 
edanb profile image
Edan Bainglass

Never mind... I just went with a simpler solution. Moved my project to the WSL filesystem (/home//project and lunched VS Code from there. I can now access it as if it was any other VS code workspace, but as it is now on the WSL filesystem, the default WSL git is at normal speed.

Thanks for everything :)

Collapse
 
obriat profile image
O'Briat • Edited

I try to link directly the \\wsl$ and it seems to work, disk performance are back to normal and I doesn't need a startup script.

C:\>mklink /d C:\FOO \\wsl$\debian\BAR
$ cd /BAR/
dd if=/dev/zero of=./testfile bs=1M count=1000 
# x4 to x6 faster than /c/ (or /mnt/c/ if you don't use "root = /" option in wsl.conf)

[Edit]: after navigating through these folders, Explorer switchs path from C:\... to \\wsl$\... and I cannot open the new path with my IDE (phpstorm)

Collapse
 
dulanjala007 profile image
Chirantha Dulanjala • Edited

this is not a true solution, say you delete your wsl2 distro's ext4.vhdx, all of your project files are gone... "rmdir C:\Users<Your Windows Username>\Code /s /q" --> delete the windows side folder.
all you're doing is just making a shortcut into the wsl distro, so you can see the project files from windows explorer...
My solution for accessing my harddirves is (wsl2 sshfs:/mnt/d/) -> (wsl1 ssh server)
explaination :- light wsl1 distro (which has faster drive access by design) with ssh server, connecting to it's /mnt/(d,f,g,..) via sshfs from wsl2 distro, much faster access than direct drive access from wsl2
also for project files i have separate ext4 vhdx mounted on wsl2 distro, top speed file access, and your data is separated from the system(OS) disk, 👌 say your distro corrupted, windows had to reinstall without any back up of C: drive, no worries project files are in separate disk in a separate partition on real disk

Collapse
 
nabeel profile image
Nabeel Molham

Hi Matej,

Thanks for the idea, the thing is I don't follow now do I access the windows files now from WSL? they doesn't show up under the /home/{username/ dir

Collapse
 
adsoncicilioti profile image
Adson Silva Cicilioti

"Z:\home<Your WSL Username>\Code" could it be "Z:\Code"?
Coincidentally I have a Z: disk and I keep my Projects in a directory at the root. I didn't quite understand why "home \ wsl_username".

Collapse
 
kmc79 profile image
kmc79

Why not run git from wsl?

Collapse
 
themartes_ profile image
Matej Bunček 🤔

You're using git in WSL, the problem is with mounted windows. When windows is under /mnt git is extremely slow, and for me personally and projects what I'm developing in work it's really a bummer. This repository is more than 10 years old, and simple git status is taking about 5 seconds. With this "hack" the git is running really smooth. Basically git something which is running on top of your filesystem, so it makes sense that it could be slower on mounted filesystems.

Collapse
 
ilanvivanco profile image
Ilán Vivanco

If I run this as soon as the session is logged on, it results in an error. So you can have it delay by 15 min at logon. That worked for me.

Collapse
 
ilanvivanco profile image
Ilán Vivanco

Extremely helpful. Just what I was looking for, thanks!

Collapse
 
nelsonym profile image
nelsonym

For simplicity here's my share github.com/microsoft/WSL/issues/44...