You might have come across this error once or twice in your development journey with Visual Studio Code. In most cases, this error appears when you are about to push some code to your Github repository using the VS Code terminal.
You just run the ‘git push’ command and suddenly this weird error appears that says:
fatal: Authentication failed for 'https://github.com/abc/xyz.git/'
There’s no need to panic though. Some users have actually un-committed or un-staged there code changes after seeing this error which really makes no sense.
Most common reason
Usually, this error happens when you have recently updated or upgraded your VS code and then you made some changes to your code to push it to your Github repo. In that case, you simply need to re-connect your VS code with your Github account to restore the connection.
The Solution
Here I am going to take you step-by-step on how to fix this error in no time. Heck, I am even going to share some screenshots so that you don’t have to figure out anything by yourself.
Here’s how you can re-authenticate your VS code to your Github account:
- First step is to Login to your Github account.
- Then go to your Github settings.
- After that, you need to go to the ‘Developer Settings”.
- Then, you need to click on the ‘Tokens (classic)’ button. This will take you to the Tokens screen. This option will appear after expanding the ‘Personal Access Tokens’ button.
- Next, you need to click on the “Generate new token (classic)” option. This will take you to the Token generation screen.
- In the next step, you can setup a custom expiration time for your token. Just remember, when the token expires, you will have to repeat the same process to authenticate with Github.
- Now, just click on the green ‘Generate Token’ button at the bottom of the screen. Note: Make sure you only enable the Workflow setting.
- In the next screen, you need to copy the token so you can insert it into VS code. Please note that you will only be able to see this token once. If you don’t copy and and exit this screen, you will have to generate a new token.
- Now here’s the most important step. You need to insert this token in your VS code to authenticate it with Github. Open your VS code terminal and set the current directory to your Project root. For example:
cd C:\Users\test\source\repo\MyTestProject
- Then enter this command to set Remote access via your Github token:
git remote set-url origin https://username:token@github.com/username/repository.git
Replace the ‘username’ and ‘token’ with your Github username and personal access token that you copied. Also replace the repository name in the url. - After doing all this, you should be able to push your code to your Github Repo.
If it still doesn’t work, you might have to restart VS code and then try to push the commits again.