Git一直提示我输入密码

825

我已经使用Git有一段时间了,但是不停地要求输入密码让我感到非常烦恼。

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

我还像GitHub的SSH key passphrases页面上提到的那样,将github SSH 密钥添加到了我的Mac OS X密钥链中。我的公钥已在Git中注册。

尽管如此,每次我尝试Git pull时,都需要输入我的用户名和密码。除了SSH密钥之外,还需要设置其他内容吗?


1
有点儿傻的问题,但是你验证过使用ssh连接到git机器时SSH密钥是否有效吗? - Kurt Stutsman
4
你是说类似于 ssh -T git@github.com 这样的东西吗?没错,这个方案完全可行(虽然有点慢)。 - Catherine
2
请参阅Git push需要用户名和密码 - user456814
对于https url,你可以使用(git1.8.3+)一个git凭证助手“netrc”。在这里查看完整示例 - VonC
1
我是一名Windows用户,即使将我的公钥添加到服务器的authorized_keys文件中,我仍然遇到了密码请求问题。实际上问题在于我没有将我的公/私密钥保存在c:\program files\git文件夹下的.ssh文件夹中。如果有人面临此类问题,请将您的密钥复制到此文件夹并尝试推送/拉取操作。 - Raja Amer Khan
显示剩余6条评论
32个回答

2

我同意"codehugger"的观点,并使用"orkoden"的指示为我解决了问题,在NetBeans 7.3中,当你右键单击文件并选择上下文菜单——推送——一个“推送到远程”窗口会打开——这里有两个选项:

  1. origin:https://github.com/myaccount/myproject.git/

  2. https://github.com/myaccount/myproject.git/

正如你所看到的,URL中的原点参数是不同的。你不想选择选项(1),你只需要检查选项(2),这对我来说完全没问题。


2
这是解决此问题的方法:
git remote -v
这将显示 origin 的 URL。如果它以 https 开头,我们需要删除此 URL 并替换为 ssh 的 URL。
git remote remove origin
git remote add origin git@github.com:PrestaShop/PrestaShop.git

在你移除和重新添加远程仓库后,如果你仍然无法将本地分支推送到GitHub,则下面列出的命令应该可以帮助解决此问题。
xpetta@host:~/home/xpetta$ git push
fatal: The current branch ABC-123 has no upstream branch.
To push the current branch and set the remote as upstream, use

    git push --set-upstream origin ABC-123

或者您可以打开.git/config文件,找到[remote "origin"]部分,并确保正在使用的URL不以https开头。如果以https开头,请将其替换为远程GitHub存储库中的ssh URL。同样适用于.git/config中列出的其他服务,如fetch等,需要替换为ssh URL。


1

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

  1. git credential-osxkeychain.

    If your credential is invalid, remove it by:

      git credential-osxkeychain erase
    

    or:

      printf "protocol=https\nhost=github.com\n" | git credential-osxkeychain erase
    

    So Git won't ask you for the keychain permission again. Then configure it again.

    See: Updating credentials from the OS X Keychain at GitHub

  2. Your SSH RSA key.

    For this, you need to compare your SSH key with what you've added, check by ssh-add -L/ssh-add -l if you're using the right identity.

  3. Your HTTPS authentication (if you're using https instead of ssh protocol).

    Use ~/.netrc (%HOME%/_netrc on Windows), to provide your credentials, e.g.

      machine stash1.mycompany.com
      login myusername
      password mypassword
    

了解更多:在 Stack Overflow 中 与 GitHub 同步


1

在你使用GitHub之前,按照教程中的步骤测试你的SSH连接进行操作:

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

1

1
如果您已经设置了SSH代理,您还可以将以下内容添加到您的~/.gitconfig中,以强制git在所有GitHub存储库中使用SSH而不是HTTPS:
[url "ssh://git@github.com/"]
    insteadOf = git://github.com/
    insteadOf = https://github.com/

如果您主要使用公共仓库,也可以使用pushInsteadOf而不是insteadOf,因为从公共仓库读取可以在无需身份验证的情况下完成。


0
在我的情况下,当我尝试像下面这样的挑选一个URL时,我总是收到一个密码提示:
git fetch http://username@gerrit-domainname/repositoryname refs/changes/1/12345/1 && git cherry-pick FETCH_HEAD

这个URL在另一台机器上挑选时效果很好。然而,在我的机器上,当我尝试使用正确的密码abc@12345进行挑选时,我会收到以下错误:

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

在我的 git 配置文件 中,远程 URL 如下:
url = ssh://gerrit-domainname:8080/wnc

解决方案:
我通过提供在我的Gerrit账户-设置-HTTP密码中找到的HTTP密码来解决身份验证失败的问题。

HTTP密码类似于Th+IsAduMMy+PaSS+WordT+RY+Your+OwNPaSs,与我的实际密码abc@12345完全不同。

注意:我的cherrpick URL以git fetch ...开头。因此,此解决方案可能适用于URL以git fetch ...开头的git checkout/download。


0

遇到了相同的问题。

请确保您已正确设置远程源:

$git remote add origin master "https://...git " 

0

0

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

第二步:移除您的远程源

git remote rm origin

第三步:使用您的用户名和密码添加远程源。
git remote add origin https://your_git_username:your_git_password@github.com/path_to_your_git.git

在重新添加远程源之后,您还需要对齐分支的远程和本地版本 - 类似于这样: git branch --set-upstream master origin/master - bownie

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接