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.
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.
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
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>
Third, reset your remote.
Enter the following command:
git remote add <remote_name> https://<user_name>:<token>@github.com/<owner>/<repo>.git
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
Now you have done it in a single command.
Top comments (1)
p.s.
Cloning a private repository is just the same: