我有一个项目托管在GitHub上。我失败时,试图推动我的修改在主人。我总是得到以下错误消息

Password for 'https://git@github.com': 
remote: Invalid username or password.
fatal: Authentication failed for 'https://git@github.com/eurydyce/MDANSE.git/'

但是,设置我的ssh密钥到github似乎没问题。实际上,当我执行ssh -T git@github.com时,我得到

Hi eurydyce! You've successfully authenticated, but GitHub does not provide shell access.

这似乎表明,一切都是OK从这一边(eurydyce是我的github用户名)。我严格按照github上给出的说明和许多堆栈讨论的建议,但没有办法。你知道我可能做错了什么吗?


当前回答

启用双因素身份验证(2FA)后,当你尝试使用git clone、git fetch、git pull或git push时,你可能会看到类似这样的东西:

$ git push origin master
Username for 'https://github.com': your_user_name
Password for 'https://your_user_name@github.com': 
remote: Invalid username or password.
fatal: Authentication failed for 'https://github.com/your_user_name/repo_name.git/'

为什么会发生这种情况

从GitHub帮助文档:

After 2FA is enabled you will need to enter a personal access token instead of a 2FA code and your GitHub password. ... For example, when you access a repository using Git on the command line using commands like git clone, git fetch, git pull or git push with HTTPS URLs, you must provide your GitHub username and your personal access token when prompted for a username and password. The command line prompt won't specify that you should enter your personal access token when it asks for your password.

如何解决

生成一个个人访问令牌。(关于为命令行创建个人访问令牌的详细指南。) 复制个人访问令牌。 重新尝试您正在尝试的命令,并使用个人访问令牌代替您的密码。

相关问题: https://stackoverflow.com/a/21374369/101662

其他回答

我也有同样的问题。我通过在客户端应用程序的repo设置中将远程分支的路径从https://github.com/YourName/RepoName更改为git@github.com:YourName/RepoName.git来解决这个问题。

https://git@github.com/eurydyce/MDANSE.git不是ssh url,而是https url(这需要你的GitHub账户名,而不是'git')。

尝试使用ssh://git@github.com:eurydyce/MDANSE.git或直接使用git@github.com:eurydyce/MDANSE.git

git remote set-url origin git@github.com:eurydyce/MDANSE.git

OP Pellegrini Eric补充道:

这就是我在~/中所做的。当前包含以下条目的gitconfig文件[remote "origin"] url=git@github.com:eurydyce/MDANSE.git

它不应该在你的全局配置中(在~/中的那个)。 你可以在你的repo中检查git config -l: url应该在本地配置中声明:<yourrepo>/.git/config。

因此,在执行git remote set-url命令时,请确保您处于repo路径中。


正如Oliver的回答中提到的,如果激活了双因素身份验证(2FA), HTTPS URL将不使用用户名/密码。

在这种情况下,密码应该是PAT(个人访问令牌),见“在命令行上使用令牌”。

此限制仅适用于HTTPS url, SSH不受此限制。

我只是关闭双因素认证,然后再试一次。这对我很管用。

解决步骤:

控制面板 证书管理器 单击Windows凭证 在Generic Credential部分,将有git url,更新用户名和密码 重新启动Git Bash并尝试克隆

注意: 如果你在通用凭据部分没有找到git url,请遵循下面的答案 https://stackoverflow.com/a/55858690/7372432

当我遇到这个问题时,我所做的解决方法是从我的github仪表板生成新的令牌,并将以下代码粘贴到我的终端

$ git远程设置url来源https://your-github-username:your-github-token@github.com/your-github-username/your-github-repo.git