How to use Git credential store on WSL (Ubuntu on Windows)?

GitUbuntuWindows Subsystem-for-Linux

Git Problem Overview


I've tried following these instructions: https://stackoverflow.com/a/40312117/21728 which basically do this:

sudo apt-get install libsecret-1-0 libsecret-1-dev
cd /usr/share/doc/git/contrib/credential/libsecret
sudo make
git config --global credential.helper /usr/share/doc/git/contrib/credential/libsecret/git-credential-libsecret

But when I do any network operation, I get this error:

** (process:7902): CRITICAL **: could not connect to Secret Service: Cannot autolaunch D-Bus without X11 $DISPLAY

That's logical I guess as there is indeed no X11 display.

How to make Git credentials caching work on Ubuntu on Windows (WSL)?

Git Solutions


Solution 1 - Git

If you installed Git for Windows there is a windows integrated credential manager installed on your system.

You can run windows executables from WSL as found here.

To use it you can run the following command (assuming that your git for windows is installed on C:\Program Files\Git)

git config --global credential.helper "/mnt/c/Program\ Files/Git/mingw64/bin/git-credential-manager-core.exe"

Solution 2 - Git

TL;DR

I've created a script that does this for you. I use it with my Chef orchestration.

Locate or install git-credential-manager.exe

  1. Open cmd.exe and call where git-credential-manager.exe
  • If it returns a path, GREAT. Move on to converting the path.
  • If not...
  1. In cmd.exe call where git.exe
  • If it does not return a path, the next step is to install the Credential Manager alone
  • If it does return a path, it will be something like:
  • C:\Program Files\Git\cmd\git.exe
  • Let's drop the everything after the next to last slash and change it like so:
  • C:\Program Files\Git\mingw64\libexec\git-core\git-credential-manager.exe
  • If that exists, GREAT. Move on to converting the path.
  • Otherwise...
  1. Install Credential Manager from Microsoft's git repo, and then use where again to get the path.

Convert the path from DOS to Linux

We need to:

  1. Replace the C:\ with /mnt/c/
  2. Flip the slashes from \ to /
  3. Escape spaces (and parenthesis if there are any) with double backslashes \\

So...

  • "C:\Program Files\Git\mingw64\libexec\git-core\git-credential-manager.exe" becomes...
  • "/mnt/c/Program\\ Files/Git/mingw64/libexec/git-core/git-credential-manager.exe"

My script above has a function for doing just that

dos_path_to_linux(){
    sed -e 's?\\?/?g' -e' s?[cC]:?/mnt/c?' <<<"$1"
}

But, as @12345ieee has since commented, a wslpath utility has been added to WSL build 17046. It's worth checking out, but I don't have access to Windows at this time to verify. (Note that even though a usage statement is given in the release notes in my link, it seems that the command doesn't currently include a usage statement, -h, etc.)

Configure git

  1. In bash call git config --global credential.helper "<converted/path>"

Solution 3 - Git

Using Windows 10 and "WSL", I created a ~/.gitconfig file, but had mistyped the [credential] section label as [credentials]. I tried running git credential fill and then feeding its output to git credential approve, which might have worked, but I suspect not since it said "usage: git credential [fill|approve|reject]". Finally, I simply ran:

$ git config --global credential.helper cache

and then did a git pull; when prompted for user and password I typed them as usual. After that, it remembered it. I found it had added the (correctly named) section to my ~/.gitconfig:

[credential]
        helper = cache

I edited that to provide a much longer timeout:

[credential]
        helper = cache --timeout=144000

And it all seems to be working nicely now.

Solution 4 - Git

alias git=git.exe

Will simple use the git.exe from windows and its configurations

Solution 5 - Git

All the answers are overly complicated to this point. And the git documentation does not really help, they like to reference material a lot so you need to follow 2-3 links to get the info you need!

  1. You do not need to use Windows git.exe with WSL! Use linux git sudo apt install git-all (I think it comes pre-installed with Ubuntu).
  2. Then you can simply follow the instructions at https://git-scm.com/book/en/v2/Git-Tools-Credential-Storage.

Summary

git supports 2 methods by default. To see what methods you have installed, execute this command:

git help -a | grep credential

my result was:

   credential           Retrieve and store user credentials
   credential-cache     Helper to temporarily store passwords in memory
   credential-store     Helper to store credentials on disk

How to for cache & store:

cache

@selkieTG covers this in their answer, including for completeness...

git config --global credential.helper "cache --timeout 30000"

will cache your password/token for 30,000 seconds (8 hrs 20min)

store

git config --global credential.helper "store"

will store plain text password/token in ~/.git-credentials.

Plain Text?!! For WSL, I am absolutely OK with plain text here. I enter credentials to run my Windows machine and I enter credentials to sign into WSL2. Do I need to hide these? Not really, it is more of a convenience on my dev box.

manager-core

If you really want to use manager-core you can install it in your Ubuntu version. And then use it.

Solution 6 - Git

I have just recently updated to WSL2 and in my case the following wasn't working:

"/mnt/c/Program\\ Files/Git/mingw64/libexec/git-core/git-credential-manager.exe"

What worked was the following: git config --global credential.helper "/c/Program\\ Files/Git/mingw64/libexec/git-core/git-credential-manager.exe"

Until I've removed /mnt/ from the path I was getting a "not found" error.

From what I've investigated there's an issue with mounting windows drives in WSL2 after a clean Windows startup, more details here: https://github.com/microsoft/WSL/issues/4122 And that was the most probable cause in my case.

Another reason for this can be a misconfiguration of root directory in /etc/wsl.conf

Solution 7 - Git

Download: (gcmcore-linux_amd64.2.0.567.18224.deb) https://github.com/GitCredentialManager/git-credential-manager/releases/tag/v2.0.567

Install:

sudo apt install gcmcore -y or 

sudo dpkg -i <path-to-package.deb>   (gcmcore-linux_amd64.2.0.567.18224.deb)     

Configure:

export DOTNET_SYSTEM_GLOBALIZATION_INVARIANT=1   

git-credential-manager-core configure

Solution 8 - Git

Couldn't get this working with git-credential-manager.exe on WSL2 with Debian. I'd always get remote: Repository not found. with no further error.
Instead I did the same with git-credential-manager-core.exe so my config is now credential.helper=/mnt/c/Program\ Files/Git/mingw64/libexec/git-core/git-credential-manager-core.exe. This worked right away, with GitHub 2FA/PAT set up on Windows before hand.

I have the following git versions:

  • Windows:
    git version 2.31.0.windows.1
    Git Credential Manager for Windows v1.20.0.0
  • Debian/WSL2
    git version 2.30.2

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionBorek BernardView Question on Stackoverflow
Solution 1 - GitCarlos BepplerView Answer on Stackoverflow
Solution 2 - GitBruno BronoskyView Answer on Stackoverflow
Solution 3 - GitselkieTGView Answer on Stackoverflow
Solution 4 - GitJonas BrandelView Answer on Stackoverflow
Solution 5 - GitBruceJoView Answer on Stackoverflow
Solution 6 - GitSimon KatanskiView Answer on Stackoverflow
Solution 7 - GitJarioView Answer on Stackoverflow
Solution 8 - GitMormundView Answer on Stackoverflow