Git

Remote Origin Already Exists on “git push” to a New Repository

While working on a large project with multiple developers, each developer works on their local system and then pushes their local changes to the remote repository to collaborate with other team members. For this purpose, you must add their remote Git repository as a remote to their local repository. However, they may face some errors while adding remote origin.

This write-up will explain the solution for the existing remote origin error.

How Does the “remote origin already exists” Error Occur?

Users push their local code changes to the particular remote repository. Sometimes, they may want to push those changes to another remote repository. For this purpose, it is required to add another GitHub repository as a remote in the local repository. While adding a new remote, some errors like the “remote origin already exists” occurs. This is because the remote “origin” already exists in the current repository.

In the below image, it can be seen that when we add a remote origin, it gives an error:

git remote add origin https://github.com/laibayounas/Perk_Repo.git

Here, as you can see the “origin” already exists in the current repository:

git remote -v

How to Resolve the “remote origin already exists” Error?

To resolve the above-discussed error, different methods can be used:

Method 1: Add Remote with a Different Name

To add a remote with a different name, utilize the “git remote add” command and specify the new desired remote name and remote repository URL:

git remote add myOrigin https://github.com/laibayounas/Perk_Repo.git

Here, “myOrigin” is our new remote name:

Then, run the provided command to verify changes:

git remote -v

It can be observed that the new remote has been added:

Method 2: Remove the Existing Remote Origin and Add New Remote

First, type out the following command along with the existing remote name to remove it:

git remote remove origin

Here, “remote” is our existing remote name:

Then, add a new remote with the new remote repository URL:

git remote add origin https://github.com/laibayounas/Perk_Repo.git

Now, verify the newly added remote:

git remote -v

It can be seen that the new remote has been added successfully:

Method 3: Set a New Remote URL

To set the new URL for the already existing remote name, write out the provided command with the “set-url” option:

git remote set-url origin https://github.com/laibayounas/Perk_Repo.git

Then, ensure changes with the below-listed command:

git remote -v

As you can see the remote URL has been changed successfully:

We have explained different methods to resolve the “remote origin already exists” error.

Conclusion

To resolve the “remote origin already exists” error, different methods can be used, such as adding a remote with a different name, removing the existing remote origin and adding a new remote, or setting a new remote URL. This write-up explained the solution for the existing remote origin error.

About the author

Laiba Younas

I have done bachelors in Computer Science. Being passionate about learning new technologies, I am interested in exploring different programming languages and sharing my experience with the world.