115

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)?

4

14 Answers 14

206

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, and is version < v2.36.1)

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

For the latest paths, see the Microsoft tutorial: https://learn.microsoft.com/en-us/windows/wsl/tutorials/wsl-git

16
  • 3
    Ok, you can´t install, but you can use the windows executables from WSL, so you can use the credential helper from git for windows on git running on WSL. This configuration is copied from my own machine. Nov 22, 2017 at 12:36
  • 73
    git config --global credential.helper "/mnt/c/Program\ Files/Git/mingw64/libexec/git-core/git-credential-wincred.exe" This is the command I use. If you have a path with a space you need to escape it and also put it in quotes.
    – Chéyo
    Nov 22, 2017 at 19:54
  • 8
    git-credential-wincred.exe is an older implementation, it will be better to use git-credential-manager.exe today. Feb 21, 2019 at 18:36
  • 3
    git-credential-manager is implemented by Microsoft in a separate repository. In that repository there are the following message: "The Git Credential Manager for Windows (GCM) provides secure Git credential storage for Windows. It's the successor to the Windows Credential Store for Git (git-credential-winstore), which is no longer maintained. " Apr 16, 2020 at 13:41
  • 5
    I think this should be git-credential-manager-core, as git-credential-manager has been deprecated, according to Microsoft May 13, 2021 at 6:26
65

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...
  2. 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...
  3. 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>"
7
  • 3
    The script didn't work for me -- but doing the steps manually still did, so +1.
    – inavda
    Nov 8, 2018 at 16:37
  • 1
    Quick point of clarity if "use Git credential store on WSL (Ubuntu on windows)" indicates 'within' to anyone instead of 'upon': this works unless you are using a linux installation of git and ~/.gitconfig within Ubuntu where you can just do git config credential.helper store or better yet use git-credential-libsecret instead.
    – Scott Wade
    Aug 24, 2019 at 2:36
  • 3
    git config --global credential.helper "/mnt/c/Program\\ Files/Git/mingw64/libexec/git-core/git-credential-manager.exe"
    – sabbour
    Dec 13, 2019 at 23:47
  • 6
    Since recent versions of WSL, there is a wslpath utility on the linux side that can convert paths automatically, call wslpath path\to\convert from the WSL shell and it'll make the conversion for you.
    – 12345ieee
    Feb 26, 2020 at 10:48
  • 1
    Ubuntu-specific solution by @scott-wade should be the correct answer here. Running the Windows exe from within WSL makes no sense if git is being run in the Ubuntu instance.
    – Cahit
    May 31, 2020 at 22:15
45

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.

3
  • This worked out nicely for me. Thanks for a great tip and have a +1 from me! Aug 23, 2018 at 22:57
  • Perfect and simple solution for me, this timeout are in seconds?
    – eutobias
    Jul 22, 2019 at 19:35
  • Worked perfectly with VsCode + WSL2 under Windows 10. Thanks!
    – amiroff
    Jul 20, 2020 at 9:03
26
alias git=git.exe

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

5
  • I struggled with a timeout when trying to run any git command to a remote repository hosted on Github. This alias eliminated the issue! Apr 9, 2021 at 12:57
  • This is an incredibly simple and effective solution to the problem.
    – Jason L.
    Nov 8, 2021 at 18:26
  • This was incredible simple and effective!!!
    – Aaron
    Jan 20, 2022 at 15:27
  • Works perfectly from CLI, but not from VSCode (continue asking for password)
    – equiman
    Mar 5 at 1:26
  • Thank you. Works for me: VSCode + Windows 10 + WSL2
    – Amel
    Aug 9 at 9:13
17

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.

1
  • 3
    hopefully you also enter e pw for the disk encryption, otherwise your password may be exposed. Also, any malware can read it from \\wsl$ Good luck. Sep 28, 2021 at 20:45
4

Consider Microsoft GCM v2.1.1 (May 2023), as it includes:

  • Better browser detection inside of WS (#1148, resolving issue 878: "for wslview: Browser authentication disabled if DISPLAY variable is not set")
  • Better documentation on the GCM install paths in WSL (#1168)

The installation is described here:

Start by installing the latest Git for Windows.

Inside your WSL installation, run the following command to set GCM as the Git credential helper:

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

Note: the location of git-credential-manager.exe may be different in your installation of Git for Windows.

This is now git-credential-manager.exe, no longer git-credential-manager-core.exe (since Git 2.39.0+, Dec. 2022, and GCM v2.0.877 (Nov. 2022), PR 551).

And, as I described here, for a credential per repository:

git config --global credential.UseHttpPath true

As noted by Loligans in the comments, regarding the location of that executable:

If you don't know where your git executable location is and you are able to execute git on the command line then you can use the following command: git --exec-path which will give you the git executable location.
This should help narrow down your search

(Although I had a different opinion in 2017)
(I mentioned --exec-path here)

3
  • If you don't know where your git executable location is and you are able to execute git on the command line then you can use the following command: git --exec-path which will give you the git executable location. This should help narrow down your search
    – Loligans
    Jun 12 at 3:06
  • 1
    @Loligans Thank you for your feedback. I have included your comment in the answer for more visibility.
    – VonC
    Jun 12 at 9:56
  • This is a great answer. Thank you. Super easy to setup. Sep 12 at 23:08
3

It's already documented on vscode-docs:


Sharing Git credentials between Windows and WSL

If you use HTTPS to clone your repositories and have a credential helper configured in Windows, you can share this with WSL so that passwords you enter are persisted on both sides. (Note that this does not apply to using SSH keys.)

Just follow these steps:

  1. Configure the credential manager on Windows by running the following in a Windows command prompt or PowerShell:

     git config --global credential.helper wincred
    
  2. Configure WSL to use the same credential helper, but running the following in a WSL terminal (assuming you have git >= v2.39.0):

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

To know the path for previous versions of git, please refer to https://learn.microsoft.com/en-us/windows/wsl/tutorials/wsl-git

Any password you enter when working with Git on the Windows side will now be available to WSL and vice versa.


Source: https://github.com/microsoft/vscode-docs/blob/vnext/docs/remote/troubleshooting.md#sharing-git-credentials-between-windows-and-wsl

1
  • 1
    Note that the WSL side is now called just "git-credential-manager.exe". See aka.ms/gcm/rename
    – ecatmur
    Jul 3 at 4:59
2

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

1
  • This answer only works for git < v2.36.1
    – Nzbuu
    Jul 14 at 11:33
0

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
0

Install git on wsl Ubuntu

sudo apt install git

Set email and username

git config --global user.email "YOUR_EMAIL"
git config --global user.name "YOUR_USERNAME"

Install Git credentials manager

wget "https://github.com/GitCredentialManager/git-credential-manager/releases/download/v2.0.886/gcm-linux_amd64.2.0.886.deb" -O /tmp/gcmcore.deb
sudo dpkg -i /tmp/gcmcore.deb
git-credential-manager configure

Set credentials manager to read from cache

git config --global credential.credentialStore cache
0

To use Git credential store on WSL (Ubuntu on Windows), you can follow these steps:

  1. Configure the Windows Git credential helper by running the following command:
git config --global credential.helper "/mnt/c/Program\ Files/Git/mingw64/bin/git-credential-manager.exe"
  1. Ensure that the HttpPath is used to prevent the error message "fatal: Cannot determine the organization name for this 'dev.azure.com' remote URL." You can do this by running the following command:
git config --global credential.useHttpPath true

By following these steps, you should be able to use the Git credential store on WSL (Ubuntu on Windows) successfully.

0

I just bring over my .gitconfig from C:/Users/[Myself]/.gitconfig (Windows) to the git config file in Debian (WSL) which is located at ~/.gitconfig.

You can also open both in the respective operating systems by using git config --global --edit as a command.

You could do some fancy linking of the files but what I do is just the simple and easy copying of the contents from my Windows system into my Linux system.

Both should end up looking at least like the following:

[user]
    name = YourName
    email = [email protected]
0

This tutorial from Microsoft helped me. Basicly says:

If GIT installed is >= v2.39.0

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

else if GIT installed is >= v2.36.1

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

else if version is < v2.36.1

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

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.