如何在 WSL(Windows 上的 Ubuntu)上使用 Git 凭证存储?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/45925964/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 12:53:04  来源:igfitidea点击:

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

gitubuntuwindows-subsystem-for-linux

提问by Borek Bernard

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

我试过按照以下说明操作:https: //stackoverflow.com/a/40312117/21728基本上是这样做的:

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.

我猜这是合乎逻辑的,因为确实没有 X11 显示器。

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

如何使 Git 凭据缓存在 Windows (WSL) 上的 Ubuntu 上工作?

回答by Carlos Beppler

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

如果您为 Windows 安装了 Git,则您的系统上会安装一个 Windows 集成凭据管理器。

You can run windows executables from WSL as found here.

您可以从WSL如发现运行Windows可执行文件在这里

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

要使用它,您可以运行以下命令(假设您的 git for windows 安装在 C:\Program Files\Git 上)

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

回答by Bruno Bronosky

TL;DR

TL; 博士

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

我已经创建了一个脚本来为你做这件事。我将它与我的 Chef 编排一起使用。

Locate or install git-credential-manager.exe

找到或安装 git-credential-manager.exe

  1. Open cmd.exeand call where git-credential-manager.exe
    • If it returns a path, GREAT. Move on to converting the path.
    • If not...
  2. In cmd.execall 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 Managerfrom Microsoft's git repo, and then use whereagain to get the path.
  1. 打开cmd.exe并调用where git-credential-manager.exe
    • 如果它返回一个路径,很好。继续转换路径。
    • 如果不...
  2. cmd.exe通话where git.exe
    • 如果没有返回路径,下一步就是单独安装Credential Manager
    • 如果它确实返回了一条路径,它将类似于:
    • C:\Program Files\Git\cmd\git.exe
    • 让我们删除最后一个斜杠后的所有内容并像这样更改它:
    • C:\Program Files\Git\mingw64\libexec\git-core\git-credential-manager.exe
    • 如果存在,那太好了。继续转换路径。
    • 除此以外...
  3. Microsoft 的 git repo安装Credential Manager,然后再次使用以获取路径。where

Convert the path from DOS to Linux

将路径从 DOS 转换为 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 \\
  1. 更换C:\/mnt/c/
  2. 将斜杠从 翻转\/
  3. 使用双反斜杠转义空格(和括号,如果有的话) \\

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"
  • "C:\Program Files\Git\mingw64\libexec\git-core\git-credential-manager.exe"变成……
  • "/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?' <<<""
}

But, as @12345ieee has since commented, a wslpathutility 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.)

但是,正如@12345ieee 此后评论的那样WSL 版本 17046 中wslpath添加了一个实用程序。值得一试,但我目前无法访问 Windows 进行验证。(请注意,即使在我的链接中的发行说明中给出了使用说明,但该命令目前似乎不包含使用说明、-h 等)

Configure git

配置git

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

回答by selkieTG

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:

使用 Windows 10 和“WSL”,我创建了一个 ~/.gitconfig 文件,但将 [credential] 部分标签错误地输入为 [credentials]。我尝试运行 git credential fill 然后将其输出提供给 git credential批准,这可能有效,但我怀疑不是因为它说“用法:git credential [fill|approve|reject]”。最后,我只是跑了:

$ 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:

然后做了一个 git pull;当提示输入用户名和密码时,我像往常一样输入它们。在那之后,它记住了它。我发现它已将(正确命名的)部分添加到我的 ~/.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.

现在这一切似乎都运行良好。