thumb

After enabling a two-factor authentication in my GitHub account, when I run the Git git push command it throws the following error message:

$ git push
Username for 'https://github.com': Username
Password for 'https://Username@github.com': 
remote: Invalid username or password.
fatal: Authentication failed for 'https://github.com/username/repository.git/'

What causes this error

This error message is self explanatory. It means that we are trying to use the invalid username or password. But I’m sure I’m using the correct username and password. In my case, this happened with me after I turned on a two-factor authentication (2FA) in my GitHub account. So I know what caused this error message.

How to solve it

To solve this problem all we need to do is create a GitHub personal access token and use it instead of our GitHub password and a two-factor authentication code.

Creating a GitHub personal access token.


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.

Give your token a descriptive name.

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 to your clipboard. For security reasons, after you navigate off the page, you will not be able to see the token again.


Note! Treat your tokens like passwords and keep them secret (if you don’t want other people to use the API on your behalf). When working with the API, use tokens as environment variables instead of hardcoding them into your programs.

Using a token on the command line.


Once we have a token, we can enter it instead of our password when performing Git operations over HTTPS. Just inter your token after prompted a password and then watch the magic happen…

git push
Username: your_username
Password: your_token

Note! 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.

Note! If you are not prompted for your username and password, your credentials may be cached on your computer. You can update your credentials in the Keychain to replace your old password with the token.


If this article helped you solve the problem then please leave a comment. :smiley:

Thanks for reading!



Arthur Gareginyan

Arthur Gareginyan

Arthur is a designer and full stack software engineer. He is the founder of the studio SpaceX-Chimp and the blog MyCyberUniverse.com. His personal website can be found at arthurgareginyan.com.

Follow Arthur: