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

Bor*_*ard 39 git ubuntu windows-subsystem-for-linux

我试过按照这些说明操作: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
Run Code Online (Sandbox Code Playgroud)

但是当我进行任何网络操作时,我收到此错误:

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

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

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

Car*_*ler 47

如果您安装了Git for Windows,则系统上会安装Windows集成凭据管理器.

您可以从WSL运行Windows可执行文件,如此处所示.

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

git config --global credential.helper "/mnt/c/Program\ Files/Git/mingw64/libexec/git-core/git-credential-manager.exe"
Run Code Online (Sandbox Code Playgroud)

  • `git config --global credential.helper"/ mnt/c/Program\Files/Git/mingw64/libexec/git-core/git-credential-wincred.exe"`这是我使用的命令.如果你有一个带空格的路径,你需要将其转义并将其放在引号中. (29认同)
  • git-credential-wincred.exe 是一个较旧的实现,今天使用 git-credential-manager.exe 会更好。 (6认同)
  • 我认为这应该是“git-credential-manager-core”,因为“git-credential-manager”已被弃用,[根据微软](http://microsoft.github.io/Git-Credential-Manager-for -视窗/) (5认同)
  • 好的,您无法安装,但您可以使用 WSL 中的 Windows 可执行文件,因此您可以使用 git 中的凭据帮助程序在 WSL 上运行的 git 上使用 Windows。这个配置是从我自己的机器上复制过来的。 (2认同)
  • [git-credential-manager](https://github.com/Microsoft/Git-Credential-Manager-for-Windows) 由 Microsoft 在单独的存储库中实现。在该存储库中,有以下消息:“Git Credential Manager for Windows (GCM) 为 Windows 提供安全的 Git 凭证存储。它是 Windows Credential Store for Git (git-credential-winstore) 的后继者,不再维护.” (2认同)
  • @UmarGhouse,我认为这里可能存在新闻混淆,据我了解,“git-credential-manager-core”实际上只是名义上被弃用的(https://aka.ms/gcm/rename) )。如果您转到 git 凭据管理器的 Windows 文件夹,您可以看到有两个凭据管理器可执行文件(一个带有 *-core.exe,另一个没有)。两者之间的版本控制似乎相同。因此,据我了解,他们仍然保留“core.exe”版本只是为了向后兼容。 (2认同)

Bru*_*sky 28

TL; DR

我已经创建了一个脚本来为您完成此操作.我和我的Chef编排一起使用它.

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

  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

将路径从DOS转换为Linux

我们要:

  1. 更换C:\/mnt/c/
  2. 翻转斜杠\/
  3. 具有双反斜杠的转义空格(以及括号中的括号) \\

所以...

  • "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"

配置git

  1. bash电话中git config --global credential.helper "<converted/path>"

  • 从 WSL 的最新版本开始,Linux 端有一个“wslpath”实用程序可以自动转换路径,从 WSL shell 调用“wslpath path\to\convert”,它会为您进行转换。 (4认同)
  • 这很好地解决了.谢谢 (3认同)
  • `git config --global credential.helper "/mnt/c/Program\\ Files/Git/mingw64/libexec/git-core/git-credential-manager.exe"` (3认同)
  • 该脚本对我不起作用-但仍然手动执行步骤,所以+1。 (2认同)

小智 18

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

$ git config --global credential.helper cache
Run Code Online (Sandbox Code Playgroud)

然后做了一个git pull; 当提示输入用户和密码时,我像往常一样输入它们.在那之后,它记住了它.我发现它已将(正确命名的)部分添加到我的〜/ .gitconfig中:

[credential]
        helper = cache
Run Code Online (Sandbox Code Playgroud)

我编辑它以提供更长的超时:

[credential]
        helper = cache --timeout=144000
Run Code Online (Sandbox Code Playgroud)

现在这一切似乎都运转良好.


Jon*_*del 6

alias git=git.exe
Run Code Online (Sandbox Code Playgroud)

将简单地使用 windows 中的 git.exe 及其配置


Bru*_*eJo 5

到目前为止,所有答案都过于复杂。而且 git 文档并没有真正的帮助,他们喜欢参考资料很多,所以你需要按照 2-3 个链接来获取你需要的信息!

  1. 您不需要将 Windowsgit.exe与 WSL一起使用!使用 linux git sudo apt install git-all(我认为它预装了 Ubuntu)。
  2. 然后您只需按照https://git-scm.com/book/en/v2/Git-Tools-Credential-Storage 上的说明操作即可。

概括

git 默认支持 2 种方法。要查看您安装了哪些方法,请执行以下命令:

git help -a | grep credential
Run Code Online (Sandbox Code Playgroud)

我的结果是:

   credential           Retrieve and store user credentials
   credential-cache     Helper to temporarily store passwords in memory
   credential-store     Helper to store credentials on disk
Run Code Online (Sandbox Code Playgroud)

如何缓存和存储:

缓存

@selkieTG 在他们的回答中涵盖了这一点,包括完整性......

git config --global credential.helper "cache --timeout 30000"
Run Code Online (Sandbox Code Playgroud)

将您的密码/令牌缓存 30,000 秒(8 小时 20 分钟)

店铺

git config --global credential.helper "store"
Run Code Online (Sandbox Code Playgroud)

将纯文本密码/令牌存储在~/.git-credentials.

纯文本?!!对于 WSL,我完全可以在这里使用纯文本。我输入凭据以运行我的 Windows 计算机,然后输入凭据以登录 WSL2。我需要隐藏这些吗?不是真的,它在我的开发箱上更方便。

经理核心

如果你真的想使用manager-core你可以在你的Ubuntu 版本中安装它。然后使用它。

  • 希望您还输入 e pw 进行磁盘加密,否则您的密码可能会被泄露。另外,任何恶意软件都可以从“\\wsl$”读取它,祝你好运。 (3认同)