我使用Git已经有一段时间了,但不断的密码请求开始让我感到很烦。

我使用的是Mac OS X和GitHub,我按照GitHub的设置Git页面的指示设置Git和SSH密钥。

我还将github SSH密钥添加到了我的Mac OS X密钥链中,正如github的SSH密钥密码页面中提到的那样。我的公钥已在Git中注册。

然而,每次我尝试Git拉时,我都必须输入用户名和密码。除了SSH密钥之外,我是否需要为此设置其他东西?


当前回答

orkoden关于在终端中使用带有Git的密钥链的回答不完整,并引发了错误。这是您必须执行的操作,以保存在钥匙链中的终端中输入的用户名和密码:

curl http://github-media-downloads.s3.amazonaws.com/osx/git-credential-osxkeychain -o git-credential-osxkeychain
sudo mv git-credential-osxkeychain /usr/local/bin
sudo chmod u+x /usr/local/bin/git-credential-osxkeychain

然后输入

git config --global credential.helper osxkeychain

如果你已经在curl之前完成了Git配置的部分,那没问题;它会奏效的。

其他回答

orkoden关于在终端中使用带有Git的密钥链的回答不完整,并引发了错误。这是您必须执行的操作,以保存在钥匙链中的终端中输入的用户名和密码:

curl http://github-media-downloads.s3.amazonaws.com/osx/git-credential-osxkeychain -o git-credential-osxkeychain
sudo mv git-credential-osxkeychain /usr/local/bin
sudo chmod u+x /usr/local/bin/git-credential-osxkeychain

然后输入

git config --global credential.helper osxkeychain

如果你已经在curl之前完成了Git配置的部分,那没问题;它会奏效的。

如果你想阻止Git总是要求你提供GitHub存储库的登录凭据,这很容易做到。

使用SSH而不是HTTPS

您可以使用SSH而不是HTTPS更新源远程“

git remote set-url origin git@github.com:username/your-repo.git

配置Git以存储密码和用户名

下面是如何让Git存储用户名和密码:

git config --global credential.helper store

接下来,保存会话的用户名和密码:

git config --global credential.helper cache

如果设置了SSH代理,还可以将其添加到~/.gitconfig中,以强制git对所有GitHub repo使用SSH,而不是HTTPS:

[url "ssh://git@github.com/"]
    insteadOf = git://github.com/
    insteadOf = https://github.com/

(如果你主要使用公共回购,你也可以使用pushInsteadOf而不是insteadOf,因为从公共回购中读取可以在没有身份验证的情况下完成)。

我觉得static_rtti提供的答案在某种意义上很粗糙。我不知道这是否在早期可用,但Git工具现在提供了凭据存储。

缓存模式

$ git config --global credential.helper cache

使用“缓存”模式将凭据保存在内存中一段时间。没有一个密码存储在磁盘上,15分钟后将从缓存中清除。

存储模式

$ git config --global credential.helper 'store --file ~/.my-credentials'

使用“存储”模式将凭据保存到磁盘上的纯文本文件中,它们永远不会过期。

我个人使用的是商店模式。我删除了存储库,克隆了它,然后必须输入一次凭据。

参考:7.14 Git工具-凭证存储

正如其他人所说,您可以安装密码缓存助手。我主要只是想发布其他平台的链接,而不仅仅是Mac。我正在运行Linux服务器,这很有用:在Git中缓存GitHub密码

对于Mac:

git credential-osxkeychain

窗户:

git config --global credential.helper wincred

Linux:

git config --global credential.helper cache
git config --global credential.helper 'cache --timeout=3600'
# Set the cache to timeout after 1 hour (setting is in seconds)