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

时间:2014-08-28 13:48:37

标签: git github two-factor-authentication

我刚启用2FA(我想不出我做的任何其他更改),git要求我输入用户名和密码。我提供了两者,但他们“错了”。我在这里尝试了很多解决方案:Git push requires username and password但这不起作用。特别是,当从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/'

任何提示?

10 个答案:

答案 0 :(得分:84)

您需要生成访问令牌。您可以转到settings page创建一个。

enter image description here

在命令行中使用此访问令牌作为您的密码。

答案 1 :(得分:25)

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

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

    enter image description here

    在命令行中使用此访问令牌作为您的密码。

  2. 您可以通过将用户名包含在项目远程网址中来保留用户名。其中一种方法是修改.git/config以将url行修改为以下格式:

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

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

    $ git config credential.helper store

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

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

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

答案 2 :(得分:13)

我有类似的问题。我不得不改变git命令中使用的url来包含我的用户名。

git push https://YOUR_USERNAME_HERE@github.com/mlbileschi/scala.git

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

答案 3 :(得分:6)

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

1)生成新的SSH密钥(source

打开终端。

粘贴下面的文字,替换为您的GitHub电子邮件地址。

ssh-keygen -t rsa -b 4096 -C "your_email@example.com"

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

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

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

cat ~/.ssh/id_rsa.pub

应该输出某些内容

ssh-rsa AAAAB3NzaC1y ... mKAKw== your_email@example.com

导航至https://github.com/settings/keys并点击New SSH Key,为其添加标题并复制粘贴公钥。

enter image description here

3)将git来源从https://更改为ssh

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

git remote set-url origin git@github.com:<github username>/<repository name>

答案 4 :(得分:3)

2021 年更新

不确定这是否适用于所有人,但将我的 git version2.27.0 更新到最新版本(当前为 2.30.0)已解决我的问题,而尝试在命令行中使用个人访问令牌作为密码却没有。

更新后,在尝试推送时,系统提示我通过浏览器登录 GitHub,而不是在对话框或命令行中输入我的凭据。

答案 5 :(得分:0)

这对我有用:

  • 转到[your-git-repo] /。git / config

  • [remote "origin"]下,将URL键从http协议更改为git。

示例

如果url的值为https://github.com/。git将其更改为git@github.com:<repo-url>.git

答案 6 :(得分:0)

如果您已经在使用ssh密钥,则启用2FA后,它将强制您使用SSH进行远程读写。您真的不需要添加个人令牌,而只需继续使用现有的SSH密钥对即可。

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

git remote set-url origin git@github.com:<github-username>/<repo-name>

答案 7 :(得分:0)

在Github上启用2FA后,这对我有用:

  1. 创建个人访问令牌:complete one
    注意:请记住为令牌选择正确的权限。

  2. 运行:
    git clone https://github.com/username/repo.git
    用户名:your_username
    密码:your_token

进一步阅读:https://help.github.com/en/github/authenticating-to-github/creating-a-personal-access-token-for-the-command-line

答案 8 :(得分:0)

当我为我的一个私人仓库启用2FA(两因素身份验证)时,我在现有仓库上遇到了这个问题。 我可以在ubuntu 19.0终端上按照以下步骤解决问题:-

  1. 将ssh密钥添加到 github ,以便您无需再次使用密码,因为现在您已启用2FA。Visit github page知道如何轻松地进行操作。
  2. 添加密钥后,转到终端,然后更新原始URL

    git remote set-url origin git@github.com:<USERNAME>/<BRANCH-NAME>

就这样。希望对您有帮助

答案 9 :(得分:0)

切换到旧笔记本电脑时遇到了这个问题。真正的问题只是我正在运行旧版本的git。使用新的跨平台凭据管理器将我更新到最新版本的git后,便可以使用2FA完美地为我签名(看起来它会自动为您创建PAT)。

相关问题