Photo by Roman Synkevych on Unsplash
How to resolve Github Authentication failure on command line
GitHub authentication failed command line
You have been using Git and GitHub for a while now, and everything has been all cozy and good so far.
Using the git add
, git commit
, and git push
commands never gave you issues. However, you suddenly start getting a prompt to input your username and password whenever you try to git push
.
Okay! No problem! You proceeded to input your correct username and password, but you got served an error;
fatal: Authentication failed
What could be the cause of this error and how do I fix it? When two-factor authentication is enabled on your Github, one of the consequences involves not being able to push your code via HTTPS using your password. A second reason for this error is the fact that support for password authentication has been removed by Github officially. Hence, you have to generate a PAT (Personal Access Token) from your Github settings. Don't worry! It's certainly not that hard.
To solve this issue yourself, of course, you would need to have Git installed on your computer.
At the end of this article, you should have learned how to solve authentication failure while trying to access your repo from the command line.
Let's walk through the whole process:
I have staged my files using git add .
.
Secondly, I committed them with a simple message using git commit -m "hello world"
.
Then proceeded to push my code to my remote repository, after typing `git push origin master, it results in a 'username' and 'password' prompt. In this case, entering my correct username and password would generate an error:
The solution here is to generate a Personal Access Token
Creating the token
- Ensure your email has been verified, if your email has not been verified, see how to go about it here
- At the upper-right corner of your Github page, click on your profile picture, then click on Settings
3. Then click on Developer settings
4. Then on the left sidebar, click on Personal Access Token
5.
Generate a Personal Access Token
Afterward, you might be required to input your password before you generate the token. Fill in a descriptive name for your token then select the expiration date for the token.
Important: Ensure you grant the token permission to the repo, else it would not be useful in this use case!
After the token is generated, make sure to copy it because it disappears once you leave the page!
Now back to the command line
Enter the git push
command again which prompts the username and password. All you have to do is simply replace the password with the new token you just generated.
$ git clone https://github.com/username/repo.git
Username: your_username
Password: your_token
These steps solve the problem. Easy right? It is also noteworthy to state that Github officially removed support for password authentication in August 2021, passwords are found to be easier to crack compared to tokens. Consequently, tokens are now mandatory when using HTTPS on the command line!
In addition, to avoid being prompted all the time, you may need to run the code below until you are no longer prompted.
git remote set-url origin https://<githubtoken>@github.com/<username>/<repositoryname>.git
Alternative Option
There's a chance you might not like the idea of generating or using Personal Access Tokens and you prefer another option.
You can switch from HTTPS to SSH (Secure Shell) instead. The SSH protocol allows you to connect and authenticate to Github without necessarily supplying your username and personal access token at every visit. Cool right?
When setting up SSH, you would need to generate a new SSH key and add it to the SSH agent.
However, you must add the SSH key to your Github account to enable you to use it for authentication. You could also use a passphrase to refer to your key after adding it to the SSH agent, thereby, guaranteeing extra security.
I hope you found this article helpful. For additions and inputs, please let me know in the comments below.
Follow me on twitter.
Happy coding!