如何使用 git 与 gnome-keyring 集成

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)

82751 次浏览

二零一六年第四季更新:

  • Unix,Mac (Git 2.11 +)

    git config --global credential.helper libsecret
    

(See "Error when using Git credential helper with gnome-keyring")

  • Windows:

    git config --global credential.helper manager
    

(See "How to sign out in Git Bash console in Windows?": That is Git for Windows using the latest Microsoft Git Credential Manager for Windows)


Original answer (2012)

Credential Helpers, for Windows, Mac and Unix platforms, have been introduced first in "git-credential-helper" repo, which now has been included in git distro:

This repository contains the set of Git credential helpers (gitcredentials(7)) that are part of git (or meant to be contributed in the future).

$ 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目录中。

要使用此后端,可以通过设置

(点击这里查看 Unix) :

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

视窗注意事项:

我想你可以编写一个在 Windows 上运行的程序,调用一个类似于“ Pypi keyring 0.10”的库。
但这是后端,您不能直接从 Git 使用它。

您正在使用的是一个“凭据助手”(反过来,它将调用 它想在 Windows 上使用的任何凭证 API)。

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

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 配置命令应该指向可执行文件。这就是为什么它对我不起作用。

@ marcosdsanchez 的回答是 Arch (它回答了最初的问题) ,但是我在 Ubuntu 上:

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

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

对于 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

只需在 ~/.gitconfig文件中添加以下两行:

[credential]
helper = gnome-keyring

下次 Git 要求输入密码时,输入的密码将被保存到 Gnome Keyring (您可以通过 seahorse工具看到这一点) ,之后您将不会再被要求输入密码。

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

2018年10月最新情况

GNOME 已经废弃了 libgnome-keyring,并将其替换为 libsecret。

git config --global credential.helper libsecret

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

但是,这本身不会自动启用 Git 与 GNOME Keyring 的集成。您仍然必须将 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

在软呢帽上你需要 安装

$ sudo dnf install git-credential-libsecret

并编辑 Git 配置以使用凭据助手。

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

供参考,libsecret软件包最近被拆分了,参见 来自@rugk 的邮件。这就是为什么用户需要重新安装这个软件包。

我在 无头服务器上尝试 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) :

  • 首先,我运行与 答案中相同的命令,将 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和类似的自动身份验证。