启用2FA后,Git身份验证失败

Max*_*chi 77 git github two-factor-authentication

我刚刚启用2FA(我想不出我做的任何其他更改)并且git要求我输入用户名和密码.我提供了两者,但他们"错了".我在这里尝试了很多解决方案:Git push需要用户名和密码,但这不起作用.特别是,当从https切换到ssh时,ssh键会给出

权限被拒绝(publickey).致命:无法从远程存储库读取.

$ git push
warning: push.default is unset; its implicit value is changing in
Git 2.0 from 'matching' to 'simple'. To squelch this message
and maintain the current behavior after the default changes, use:

  git config --global push.default matching

To squelch this message and adopt the new behavior now, use:

  git config --global push.default simple

See 'git help config' and search for 'push.default' for further information.
(the 'simple' mode was introduced in Git 1.7.11. Use the similar mode
'current' instead of 'simple' if you sometimes use older versions of Git)

Username for 'https://github.com': **********
Password for 'https://mlbileschi@github.com': 
remote: Invalid username or password.
fatal: Authentication failed for 'https://github.com/mlbileschi/scala.git/'
Run Code Online (Sandbox Code Playgroud)

有小费吗?

Ger*_*osi 96

您需要生成访问令牌.您可以转到设置页面创建一个.

在此输入图像描述

在命令行中使用此访问令牌作为密码.


Ray*_*Luo 30

端到端解决方案需要3个步骤.

  1. 感谢Gergo Erdosi.他的答案基本上是正确的,只是Github更改了设置页面.截至2016年底,您需要从个人访问令牌页面生成访问令牌.

    在此输入图像描述

    在命令行中使用此访问令牌作为密码.

  2. 您可以通过将用户名包含在项目远程URL中来保留用户名.其中一种方法是编辑您.git/configurl行,将其修改为以下格式:

    url = https://YOUR_USERNAME_HERE@github.com/owner/repo.git

  3. 您只需运行一次即可保留密码:

    $ git config credential.helper store

    然后你的未来git密码将使用格式以明文形式存储在〜/ .git-credentials中https://user:PlaintextPassword@example.com.

    以明文存储密码通常被视为安全风险.但在这种2FA情况下,凭证不是您的真实密码,而是随机生成的字符串.因此,与使用ssh私钥无密码短语ssh私钥一样安全.CAVEAT:请记住,如果你碰巧在这台机器上也使用了另一个没有2FA的git帐户,那些真实的密码也将以明文形式存储.

PS:或者,您可以选择使用基于ssh的登录,使用受密码保护的ssh私钥,这样会更安全,更不方便,但它超出了本答案的范围.


小智 13

我遇到了类似的问题.我不得不改变git命令中使用的url来包含我的用户名.

git push https://YOUR_USERNAME_HERE@github.com/mlbileschi/scala.git
Run Code Online (Sandbox Code Playgroud)

然后,当它要求PW时,请按照Gergo Erdosi的回答中的说明使用您创建的访问令牌.


Jos*_*ush 7

在linux上,您可以使用SSH密钥验证您的GitHub身份.

1)生成新的SSH密钥()

打开终端.

粘贴下面的文本,替换您的GitHub电子邮件地址.

ssh-keygen -t rsa -b 4096 -C "your_email@example.com"
Run Code Online (Sandbox Code Playgroud)

这将使用提供的电子邮件作为标签创建一个新的ssh密钥.


2)将密钥链接到您的GitHub帐户

打开终端并复制生成的公钥

cat ~/.ssh/id_rsa.pub
Run Code Online (Sandbox Code Playgroud)

应输出的东西为

ssh-rsa AAAAB3NzaC1y ... mKAKw== your_email@example.com
Run Code Online (Sandbox Code Playgroud)

导航到https://github.com/settings/keys并单击New SSH Key,为其指定标题并复制粘贴公钥.

在此输入图像描述


3)将git起源https://改为ssh

打开终端,cd到您的存储库位置并输入

git remote set-url origin git@github.com:<github username>/<repository name>
Run Code Online (Sandbox Code Playgroud)


Sak*_*oor 6

如果您已经在使用 ssh 密钥,则在启用 2FA 后,它将强制您使用 SSH 远程读/写。您实际上并不需要添加个人令牌,而是继续使用现有的 SSH 密钥对。

只需将您的远程 URL 从 HTTPS 更改为 SSH:

git remote set-url origin git@github.com:<github-username>/<repo-name>
Run Code Online (Sandbox Code Playgroud)