“fatal: remote origin already exists” problem, you get this git error when you try to create a link to a remote repository called “origin” when another remote repository with that name already exists.
The answer is to alter the URL of the remote repository with the name “origin” to the URL of the remote repository you want to add, rather than trying to establish a new remote repository with the name “origin.”
You can do so with the following command:
git remote set-url origin https://github.com/your/repository
You may configure your local repository wrong if you don’t completely comprehend the instructions you’re doing, or if you cloned a repository that already has a remote with the name “origin” configured.
Let’s take a closer look at remote repositories so you don’t make the same mistake again. The most typical cause of this problem is a misunderstanding of one or more of the following: what a remote repository is, how the remote command works, or, more specifically, what the “origin” aspect of these instructions means.
Most common git errors we have found:
Table of Contents
Why Does the "Remote Origin Already Exists" Error Occur?
Consider the following scenario: You’re working through a Git instruction that you discovered online. So far, everything has gone smoothly. Then you come upon a line that looks like this:
git remote add origin <Your-URL>/<Your-REPOSITORY-NAME>.git
When you try to run the command above, you get the dreaded error message:
fatal: remote origin already exists.
This message is actually quite simple to comprehend. Git does not have a central server, unlike centralized VCSs. Remote repositories, or simply remotes, are what we call them in Git. Remotes are repositories to which you may have read and/or write permissions.
These are normally located on machines other than your own, and you can connect to them using SSH or HTTP. Keep in mind that, despite their name, remotes aren’t always found on remote machines: local remotes, although sounding like an oxymoron, are entirely feasible.
Remotes have names to help you recognise them, and you can have as many as you need or wish per repository. You can’t have two remotes with the same name, though. So, if you try to add a remote with the same name as an existing remote, you’ll get an error message. A fatal blunder.
If you wish to double-check if the remote called origin exists, use the following command:
git remote
This will cause your Git to display a list of all available remotes for the current repository. If you want extra information, use the verbose parameter with the remote command, such as this:
git remote –v
This will yield the names of each remote as well as their URLs:
origin https://github.com/your/repository (fetch)
origin https://github.com/your/repository (push)
By the way, the phrase “origin” will not always appear in the message. Assume you’re trying to add a codeleaks remote, but there’s already one with that name. The error message in this scenario would be:
fatal: remote codeleaks already exists.
The default remote is called origin, just like the default branch in Git is called controller—though that could change in the near future—but you can call it anything you want as long as it’s a legal Git name.
Four Approaches to Resolving the "Remote Origin Already Exists" Error
After explaining why, the error notice appears, we’ll go over some of the possible strategies for resolving the issue. Keep in mind that the remedy you choose will be dependent on your individual situation because this problem can occur in a variety of ways.
Approach 01: Remove the Remote that already exists
The first example we’ll look at is when a remote called origin already exists, but it’s the wrong remote for whatever reason. Let’s pretend you were previously using GitLab to store your online repositories and then switched to GitHub (or vice versa). You could do so by following the instructions below:
- Using GitHub or GitLab, create a newbit online repository.
- Remove the existing origin remote from your local repository.
- Assign the correct origin remote to the new online repository.
- Your code will be pushed to the new origin.
If you omit step #2 for whatever reason, Git will display the “remote origin already exists” message. As a result, simply deleting the existing remote could be a viable option:
git remote remove origin
As previously stated, origin is simply a term for a distant. For you, it could be a different name. You can use the Git remote command, which you saw before, to verify that the remote has been destroyed. After that, if everything looks good, you can proceed to adding the chosen remote.
Approach 02: Update the URL of Existing Remote
We just showed you how to remove an existing remote, so perhaps you can install a new one with the correct URL this time. However, you may believe that removing the remote and re-adding it with a different link will have a result that is eerily similar to changing the URL of the original remote. Of course, if that’s the case, you’re correct.
So, let’s see how we can get the same outcome as we did in the previous section, but in a shorter amount of time. You only need to use one command:
git remote set-url <REMOTE-NAME> <NEW-URL>
We’ve already mentioned that we’ll be talking about origin throughout this post, but there’s nothing stopping you from using whatever remote names you choose. So, using origin as the remote name and a URL to a real repo, a complete example may look like this:
git remote set-url origin https://github.com/git/git.git
Approach 03: Rename the Remote that already exists
Let’s imagine you already have a remote called origin for any reason. You’d want to add a new origin, but you’d like to maintain the previous one as well. What would you do if you had to do it?
Easy. Before you add the new remote, rename the old one. All you have to do now is run the following command:
git remote rename <old-name> <new-name>
Let’s imagine you wish to rename your backup remote to origin remote. You’d simply take off:
git remote rename origin backup
Then you can add your new remote called origin as usual, and the “remote origin already exists” error should be gone.
Approach 04: Don’t need to do Anything
I promise you, this isn’t a joke. But here’s the deal: When following a tutorial with a step that asks you to add a remote called origin, you may get the “remote origin already exists” issue. If you try to run the command and receive an error message, it’s possible you’ve previously done it and aren’t aware of it.
You can use the Git remote command with the verbose option to see if that’s the case, as we’ve seen before:
git remote –v
You’ll be able to see all of the existing remotes, as well as the URLs they link to. If the existing remote already has the same URL as the tutorial, your repo is ready to use and there is no need to do anything else.
"Remote Origin Already Exists" Nothing to get Panic
Git is a must-have in the toolkit of any modern software engineer. Unfortunately, many engineers regard Git as a difficult technology to master. Those assertions include some truth.
Though the most common Git commands are simple to learn and understand, you may come into a particularly challenging component of the tool from time to time. You might, for example, receive a strange error message. Or you can be tripped up by the different ways Git allows you to go back and tweak things.
Conclusion
In conclusion, we discussed how to fix the git error fatal: remote origin already exists. We deconstructed the notion of Git remote repositories and went over a few of the most frequent commands you’ll need to know in order to use them effectively.