Authentication issue
After I enabled the Two-factor Authentication in GitHub, I can’t push the local commits to GitHub repository, and encountered the issue as follows:
How do I Resolve it
Why this issue happened after Two-factor Authentication has been enabled? I got the answer from GitHub Documentation.
Here, I just take notes for the solution. GitHub requires users to create a personal access token to replace the password when performing Git operations over HTTPS with Git on the CLI.
A personal access token is required to authenticate to GitHub in the following situations:
- When you’re using [two-factor authentication](two-factor authentication)
- To access protected content in an organization that uses SAML single sign-on (SSO). Tokens used with organizations that use SAML SSO must be authorized.
Create a Token
-
Log in to GitHub, in the upper-right corner of any page, click your profile photo, then click Settings.
-
In the left sidebar, click Developer settings.
-
In the left sidebar, click Personal access tokens.
-
Click Generate new token.
-
Name it, then select the scopes, or permissions, you’d like to grant this token. To use your token to access repositories from the command line, select repo.
-
Click Generate token.
-
Copy the token. Then you can enter it instead of your password when performing Git operations over HTTPS.
$ git clone https://github.com/username/repo.git
Username: your_username
Password: your_token # Replace your initial password)
It works!
Tip: Personal access tokens can only be used for HTTPS Git operations. If your repository uses an SSH remote URL, you will need to [switch the remote from SSH to HTTPS](switch the remote from SSH to HTTPS).