As you use Git, you might come across this error, “fatal: remote origin already exists”. If this is your first time facing this error, it could be hard to understand the cause and where to start. Furthermore, if you are not familiar with Git, you may not know what remote origin means. This article discusses the definition of remote origin and how to resolve the error differently.
In Git, “origin” refers to the remote repository that your local project was or will be cloned from. The name, “origin”, follows the naming convention to indicate the source of your storage. However, the name is not immutable, even though that is not desirable. You may be familiar with the git clone command like below.
|$ git clone https://github.com/gitproject/git-your-project.git|
From the example, the git url next to the clone command becomes the origin. If you have already cloned from a remote repo, you can check your remote origin by:
|$ git slight -v|
origin https://github.com/project/your-project.git (fetch)
origin https://github.com/project/your-project.git (push)
The command gives you where your local repository was fetched and to which warehouse you push your code.
The fatal error tells you that you are trying to create a remote with the name “origin” while that name already exists in your remote repository. This error can happen to anyone who didn’t create a new repository with a different name. This error can also occur when you try to modify the “origin” url of the remote repository by using the git remote add command below.
|$ git remote add origin [url].gits|
Since your url in the square brackets already exists, Git will return that fatal error message.
There are several ways to resolve the “fatal: remote origin already exists” error. Choose the best option depending on your situation.
Removing an existing resource comes with a risk. So, use this method when your remote’s name is incorrectly configured, and you want to remove the remote. To remove the existing remote, follow the steps.
- Create a new repository in your git console.
- In your local repository, remove the current origin remote.
- Add the new remote repository with the correct name.
- Push your code to that new origin.
In step 2, you can run the following command to remove your existing remote.
|$ git remote remove origin|
Here, “origin” is merely a name for your remote, and it can be any name. To check if your remote is deleted, you can execute the Git remove -v command as explained above. The command will give you an empty result since you remove your only one or a list of handlers. Now, you can run the git remote add origin command.
Removing the remote origin and adding a new one has the same result as changing the existing remote’s URL. All we want is to replace the duplicate remote definition with a unique one. To change the url, we need to use the following command.
|$ git remote set-url <REMOTE-NAME> <NEW-URL>|
Once this is completed, you can now push and pull code from the newly configured Git repository location. The remote name section would be “origin” in our case, but depending on your settings, it can be different. A complete command will look like below.
|$ git remote set-url origin https://github.com/git/git.git|
If there is a remote named “origin” already and you just tried to create a new origin, it will cause a fatal error. You can change the remote name to another so that you can create a new one without an error. The command you can use is:
|$ git remote rename <old-name> <new-name>|
The old name section will be your current remote name that caused a conflict. A complete command will look like this:
|$ git remote rename origin old-remote|
This will let you add your new remote name, “origin,” without the error.