When you get the error fatal: remote origin already exists. after running this command inside a local Git repository:
git remote add origin https://github.com/profile-name/project-name.gitIt’s usually because you cloned a remote repository that already has a remote origin URL configured. But that’s not how Git works.
The quick fix is to replace add origin with set-url origin.
However you might want to learn something while you’re here, so read on.
In your Terminal, while inside the cloned project folder (local repository), run this command:
git remote --verboseThis will give you the origin URLs for fetch and push, for the repo you cloned.
Let’s say you just cloned the official create-react-app project from Facebook’s GitHub repository:
git clone https://github.com/facebook/create-react-app.gitThen in your Terminal, while inside your create-react-app directory, you run the git remote --verbose command while inside it, and get this output:
origin https://github.com/facebook/create-react-app.git (fetch)
origin https://github.com/facebook/create-react-app.git (push)This should be an AHA moment, look at the remote origin URLs, they point to Facebooks GitHub profile /facebook/.
But you want them to point to your GitHub profile /your-profile/.
You need to change the origin URLs, by using the aforementioned set-url command instead of add.
While inside your Terminal where you just got the fatal error, run this command (replace profile-name with your real GitHub profile name, and project-name with the name of your GitHub remote repository):
git remote set-url origin https://github.com/profile-name/project-nameIn my case, since my GitHub profile name is TechStacker and I created a remote GitHub repo also called create-react-app (the same as the cloned repo), my command looks like this:
git remote set-url origin https://github.com/TechStacker/create-react-appTo confirm, I’ll run the git remote --verbose command again. This time, both my origin URLs (fetch & push) point to my GitHub profile:
origin https://github.com/TechStacker/create-react-app (fetch)
origin https://github.com/TechStacker/create-react-app (push)The set-url command simply updates the origin URL of your repository, so it now points to your remote repository, instead of the profile where you cloned the project from.