简体   繁体   English

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

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

I've tried following these instructions: https://stackoverflow.com/a/40312117/21728 which 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 上工作?

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-core.exe"

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.exe and call where git-credential-manager.exe打开cmd.exe并调用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.execmd.exe调用where git.exe
    • If it does not return a path, the next step is to install the Credential Manager alone如果没有返回路径,下一步就是单独安装Credential Manager
    • 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.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//mnt/c/替换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... "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?' <<<"$1"
}

But, as @12345ieee has since commented , a wslpath utility has been added to WSL build 17046 .但是,正如@12345ieee 此后评论的那样,一个wslpath实用程序已添加到WSL 版本 17046 中 It's worth checking out, but I don't have access to Windows at this time to verify.值得一试,但我目前无法访问 Windows 进行验证。 (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.) (请注意,即使在我的链接的发行说明中给出了使用说明,但该命令目前似乎不包含使用说明、-h 等)

Configure git配置git

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

Using Windows 10 and "WSL", I created a ~/.gitconfig file, but had mistyped the [credential] section label as [credentials].使用 Windows 10 和“WSL”,我创建了一个 ~/.gitconfig 文件,但将 [credential] 部分标签错误地输入为 [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]".我尝试运行 git credential fill 然后将其输出提供给 git credential批准,这可能有效,但我怀疑不是因为它说“用法:git credential [fill|approve|reject]”。 Finally, I simply ran:最后,我只是跑了:

$ git config --global credential.helper cache

and then did a git pull;然后做了一个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:我发现它已将(正确命名的)部分添加到我的 ~/.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.现在这一切似乎都运行良好。

alias git=git.exe

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

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!而且 git 文档并没有真正的帮助,他们喜欢参考资料很多,所以你需要按照 2-3 个链接来获取你需要的信息!

  1. You do not need to use Windows git.exe with WSL!您不需要将 Windows git.exe与 WSL 一起使用! Use linux git sudo apt install git-all (I think it comes pre-installed with Ubuntu).使用 linux git sudo apt install git-all (我认为它预装了 Ubuntu)。
  2. Then you can simply follow the instructions at https://git-scm.com/book/en/v2/Git-Tools-Credential-Storage .然后您只需按照https://git-scm.com/book/en/v2/Git-Tools-Credential-Storage 上的说明操作即可。

Summary总结

git supports 2 methods by default. git 默认支持 2 种方法。 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... @selkieTG 在他们的回答中涵盖了这一点,包括完整性......

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

will cache your password/token for 30,000 seconds (8 hrs 20min)将您的密码/令牌缓存 30,000 秒(8 小时 20 分钟)

store商店

git config --global credential.helper "store"

will store plain text password/token in ~/.git-credentials .将纯文本密码/令牌存储在~/.git-credentials

Plain Text?!!纯文本?! For WSL, I am absolutely OK with plain text here.对于 WSL,我完全可以在这里使用纯文本。 I enter credentials to run my Windows machine and I enter credentials to sign into WSL2.我输入凭据以运行我的 Windows 计算机,然后输入凭据以登录 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 .如果你真的想使用manager-core你可以在你的Ubuntu 版本中安装它。 And then use it.然后使用它。

I have just recently updated to WSL2 and in my case the following wasn't working:我最近刚刚更新到 WSL2,在我的情况下,以下内容不起作用:

"/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"有效的是以下内容: 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.在我从路径中删除/mnt/ ,我收到了“未找到”错误。

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

Another reason for this can be a misconfiguration of root directory in /etc/wsl.conf另一个原因可能是/etc/wsl.conf中根目录的错误配置

Couldn't get this working with git-credential-manager.exe on WSL2 with Debian.无法在 WSL2 和 Debian 上使用git-credential-manager.exe进行此操作。 I'd always get remote: Repository not found.我总是得到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 .相反,我对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 This worked right away, with GitHub 2FA/PAT set up on Windows before hand.这立即奏效,事先在 Windows 上设置了 GitHub 2FA/PAT。

I have the following git versions:我有以下 git 版本:

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

download: (gcmcore-linux_amd64.2.0.567.18224.deb) https://github.com/GitCredentialManager/git-credential-manager/releases/tag/v2.0.567下载:(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 apt install gcmcore -y 或

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

configure:配置:

export DOTNET_SYSTEM_GLOBALIZATION_INVARIANT=1导出 DOTNET_SYSTEM_GLOBALIZATION_INVARIANT=1

git-credential-manager-core configure git-credential-manager-core 配置

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

相关问题 如何在 Ubuntu 上使用 Git Credential Manager 存储 Github 令牌? - How can I store Github token with Git Credential Manager on Ubuntu? 如何在 Windows 的 Git Credential Manager 中的每个 repo 本地存储凭据? - How to store credentials locally per repo in Git Credential Manager for Windows? "如何在 Windows 10 Ubuntu bash (WSL) 上使用 difftool 和 mergetool" - How to use difftool and mergetool on Windows 10 Ubuntu bash (WSL) 无法在 Ubuntu WSL 中下载/使用 git - Unable to download/ use git in Ubuntu WSL 如何将 windows 凭证管理器与 ubuntu 连接 - How to connect windows credential manager with ubuntu 通过wsl (ubuntu) &amp; windows终端使用git,如何将默认分支名称改为main? - Using git through wsl (ubuntu) & windows terminal, how do you change the default branch name to main? 在Windows上的Ubuntu上使用Git和VS代码以及Bash(WSL) - Using Git with VS Code and Bash on Ubuntu on Windows (WSL) 如何在 Ubuntu 上对 Github 进行身份验证? git: &#39;credential-netrc&#39; 不是 git 命令 - How to authenticate Github on Ubuntu? git: 'credential-netrc' is not a git command 如何确定Windows版Git Credential Manager的版本? - How to determine the version of Git Credential Manager for Windows? 在 WSL2 上找不到 Git 凭据管理器 - Git Credential Manager Not Found on WSL2
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM