Solution git fatal: remote origin already exists error on push

This article discusses how to resolve the issue git fatal: remote origin already exists error during the push or add commands.

How to 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 is created in the nested folder of any git repository, the git command throws git fatal: remote origin already exists error.

fatal: remote origin already exists git error

What does its origin mean?

This error comes during multiple use cases.

  • created a local nested folder app, It is a child folder of a local git repository, these folders map to the new remote repository.
B:\blog\jswork>git remote add origin https://github.com/intkiran/react-auth0-login.git
fatal: remote origin already exists.

An error would

fatal: remote origin already exists.

From the above code, It creates an origin local name and adds it to an existing repository.

In any local repository, How do you know the remote repository url? using the -v option gives the local name with a 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 the original 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 git 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 with new url in Git

below set the new url for the local name origin.

git remote set-url origin remote-repo-url

rename existing origin name in Git

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

wrap

There are many ways we can fix these errors with the above solutions.