Few days ago, I was having a good time writing some TypeScript codes for my new project. I would switch to different git branches, commit code, and all that stuff. Everything seems to have been going alright until I was ready to push to github.
So I add my remote repository with;
git remote add origin http://github.com/nkemcels/screentime.git
And then
git push -u origin master
As usual, Git asks for my username and password and I give it to them, but out of the blue, this happens:
remote: Support for password authentication was removed on August 13, 2021. Please use a personal access token instead. remote: Please see https://github.blog/2020-12-15-token-authentication-requirements-for-git-operations/ for more information. fatal: unable to access "..." : The requested URL returned error: 403
My happy mood suddenly changes to a stackoverflow mood. After lots of research, it turns out that github had deprecated password authentication in favor of using Personal Access Tokens (PAT) for security reasons.
In this post, I will share with you two ways you can authenticate to your remote repositories the new way and push your codes to, cloning or pulling from private repositories.
Before we start with any of the two approaches, we'll need to generate a github Personal Access Token.
Generating a Personal Access Token (PAT)
I have written a more detailed post on how to generate a personal access token here
Please read through if you are not yet familiar on how to generate one.
METHOD 1:
With your new PAT, head over to your waiting cli and change/set your git remote as
git remote set-url origin https://<PAT_here>@github.com/<username>/<repo>
An actual example of the command above would look like this
git remote set-url origin https://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@github.com/nkemcels/screentime.git
That is it my friend. You should be able to push to and pull from your repository without any hassle.
Cloning a private repository using this method is similar
git clone https://<PAT_here>@github.com/<username>/<repo>
This approach is the easiest way of authenticating to your remote repositories. However, you'll have to use your PAT directly on every repository you create. Imagine you have a lot of repositories configured this way and one day you decide to update your PAT. You'll have to start moving from one repository to the next, updating remote URLs to match the new PAT. Maybe this might be a great exercise for you, but as for me, I wont find it funny.
Luckily, there is a second and a more elegant way (IMO) to do this which we shall be looking at in the second method.
METHOD 2:
This method is OS specific. So follow the instructions that matches your operating system.
For Windows:
- From the Control Panel, got to Credential Manager
- Select Windows Credentials
- Look for
git:https://github.com
, select it and click on Edit. - Update the password field with your Personal Access Token and you are good to go.
- If you could not find
git:https://github.com
in step 3; - Click on Add a generic credential
- Set the internet address to
git:https://github.com
- Type in your github username
- In place of password, enter your Personal Access Token.
- Click Ok and you are good to go.
For MacOs:
- Click on the Spotlight icon (magnifying glass) on the right side of the menu bar. Type Keychain access and press Enter.
- In the Keychain access app, search for
github.com
. - Find the "internet password" entry for
github.com
. - Edit the entry with your Personal Access token and you are good to go
Once you have setup your github credentials for your operating system, you do not need to add them anymore to your repository urls. All subsequent pull, push, clone commands to your remote repositories shall be automatically authenticated by your operating system. If you get a password prompt, it means the instructions were not properly followed.
Hope that helped. Till we meet again, adieu.