无法向任何Github仓库推送代码

4

非常奇怪,但我无法向Github提交任何存储库(包括新的存储库)。

我已经将我的SSH密钥上传到Github。

demas@demas-home:~/dotfiles$ ssh -T git@github.com
Warning: Permanently added the RSA host key for IP address '192.30.252.130' to the list of known hosts.

嗨,德马斯!您已成功进行身份验证,但GitHub不提供shell访问。

现在我创建了一个新的存储库并尝试将第一个提交推送到GitHub:

touch README.md
git init
git add README.md
git commit -m "first commit"
git remote add origin https://github.com/demas/netrunner.git
git push -u origin master

但是GitHub要求输入我的用户名和密码。为什么?

即使我输入了我的用户名和密码,我还是会收到错误提示:

demas@demas-home:~/netrunner$ git push -u origin master
Username for 'https://github.com': demas
Password for 'https://demas@github.com': 
remote: Invalid username or password.
fatal: Authentication failed for 'https://github.com/demas/netrunner.git/'

如果有必要,我会使用双重身份验证。


2
你输入了密码的2FA代码吗?请参见https://help.github.com/articles/providing-your-2fa-security-code底部。 - Nevik Rehnel
谢谢,这很有帮助。我需要将个人访问令牌(https://github.com/settings/applications)输入作为密码。或者我可以使用SSH URL代替HTTPS。 - ceth
2个回答

5

问题出在这一行。

git remote add origin https://github.com/demas/netrunner.git

当 Git 尝试通过 HTTP 协议访问服务器时,它不会使用您的 SSH 密钥(根据我的了解,在这种情况下使用基本的 HTTP 认证)。请使用 SSH 访问(URL 看起来像 git@github.com:username/reponame.git)。


1
最简单的解决方法是使用ssh url克隆您的git仓库到一个新目录中,并在那里工作。

GitHub的HTTPS是读写的,如果您对repo有推送权限。问题在于a)OP正在使用2fa,因此他需要输入令牌而不是密码;b)他想使用SSH,但却使用了HTTPS URL。 - Nevik Rehnel

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