git clone private repository — An easy solution

kyrie-eleison
2 min readJul 4, 2021

--

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!).

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

See the link: https://docs.github.com/en/github/authenticating-to-github/keeping-your-account-and-data-secure/creating-a-personal-access-token

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 commandgit 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!

--

--

kyrie-eleison
kyrie-eleison

No responses yet