如何使用git与gnome-keyring集成

152
10个回答

229

@marcosdsanchez的回答适用于Arch Linux(回答了原始问题),但我使用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 < 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

12
一开始我有些犹豫,但最终我还是去做了,而且效果非常好。根据gitcredentials文档中的说法,你也许想要运行 git help -a | grep credential- 命令查看是否安装了其他帮助程序。默认情况下会有两个:credential-cache (在输入密码后记住密码一段时间,默认为15分钟) 和 credential-store (将密码明文存储在未加密的磁盘文件中, 默认路径为 ~/.git-credentials)。 - ShreevatsaR
69
为什么这需要手动编译,而不是通过软件包默认提供?能否有人解释一下? - lanoxx
4
刚刚需要运行命令 chmod 0755 git-credential-gnome-keyring,以添加除 root 用户外的其他用户执行权限。 - Lari Hotari
4
@lanoxx:请看 https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=740739 。似乎只是因为维护者缺乏时间和注意力。 - Will Manley
8
仍适用于Ubuntu 16.04 LTS。 - Eugene Kulabuhov
显示剩余22条评论

60

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发行版中不是默认构建的),并在今天早上进行了测试。它确实可以工作。 - VonC
2
如果您看到“在pkg-config搜索路径中找不到包gnome-keyring-1。”的信息,则说明您缺少gnome-keyring的开发库。在Ubuntu上,您可以通过apt-get install libgnome-keyring-dev安装这些库。此外,我还需要从https://github.com/git/git/tree/master/contrib手动下载git contrib repo并将其放置在/usr/share/git-core/中。这些文件不再包含在默认的Git安装中,至少在使用官方Git-Core Ubuntu PPA时是如此。 - Johann
1
这很好地解释了为什么aur包git-credential-gnomekeyring已经不存在了,遗憾的是在AUR中没有这样的信息。 - ryenus
在Ubuntu上,我在/usr/share/目录下没有git文件夹...只有gitweb和git-core。但是我使用的是Git 1.7.9版本。这可能是原因吗...? - temporary_user_name
1
截至2017年在Arch上,只需安装libgnome-keyring即可(至少如果您正在使用gnome)。 - ald.li
3
libgnome-keyring现已被弃用,需要安装org.freedesktop.secrets包之一。其中新的gnome-keyring是其一。 - Peilonrayz

29

2016年第四季度更新:

  • Unix,Mac(Git 2.11+)

     git config --global credential.helper libsecret
    
(请参见“在使用Git凭证助手时出现gnome-keyring错误”)
  • Windows:

    git config --global credential.helper manager
    

(参见“如何在Windows的Git Bash控制台中注销?”:这是使用最新的Git for WindowsMicrosoft Git Credential Manager for Windows

提醒: libgnome-keyring 是特定于GNOME的,并且:


原始答案(2012年)

凭证助手已经在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 中使用它。

你正在使用的是一个“凭证助手”(它将调用 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

不是我要找的答案。答案应该只涉及*nix系统。 - marcosdsanchez
@marcosdsanchez 好的,我已经编辑了我的答案,包括在Unix上使用gnome-keyring和Git的设置和用法。 - VonC
我想使用已经存在的git 1.8.0功能,而不是第三方代码。谢谢。 - marcosdsanchez
@marcosdsanchez 那么你需要编译 https://github.com/git/git/tree/master/contrib/credential/gnome-keyring(它与git捆绑在一起)。编译完成后,您可以按照我的答案中所示进行安装和使用。 - VonC
我猜没有二进制内置函数? - marcosdsanchez

21

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


2
可能,这应该是对任何基于gnome-keyring的答案的评论。 - Murmel
同意,基于“gnome-keyring”的答案应相应地进行编辑。也许可以直接排除整个选项。在我的Ubuntu 19.10上根本不起作用。 - Mario

10

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

3
我困惑了,这不是已经包含在 git 软件包中了吗?那么在 fc19 包中,git-1.8.3.1-1.fc19.x86_64 中的这个文件是什么呢?这里有一个文件:/usr/libexec/git-core/git-credential-gnome-keyring - slm
10
在Fedora 21中,使用 git 2.1.0,您只需执行以下命令即可:git config --global credential.helper gnome-keyring - RedPoppy
4
Fedora 32:sudo dnf install git-credential-libsecret 然后 git config --global credential.helper libsecret - vk5tu
这种方法现在已经过时了。 - undefined

8

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

[credential]
    helper = gnome-keyring

下次你在Git中输入密码时,密码将保存到Gnome Keyring(你可以使用 seahorse 工具查看),之后你就不会再被要求输入密码了。
这假设你的Git版本足够新(如2.1.0),并且你正在使用Fedora、RHEL或CentOS操作系统。对于旧版本或其他操作系统/发行版,请查看其他答案。

8
在Ubuntu 16.04中: git: 'credential-gnome-keyring'不是一个git命令。 - Ferit
@Saibot:哎呀,你说得对。看起来 Fedora/RHEL 提供了这个功能,但 Ubuntu 没有。所以在这种情况下,James Ward(https://dev59.com/4WYr5IYBdhLWcg3wy9KA#14528360)的答案更好。 - oliver
在RHEL中,如果尚未安装,您需要安装git-gnome-keyring。 - Alejandro López

4

在 Fedora 上,您需要安装

$ sudo dnf install git-credential-libsecret

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

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

提醒一下,libsecret软件包最近被拆分了,详情请见@rugk的帖子。这就是为什么用户需要重新安装此软件包的原因。


3

我试图在一个无头服务器上尝试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等。


1
我觉得这个答案很接近,但是为了运行我的git命令而打开一个新的shell进程并启动守护进程几乎和每次输入凭据一样麻烦。你不能在不打开新的shell进程的情况下运行dbus吗? - jiggunjer
@jiggunjer 这是个好主意。我个人的做法是,在远程机器上工作时,我会在一个 Tmux 会话里进行。这样一来,我只需要运行以上命令一次:每次继续在远程机器上工作时,我只需重新连接到 Tmux 会话中,守护进程仍然在运行。 - Giuseppe

3

有些发行版会作为安装包提供此集成功能,无需进行编译。根据您的 GNOME 版本,您需要安装软件包的 gnome-keyringlibsecret 版本,在 OpenSUSE Leap 42.3 中类似于 git-credential-gnome-keyring

然而,这本身并不会自动启用 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

1
在Ubuntu 19.10上,“gnome-keyring”选项不再适用于我,但是James Ward的使用“libsecret”的指令仍然有效。然而,我对此处建议使用“gnome-keyring”感到困惑:https://github.com/timhughes/git-credential-libsecret - Mario
libsecret(或者find /usr -iname git-credential-libsecret的结果)是在Gentoo Linux中的正确选项。请注意,必须使用USE=gnome-keyring来安装dev-vcs/git - sphakka

1

Arch Linux的git软件包包括git-credential-libsecret(替代了git-credential-gnome-keyring),因此您可以运行:

git config --global credential.helper libsecret

除了输入密码或个人令牌之外,您还可以尝试git-credential-oauth

不再需要密码!不再需要个人访问令牌!不再需要SSH密钥!

Git凭据助手使用OAuth安全地验证到GitHub、GitLab、BitBucket和其他锻造厂。

第一次推送时,助手将打开一个浏览器窗口进行身份验证。存储生命周期内的后续推送不需要任何交互。

这与任何存储助手兼容,包括git-credential-cache或git-credential-libsecret。


请不要在多个问题中发布相同的答案。相反,请根据所提出的问题调整答案。如果这些问题完全相同,请投票/标记关闭。 - Samuel Liew
1
git-credential-oauth的指针是目前大多数用户最简单的解决方案。 - BobHy

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接