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

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

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

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


当前回答

正如其他人所说,您可以安装密码缓存助手。我主要只是想发布其他平台的链接,而不仅仅是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)

其他回答

在我的情况下,当我尝试选择如下URL时,总是会收到密码提示:

git fetch http://username@gerrit-domainname/repositoryname refs/changes/1/12345/1 && git cherry-pick FETCH_HEAD

当cherry在另一台机器上选择时,这个URL工作得很好。然而,当我尝试使用正确的密码进行cherrypick时abc@12345我过去常犯以下错误:

remote: Unauthorized
remote: Authentication failed for http://username@gerrit-domainname  

在我的git配置文件中,远程URL如下所示:

url = ssh://gerrit-domainname:8080/wnc

解决方案:我通过提供HTTP密码解决了身份验证失败问题,该密码位于我的gerrit帐户->设置->HTTP密码。

HTTP密码类似于Th+IsAduMMy+PaSS+WordT+RY+Your+OwNPaSs,这与我的实际密码有很大不同abc@12345

注意:我的cherrpick URL以gitfetch开头。。。因此,此解决方案可能适用于git签出/下载,其中URL以git fetch开头。。。

步骤1:检查当前配置

cat .git/config

您将获得:

[core]
    repositoryformatversion = 0
    filemode = true
    bare = false
    logallrefupdates = true
    ignorecase = true
    precomposeunicode = true
[remote "origin"]
    url = https://github.com/path_to_your_git.git
    fetch = +refs/heads/*:refs/remotes/origin/*
[user]
    name = your_username
    email = your_email
[branch "master-staging"]
    remote = origin
    merge = refs/heads/master-staging

步骤2:删除远程源

git remote rm origin

步骤3:使用用户名和密码添加远程源

git remote add origin https://your_git_username:your_git_password@github.com/path_to_your_git.git

使用此命令:用适当的主机名替换github.com

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

在windows机器中:安装GitHub CLI

choco install gh

然后运行命令:

gh auth login

并遵循说明

您也可以点击此链接:

https://cli.github.com/manual/gh_auth_login

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配置的部分,那没问题;它会奏效的。