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

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

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

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


当前回答

使用以下命令增加超时时间,以便您可以暂时重新键入密码

git config --global credential.helper 'cache --timeout 3600'

我在Bitbucket和GitHub上使用了它,这两个都适用。你唯一需要做的就是3600秒。增加到你想要的程度。我把它改成了259200,大约30天。这样,我每隔30天左右重新输入一次密码。

其他回答

Microsoft Stack解决方案(Windows和Azure DevOps)

首先打开.git/config文件,确保地址如下:

protocol://something@url

例如,Azure DevOps的.git/config:

[remote "origin"]
    url = https://mystore@dev.azure.com/mystore/myproject/
    fetch = +refs/heads/*:refs/remotes/origin/*

如果问题仍然存在,请打开Windows凭据管理器,单击名为Windows凭据的安全框并删除所有与git相关的凭据。

下次登录git时,它不会再消失。

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

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

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

在GitHub中使用密钥之前,请按照教程中的步骤测试SSH连接:

$ ssh -T git@github.com
# Attempts to ssh to GitHub

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存储库URL。

打开.git/config并找到[remote“origin”]部分。确保您使用的是SSH:

ssh://git@github.com/username/repo.git

如果单击克隆或下载并选择SSH,您可以在存储库的主页面中看到SSH URL。

而不是https或git:

https://github.com/username/repo.git
git://github.com/username/repo.git

现在,您可以只使用SSH密钥而不是用户名和密码进行验证。

如果Git抱怨已经添加了'origin',请打开.config文件,将[remote origin]后面的url=“…”部分编辑为url=ssh://github/username/repo.git


其他服务也是如此。确保地址如下:protocol://something@网址

例如,Azure DevOps的.git/config:

[remote "origin"]
    url = https://mystore@dev.azure.com/mystore/myproject/
    fetch = +refs/heads/*:refs/remotes/origin/*