繁体   English   中英

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

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

我尝试按照以下说明操作: 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

但是当我做任何网络操作时,我得到这个错误:

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

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

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

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

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

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

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

TL; 博士

我已经创建了一个脚本来为你做这件事 我将它与我的 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. /mnt/c/替换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"

我上面的脚本有一个功能可以做到这一点

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

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

配置git

  1. bash调用git config --global credential.helper "<converted/path>"

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

$ git config --global credential.helper cache

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

[credential]
        helper = cache

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

[credential]
        helper = cache --timeout=144000

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

alias git=git.exe

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

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

  1. 您不需要将 Windows git.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

我的结果是:

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

如何缓存和存储:

缓存

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

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

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

商店

git config --global credential.helper "store"

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

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

经理核心

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

我最近刚刚更新到 WSL2,在我的情况下,以下内容不起作用:

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

有效的是以下内容: git config --global credential.helper "/c/Program\\\\ Files/Git/mingw64/libexec/git-core/git-credential-manager.exe"

在我从路径中删除/mnt/ ,我收到了“未找到”错误。

根据我的调查,在干净的 Windows 启动后,在 WSL2 中安装 Windows 驱动器存在问题,更多详细信息请点击此处: https://github.com/microsoft/WSL/issues/4122 : https://github.com/microsoft/WSL/issues/4122在我的情况下,这是最可能的原因.

另一个原因可能是/etc/wsl.conf中根目录的错误配置

无法在 WSL2 和 Debian 上使用git-credential-manager.exe进行此操作。 我总是得到remote: Repository not found. 没有进一步的错误。
相反,我对git-credential-manager-core.exe做了同样git-credential-manager-core.exe所以我的配置现在是credential.helper=/mnt/c/Program\\ Files/Git/mingw64/libexec/git-core/git-credential-manager-core.exe 这立即奏效,事先在 Windows 上设置了 GitHub 2FA/PAT。

我有以下 git 版本:

  • 窗户:
    git 版本 2.31.0.windows.1
    Windows 版 Git 凭证管理器 v1.20.0.0
  • Debian/WSL2
    版本 2.30.2

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

安装:

sudo apt install gcmcore -y 或

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

配置:

导出 DOTNET_SYSTEM_GLOBALIZATION_INVARIANT=1

git-credential-manager-core 配置

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM