How to Deal with fatal: remote origin already exists Error

Tags: softwaredevelopment, webdev, gitlab, github
Published: 2021-09-23 09:37:29

A good [version control system](https://hackernoon.com/top-10-version-control-systems-4d314cf7adea) is critical for the software development life cycle to manage and streamline the development and deployment processes properly. Git is the leading version control system powering numerous projects from simple hobby projects to enterprise-grade applications. 

As with any other software system, users may encounter errors while using Git for source code management. This article will discuss how to [fix the git error: “fatal: remote origin already exists](https://komodor.com/learn/how-to-fix-fatal-remote-origin-already-exists-error/).”

#What is fatal: remote origin already exists Error?

Users will face this error while attempting to create a remote with the name “origin” when a remote with this name already exists within the git repository. Since Git is a distributed version control system, the data is stored locally until the code is pushed to a remote repository. This error occurs when a user tries to link a remote git repository with the local repo using the git remote tool. 

The “**origin**” in this error refers to the name of the remote. If there is a remote named “test” in the repository and a user is trying to add a remote with the same name, it will generate the following error;
```
fatal: remote test already exists
```
The name [“origin” refers to the default remote](https://www.git-tower.com/learn/git/glossary/origin/#:~:text=In%20Git%2C%20%22origin%22%20is,but%20just%20a%20standard%20convention.), and users can use any name as long as it is compatible with Git. However, the best practice is to [adhere to the Git naming convention](https://gist.github.com/digitaljhelms/4287848) when naming git branches.

##Common Causes for fatal: remote origin already exists Error

Newcomers to Git will encounter this error if they try to set up an already existing repository. This is common if the user is trying to follow along with a tutorial without knowing that they have already linked the remote repository or simply forgot that the local repository is already linked to the remote repository (remote “origin” already configured).

Another reason is trying to change the URL of the “origin” remote repository using the git remote add command. The remote add command will attempt to create a new link between the local and the remote repo, and it will create the “fatal: remote origin already exists error” if there is an existing configuration.

##How to Fix the fatal: remote origin already exists Error

Since now we have a clear understanding of this error, let’s see how to mitigate it. Users need to identify any existing remotes before trying to apply a fix. This can be done by running [the git remote command](https://hackernoon.com/git-commands-that-every-developer-must-know-qs1n3yex) within the local git directory.
```
git remote -v
```
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7zr6zsera75tws1ejacn.png)

The output will show any remotes in the repository with both the remote name and the remote URL. If the output matches the required configuration, users should not do anything further as the remote is already configured.

If the output of the remote command is invalid, one of the following methods can be utilized to fix this error.

**Update the URL**
Sometimes the user needs to simply update the remote URL so that the specific remote will point to a different URL. We can do this using the **remote set-url** command. In the below example, we are updating the URL of the remote named “origin.”
```
git remote set-url <remote-name>  <new-remote-url>
```

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/47mhov1sl1mc1wkcrlfo.png)



Now, if we check the remotes using the **git remote** command, it will reflect the updated URL.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xiomays6pp2znpqpqozx.png)

The most important fact to remember here is to point to the correct remote name and URL, or otherwise, it will cause conflicts with the existing code in the repositories. 

**Completely Removing the Existing Remote**
Users can simply remove the existing remote and then re-add the remote. To remove an existing remote, we can use the **remote remove** command with the specific remote name. 
```
git remote remove <remote-name>
```
Let’s assume that we already have a remote called “origin” in a git repository. We can remove that remote using the following command.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zl1e9vdm9kkkac5abajr.png)

After removing, we can re-add the remote to the git repository with the same name. It will not encounter the remote origin already exists error since there are no conflicting remotes.
```
git remote add origin https://github.com/xxxxxxxx/test_application.git
```

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3xbtk12l5hgpv0e8dv77.png)


**Renaming the Existing Remote**
Another available option is to simply rename the existing remote and then add the new remote. We can use the remote rename command to rename existing remotes.
```
git remote rename <old-remote-name> <new-remote-name>
```

For example, we can rename the existing “origin” remote to “origin-old” as shown below.
```
git remote rename origin origin-old
```

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/m2fcjjubmupaji2w5tox.png)

Then we can add a new remote with the same name, which is “origin” in this instance. However, it is important to note that this method will create multiple remotes in the repository, and the user is responsible for managing these remotes. We can verify the existing remotes by running the **remote -v** command.
```
git remote -v
```
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7b0hoymaer4mub4ekr0b.png)


**Conclusion**

In reality, fatal: remote origin already exists error is not a fatal error yet a simple notification informing users that a remote with the same name is already configured in the git repository. Users can easily fix this error using the methods mentioned above. However, the best practice for completely mitigating such errors is to adhere to a [proper git workflow](https://www.atlassian.com/git/tutorials/comparing-workflows/gitflow-workflow#:~:text=The%20overall%20flow%20of%20Gitflow,merged%20into%20the%20develop%20branch) in the SDLC.