我使用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开头。。。

其他回答

我认为您可能有错误的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/*

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

根据您的配置,有不同类型的身份验证。以下是一些:

git凭证osxkeychain。如果您的凭据无效,请通过以下方式删除它:git凭据osxkeychain擦除或:printf“protocol=https\nhost=github.com\n”| git凭据osxkeychain擦除因此Git不会再向您请求密钥链权限。然后再次配置。请参阅:在GitHub上从OS X密钥链更新凭据您的SSH RSA密钥。为此,您需要将SSH密钥与添加的密钥进行比较,通过SSH-add-L/sh-add-L检查您是否使用了正确的身份。您的HTTPS身份验证(如果您使用的是HTTPS而不是ssh协议)。使用~/.netrc(Windows上的%HOME%/_netrc)提供凭据,例如。机器存储1.mycompany.com登录myusername密码mypassword

了解更多:在堆栈溢出时与GitHub同步。

如上所述,更改

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添加到配置文件中。

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

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

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