How to store HTTPS credentials for Git in WSL

If you are stuck on Windows for any reason but got most of your fun Linux tool with you, good chances are you are using Windows Subsystem for Linux (WSL). With it comes a full Linux distribution (I use Ubuntu, but there are plenty of options: Alpine, Debian, Fedore, Kali, openSUSE) backed by their whole package ecosystem behind them. Historically, you could only run command line tools, but even GUI support for running graphical applications is now (since April 2021) available as a preview feature.

Ok, back to this post’s topic: say you are using Git within WSL to work on some code project. But unfortunately, for some reason, that project’s main development server only supports HTTPS authentication. Typically, this would cause you to have to interactively authenticate by Username + Password on every git fetch or git push operation. Not so much fun.

If you are on Linux, you have some options available (with the good recommendation: if SSH is possible, use it!), but under WSL, you can use a simpler trick.

Requirements

  • Git installed within WSL
  • Git for Windows installed on host Windows

Yes, you have read correctly. If you have a native Git for Windows installation, it comes with a convenient git-credential-manager.exe that can handle HTTPS for you. All you have to do now is pointing your WSL Git to use that Windows tool:

Steps

  1. Create a file /usr/bin/git-credential-manager (e.g. by sudo vim /usr/bin/git-credential-manager) with the following contents:
#!/bin/sh
exec "/mnt/c/Program Files/Git/mingw64/libexec/git-core/git-credential-manager.exe" $@
  1. Make the file executable:
sudo chmod +x /usr/bin/git-credential-manager
  1. Open your WSL Git configuration file, e.g. by…
git config --global -e
  1. … and add the following options:
[credential]
    helper = manager

(Alternatively to steps 3 & 4, you may simply run the following command. But I prefer to see the config while modifying it.)

git config --global credential.helper manager

Test it

Within in any git repo with a remote configured to use HTTPS authentication, execute git fetch. It should launch a popup Window from the Git credential manager and ask once for your username and password.

Any subsequent calls to git fetch or git push should now succeed without the need for manual authentication.

Credits

This post is very much inspired by the very similar post Using GitHub HTTPS Credentials in WSL2 by Anaïs Betts.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.