This article explains How to fix git fatal: remote origin already exists error during the push or add commands.
Fix git fatal: remote origin already exists error on push
You must run the ‘git init’ command to enable git for any application or folder/directory.
Once git is initialized, we must either create a new repository or clone an existing repository to map this to the existing directory.
if this directory creates in the nested folder of any git repository, the git command throws git fatal: remote origin already exists error.
What does its origin mean?
This error comes during multiple use cases.
- created a local nested folder app, It is a child folder a local git repository, these folders map to the new remote repository.
In any local repository, How do you know the remote repository url?
-v option gives the local name with remote url as described following
git remote -v
And the output is
origin https://github.com/intkiran/react-auth0-login.git (fetch) origin https://github.com/intkiran/react-auth0-login.git (push)
It gives the remote repository url mapped with the local name i.e origin.
Origin is the local name for the remote repository given by default.
This error occurs after adding a new url to the existing origin.
This post covers the solution for the following errors in the git command
- fatal: No such remote:
- Remote origin already exists on ‘git push’ to a new repository
There are many ways we can fix this.
Remove origin name from the given existing repository
First, delete the local name origin for a given remote repository using the following command.
git remote rm origin
next, add the original name to the new remote repository
git remote add origin remote-repo-url
set-url Change origin name from the given existing repository
git remote set-url neworigin remote-repo-url
if remote-repo-url does not exist, it gives an error
fatal: No such remote:.
Replace current origin to new url
below set the new url for local name origin.
git remote set-url origin remote-repo-url
rename existing origin name
The last approach is to rename the existing origin to the new name
git remote rename origin neworigin
Finally, Verify the name mapped to the URLs using the below command.
git remote -v
There are many ways we can fix these errors with the above solutions.