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

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

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

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


当前回答

在我的情况下,当我尝试选择如下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开头。。。

其他回答

如上所述,更改

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

to

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

我自己将.git/config文件中的https更改为ssh,但仍然无法工作。然后我看到你必须将github.com更改为git@github.com.获取实际正确URL的一个好方法是转到您的项目页面并单击此项:

将HTTPS更改为SSH以获得正确的URL

然后将此URL添加到配置文件中。

如果您使用https协议,Git将提示您输入密码。如果您使用ssh,它将使用您的私钥进行身份验证,而不是询问密码。

以下是解决此问题的方法:

git远程-v

这将显示来源的url。您将注意到此url中的https。(此处示例)

现在,您必须先删除这个,然后使用ssh添加url

git remote remove origin

git remote add origin git@github.com:PrestaShop/PrestaShop.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

步骤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

在Windows for Git 1.7.9+中,在命令提示符下运行以下命令以在文本编辑器中打开配置文件:

git config --global --edit

然后在文件中,添加以下块(如果不存在)或相应地进行编辑:

[credential "https://giturl.com"]
username = <user id>
helper = wincred

保存并关闭文件。在上述更改后,您只需提供一次凭据。