92

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

Borek Bernard
  • 47,195
  • 54
  • 159
  • 231
  • 1
    Helpful tutorial: https://www.edwardthomson.com/blog/git_credential_manager_with_windows_subsystem_for_linux.html – 0112 Mar 14 '20 at 01:40

8 Answers8

160

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"
Carlos Beppler
  • 2,084
  • 1
  • 15
  • 16
  • This is about Windows Subsystem for Linux (WSL), not MSysGit. You can't install Git for Windows inside of the WSL. – carlin.scott Nov 20 '17 at 18:15
  • 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. – Carlos Beppler Nov 22 '17 at 12:36
  • 53
    `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 '17 at 19:54
  • 1
    I had to install the Creators Update for Windows before this worked for me. I also had to use this exact string in my .gitconfig: "/mnt/c/Program\\ Files\\ \\\(x86\\\)/Git/mingw32/libexec/git-core/git-credential-wincred.exe" – carlin.scott Dec 08 '17 at 18:40
  • This has been so useful. Now I don't need both `Git Bash` and `WSL Bash` open to work on a repo. One question, what is the difference between `git-credential-wincred.exe` and `git-credential-manager.exe`? – Ehtesh Choudhury Feb 18 '19 at 19:20
  • 6
    git-credential-wincred.exe is an older implementation, it will be better to use git-credential-manager.exe today. – Carlos Beppler Feb 21 '19 at 18:36
  • works like a charm, this should be the accepted answer. – wilsontgh Mar 23 '20 at 09:48
  • @CarlosBeppler Is there an explanation of the difference anywhere? – Alexey Romanov Apr 15 '20 at 14:22
  • 3
    [git-credential-manager](https://github.com/Microsoft/Git-Credential-Manager-for-Windows) 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. " – Carlos Beppler Apr 16 '20 at 13:41
  • Actually you don't even need to have git installed on Windows. You may just as well unzip the archive (found [here](https://github.com/Microsoft/Git-Credential-Manager-for-Windows/releases/latest)) anywhere on your linux distribution, give *.exe files executable permissions and then set `credential.helper` config value accordingly (as shown in this answer). Note: .NET Framework 4.5.1 or greater is required. – Karolis Jul 19 '20 at 13:44
  • Don't need to use Windows git for WSL – BruceJo Apr 02 '21 at 22:49
  • Yes, you do not need, but the credential manager of Windows git can be integrated with The WSL git, só you do not need to replicate your credentials. – Carlos Beppler Apr 03 '21 at 23:03
  • 2
    I think this should be `git-credential-manager-core`, as `git-credential-manager` has been deprecated, [according to Microsoft](http://microsoft.github.io/Git-Credential-Manager-for-Windows/) – Umar Ghouse May 13 '21 at 06:26
  • Yes, thanks, I'll update the instructions. – Carlos Beppler May 14 '21 at 11:27
  • The path is again updated in the current version 2.36.1 of "Git for Windows": `git config --global credential.helper "/mnt/c/Program\ Files/Git/mingw64/bin/git-credential-manager-core.exe"` – Ben JW May 22 '22 at 12:13
56

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>"
Bruno Bronosky
  • 60,791
  • 11
  • 147
  • 135
  • 3
    The script didn't work for me -- but doing the steps manually still did, so +1. – inavda Nov 08 '18 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 '19 at 02:36
  • 3
    `git config --global credential.helper "/mnt/c/Program\\ Files/Git/mingw64/libexec/git-core/git-credential-manager.exe"` – sabbour Dec 13 '19 at 23:47
  • 5
    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 '20 at 10:48
  • Thanks for the feedback @12345ieee! I have added that to my answer. – Bruno Bronosky Feb 27 '20 at 00:44
  • 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 '20 at 22:15
  • 1
    Running the Windows credential manager from WSL makes perfect sense. As the new GCM core (git-credential-manager-core.exe?) actually supports multi-factor auth on 3 major Git providers and the user is likely already logged in on the Windows provider. Something libsecret won't help you with. – Daniel Friesen Jan 20 '21 at 04:55
41

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.

selkieTG
  • 411
  • 4
  • 2
14
alias git=git.exe

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

Jonas Brandel
  • 1,075
  • 10
  • 7
7

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.

BruceJo
  • 479
  • 5
  • 13
  • 2
    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. – inetphantom Sep 28 '21 at 20:45
3

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

Simon Katanski
  • 421
  • 7
  • 8
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
Henry Ecker
  • 31,792
  • 14
  • 29
  • 50
Jario
  • 1
  • 2
-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
Mormund
  • 308
  • 3
  • 8