【问题标题】:How to use git with gnome-keyring integration如何将 git 与 gnome-keyring 集成一起使用
【发布时间】:2012-11-03 08:30:27
【问题描述】:

Git 1.8.0 支持与 gnome-keyring 集成。

http://www.h-online.com/open/news/item/Git-1-8-0-can-access-Windows-and-GNOME-keyrings-1733879.html

阅读有关 git 凭证助手的文档后:http://git-scm.com/docs/gitcredentials.html

我无法找到使用此新功能的方法。我该如何整合它? 我正在使用 Archlinux 和从 Archlinux 的存储库安装的 git。 (git 1.8.0)

【问题讨论】:

    标签: git gnome


    【解决方案1】:

    @marcosdsanchez 的答案是针对 Arch(它回答了原始问题),但我在 Ubuntu 上。对于 git >= 2.11:

    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
    

    对于 git

    sudo apt-get install libgnome-keyring-dev
    cd /usr/share/doc/git/contrib/credential/gnome-keyring
    sudo make
    git config --global credential.helper /usr/share/doc/git/contrib/credential/gnome-keyring/git-credential-gnome-keyring
    

    【讨论】:

    • 一开始我很犹豫,但我终于做到了,而且效果很好。正如docs for gitcredentials 所说,您可能还想做git help -a | grep credential- 并查看是否安装了其他帮助程序。默认出现的是credential-cache(输入密码后记住一段时间,默认15分钟)和credential-store(只需将密码以明文形式存储在磁盘上的未加密文件中,默认~/.git-credentials) .
    • 谁能解释一下为什么这需要手动编译并且默认情况下不能通过包获得?
    • 在 Ubuntu 14.04 LTS 中工作!
    • 只需要chmod 0755 git-credential-gnome-keyring 为除root 以外的其他人添加执行权限
    • 对 Ubuntu 16.04 LTS 仍然有效
    【解决方案2】:

    Git 1.8.0 带有 gnome-keyring 支持,但需要为您的平台编译二进制文件。

    这就是在 Archlinux 中为我解决的问题:

    $ sudo pacman -S libgnome-keyring
    $ cd /usr/share/git/credential/gnome-keyring
    $ make
    $ git config --global credential.helper /usr/share/git/credential/gnome-keyring/git-credential-gnome-keyring
    

    @VonC 解决方案很接近,但 git config 命令应该指向可执行文件。这就是为什么它对我不起作用。

    【讨论】:

    • 我已重新格式化我的答案以反映您的结论。我已经构建了它(它不是在我的 git 1.8 发行版中默认构建的)并在今天早上对其进行了测试。它确实有效。
    • 如果您收到Package gnome-keyring-1 was not found in the pkg-config search path.,则您缺少 gnome-keyring 的开发库。在 Ubuntu 上,这些可通过apt-get install libgnome-keyring-dev 获得。此外,我必须从github.com/git/git/tree/master/contrib 手动下载 git contrib repo 并将其放入 /usr/share/git-core/。这些文件不再包含在默认的 git 安装中,至少使用官方的 git-core ubuntu ppa。
    • 这很好地解释了为什么 aur 包 git-credential-gnomekeyring 消失了,太糟糕了,在 AUR 中没有这样的信息。
    • 2017 年在 Arch 上,只需安装 libgnome-keyring 就足够了(至少如果您使用的是 gnome)。
    • libgnome-keyring is now deprecated 并且需要安装 org.freedesktop.secrets 软件包之一。其中新的gnome-keyring就是其中之一。
    【解决方案3】:

    2016 年第四季度更新:

    • Unix、Mac (Git 2.11+)

      git config --global credential.helper libsecret
      

    (见“Error when using Git credential helper with gnome-keyring”)

    • 窗户:

      git config --global credential.helper manager
      

    (见“How to sign out in Git Bash console in Windows?”:即Git for Windows使用最新的Microsoft Git Credential Manager for Windows


    原始答案(2012 年)

    Credential Helpers,适用于 Windows、Mac 和 Unix 平台,首先在 "git-credential-helper" repo 中引入,现在已包含在 git 发行版中

    此存储库包含一组 Git 凭据助手 (gitcredentials(7)),它们是 git 的一部分(或打算在未来提供)。

    $ git clone git://github.com/pah/git-credential-helper.git
    $ BACKEND=gnome-keyring      # or any other backend
    $ cd git-credential-helper/$BACKEND
    $ make
    $ cp git-credential-$BACKEND /path/to/git/crendential
    

    构建时会安装在/path/to/git/credential目录下。

    要使用此后端,您可以通过设置将其添加到(全局)Git 配置中

    (这里是 Unix):

    git config --global credential.helper /path/to/git/credential/gnome-keyring/git-credential-gnome-keyring
    

    Windows 的注意事项:

    我想你可以制作一个在 Windows 上运行的程序并调用像“pypi keyring 0.10”这样的库。
    但那是后端,你不能直接从 Git 中使用它。

    您使用的是“凭证助手”(反过来,它将调用any credential API it wants on Windows)。

    GitHub for Windows 提供了这样一个帮助程序(作为一个名为... github 的可执行文件),并且可以在 Windows 会话期间存储您的凭据。
    从“GitHub for Windows”窗口启动一个 shell,你会看到,输入“git config --system -l”:

    C:\Users\VonC\Documents\GitHub\test [master +2 ~0 -0 !]> git config --system -l
    credential.helper=!github --credentials
    

    credential.helper=!github --credentials 部分将调用凭证助手“github”。

    $ git config [--global] credential.helper $BACKEND
    

    【讨论】:

    • 不是我要找的。答案应该是 *nix only。
    • @marcosdsanchez 好的,我已经编辑了我的答案,包括在 Unix 上使用 Git 设置和使用 gnome-keyring。
    • 我想使用 git 的 1.8.0 功能,该功能已经存在。不是第三方代码。谢谢。
    • @marcosdsanchez 然后你需要编译github.com/git/git/tree/master/contrib/credential/gnome-keyring(它是用git打包的)。编译完成后,您将按照我的回答进行安装和使用。
    • 我猜没有内置二进制文件?
    【解决方案4】:

    2018 年 10 月更新

    GNOME 已弃用 libgnome-keyring 并将其替换为 libsecret。提交https://github.com/git/git/commit/87d1353a6a 添加了一个新的凭证助手/usr/libexec/git-core/git-credential-libsecret。

    git config --global credential.helper libsecret

    【讨论】:

    • 可能,这应该是对此处任何基于 gnome-keyring 的答案的评论。
    • 同意,应相应地编辑基于gnome-keyring 的答案。也许只是排除整个选项。这对我在 Ubuntu 19.10 上根本不起作用。
    【解决方案5】:

    对于 Fedora 上的任何人,我稍微编辑了 James Ward 的答案:

    sudo yum install libgnome-keyring-devel
    cd /usr/share/doc/git/contrib/credential/gnome-keyring
    sudo make
    git config --global credential.helper /usr/share/doc/git/contrib/credential/gnome-keyring/git-credential-gnome-keyring
    

    【讨论】:

    • 我很困惑,这不是已经包含在 git 包中了吗?那么这个文件在 fc19 包中是什么,git-1.8.3.1-1.fc19.x86_64。这里有这个文件:/usr/libexec/git-core/git-credential-gnome-keyring.
    • 在 Fedora 21 的 git 2.1.0 中,你可以简单地这样做:git config --global credential.helper gnome-keyring
    • Fedora 32:sudo dnf install git-credential-libsecret 然后git config --global credential.helper libsecret
    【解决方案6】:

    只需将这两行添加到您的 ~/.gitconfig 文件中:

    [credential]
        helper = gnome-keyring
    

    下次 Git 要求你输入密码时,输入的密码会保存到 Gnome Keyring 中(你可以用seahorse 工具看到这个),之后就不会再要求你输入密码了。

    这假设您的 Git 版本足够新(如 2.1.0)并且您在 Linux Fedora、RHEL 或 CentOS 下。对于旧版本或其他操作系统/发行版,请查看其他答案。

    【讨论】:

    • 在 Ubuntu 16.04 中:git: 'credential-gnome-keyring' is not a git command.
    • @Saibot:哎呀,你是对的。看起来 Fedora/RHEL 提供了这个,但 Ubuntu 没有。所以在这种情况下,James Ward (stackoverflow.com/a/14528360/2148773) 的答案会更好。
    • 在 RHEL 中,如果尚未安装,则需要安装 git-gnome-keyring。
    【解决方案7】:

    某些发行版确实将此集成作为安装包提供,无需任何编译。根据您的 GNOME 版本,您将需要安装 gnome-keyringlibsecret 版本的软件包,例如 git-credential-gnome-keyring (OpenSUSE Leap 42.3)。

    但是,这本身不会启用 Git 与 GNOME 密钥环的自动集成。您仍然必须将 Git 配置为使用这种凭证存储方法:

    git config --global credential.helper gnome-keyring # If you installed git-credential-gnome-keyring
    git config --global credential.helper libsecret     # If you installed git-credential-libsecret
    

    【讨论】:

    • 在 Ubuntu 19.10 上,gnome-keyring 选项对我不起作用,但 James Ward 使用 libsecret 的指令仍然有效。然而,我对这里给出的使用 gnome-keyring 的建议感到困惑:github.com/timhughes/git-credential-libsecret
    • libsecret(或find /usr -iname git-credential-libsecret 给出的)是Gentoo Linux 中的正确选择。请注意,dev-vcs/git 必须与 USE=gnome-keyring 一起出现。
    【解决方案8】:

    在 Fedora 上,您需要安装

    $ sudo dnf install git-credential-libsecret
    

    并编辑您的 git 配置 以使用凭证助手。

    [credential]
        helper = /usr/libexec/git-core/git-credential-libsecret
    

    仅供参考,libsecret 包最近已拆分,请参阅post from @rugk。这就是为什么用户需要重新安装这个包。

    【讨论】:

      【解决方案9】:

      我在无头服务器上尝试the answer for Ubuntu,输入令牌时出现以下错误:

      Remote error from secret service: org.freedesktop.DBus.Error.UnknownMethod: No such interface 'org.freedesktop.Secret.Collection' on object at path /org/freedesktop/secrets/collection/login
      
      store failed: No such interface 'org.freedesktop.Secret.Collection' on object at path /org/freedesktop/secrets/collection/login
      

      这是在无头服务器上为我工作的解决方案(请参阅https://keyring.readthedocs.io/en/latest/#using-keyring-on-headless-linux-systems):

      • 首先我运行与the answer 相同的命令,将git-credential-libsecret 设置为credential.helper
      # You may also first install gnome-keyring if not installed
      sudo apt install gnome-keyring
      sudo apt 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
      
      • 然后,每当我开始使用凭据的会话(例如,git push 之类的命令)时,我都会运行:
      dbus-run-session -- sh  # Replace 'sh' with whatever shell you use.
      gnome-keyring-daemon --unlock
      # Enter your token here, then hit Enter, then Ctrl+d
      # You might clean the terminal display with Ctrl+l for security reasons
      

      这会运行一个 D-Bus 会话,我可以在其中运行 git push 之类的自动身份验证。

      【讨论】:

      • 我觉得这个答案很接近,但是打开一个新的 shell 进程并启动一个守护进程只是为了运行我的 git 命令几乎和每次都输入凭据一样麻烦。没有新的 shell 进程就不能运行 dbus 吗?
      • @jiggunjer 这是一个很好的观点。我个人所做的是我在远程机器上的 Tmux 会话中工作。这样我只需要运行一次上述命令:每次我在远程机器上恢复工作时,我都会重新连接 Tmux 会话,守护程序仍在运行。
      猜你喜欢
      • 1970-01-01
      • 2020-02-08
      • 2016-03-18
      • 2016-08-03
      • 2019-06-24
      • 1970-01-01
      • 1970-01-01
      • 2011-05-31
      • 1970-01-01
      相关资源
      最近更新 更多