DEV Community

UponTheSky
UponTheSky

Posted on • Updated on

[Git] How to use your personal access token

Background: Github Authentication Problem

It seems that from 2022, Github forces you to use your personal access token instead of passwords. I was, as usual, trying to push my commit and it was denied.

Authentication failed.
Enter fullscreen mode Exit fullscreen mode

So I had to generate my own personal access token, and it worked as expected. But the process is a bit complicated, and I was really struggling to find this method when I first hit this problem. So I would like to share my way of resolving this hassle matter.

It was originally one of my posts in Medium which I don't use now.

Original Post from Medium

Background

I use two different Github accounts - one is for my work, and the other for my personal projects. However, as a real novice in this field, whenever I switch the account from one to another, I had this problem with private remote repositories:

fatal: remote origin already exists.
Enter fullscreen mode Exit fullscreen mode

Then how do we get around this problem? Of course, there are myriads of questions and answers regarding this, including https://stackoverflow.com/questions/2505096/cloning-a-private-github-repo (probably the very first one you would hit when googling).

However, I could not really find an answer, especially the one with

https://<username>:<password>@github.com/<owner>/<repo>.git
Enter fullscreen mode Exit fullscreen mode

since my password contains '@' that the git parses before the one at @github , therefore the command does not work at all. Also, I even don't use SSH at all, and such answers really got me confused.

I finally have found an easy answer(at least in my opinion), and I might have seen this from somewhere but I could not find it again(so if there is anyone who knows links to those solutions, please let me know!).

My Method

First, you need a token for getting access to your private repo.
See the link

Second, go to your local repository and check the remotes.
git remote -v will show you the list of the repository's remotes(a simple one would probably only have origin ). Then delete the one you want to get access by the command:

git remote remove <remote_name>
Enter fullscreen mode Exit fullscreen mode

Third, reset your remote.
Enter the following command:

git remote add <remote_name>  https://<user_name>:<token>@github.com/<owner>/<repo>.git
Enter fullscreen mode Exit fullscreen mode

Now everything would be okay! If you are still stuck, please let me know!

Additionally

As I read it once again, I think you don't have to remove and add separately. Just do the following:

git remote set-url <remote_name> https://<user_name>:<token>@github.com/<owner>/<repo>.git
Enter fullscreen mode Exit fullscreen mode

Now you have done it in a single command.

Top comments (1)

Collapse
 
uponthesky profile image
UponTheSky

p.s.
Cloning a private repository is just the same:

git clone https://<user_name>:<token>@github.com/<owner>/<repo>.git
Enter fullscreen mode Exit fullscreen mode