Git authentication fails after enabling 2FA

I just enabled 2FA (I can't think of any other changes I made) and git asked for my username and password. I provided both, but they were "wrong". I tried many of the solutions here: Git push requires username and password but that didn't work. In particular, when switching from https to ssh, the ssh key gives

Permission denied (publickey). fatal: Could not read from remote repository.

$ 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/'

Any tips?


You need to generate an access token. You can create one by going to your settings page.

在这里输入图像描述

Use this access token as your password in the command line.


An end-to-end solution takes 3 steps.

  • Kudos to Gergo Erdosi. His answer is largely right, it is just that Github changes that setting page. As of late 2016, you need to generate an access token from your Personal access tokens page.

    在这里输入图像描述

    Use this access token as your password in the command line.

  • You can persist your user name by including it into your project remote url. One of the way to do it is to edit your .git/config to modify the url line into the following format:

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

  • You can persist your password by run this for one time only:

    $ git config credential.helper store

    and then your future git password(s) will be stored in ~/.git-credentials, in plaintext, using the format https://user:PlaintextPassword@example.com .

    Storing password(s) in plaintext would normally be considered as a security risk. But in this 2FA case, the credential is NOT your real password, it is a randomly generated string. So it is as secure as using a ssh private key a passphrase-less ssh private key. CAVEAT: keep in mind that, if you happen to also use another git account(s) without 2FA on this machine, those real password(s) will also be stored in plaintext.

  • PS: Alternatively, you could choose to use ssh-based login, using a passphrase-protected ssh private key, which would be more secure and less convenient, but it is outside the scope of this answer.


    I had a similar problem. I had to alter the url used in the git command to include my username.

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

    Then when it asks for PW use the access token you created from following the instructions in Gergo Erdosi's answer.

    链接地址: http://www.djcxy.com/p/92216.html

    上一篇: Bitbucket无法在git pull上进行身份验证

    下一篇: 启用2FA后,Git认证失败