_ Keep in mind that the solution you'll use will depend on your specific situation because there are a few different scenarios that can cause this problem to happen._
1. Remove the Existing Remote
The first scenario I'll cover is the one in which there's already a remote called origin, but it's the wrong remote for some reason. Let's say, for the sake of the example, that you used to use GitLab for storing your repositories online and then decided to switch over to GitHub (or vice versa). To go about that, you could follow the steps below:
1. Create a new repository online using GitHub or GitLab.
2. Go to your local repository and remove the existing origin remote.
3. Add the new online repository as the correct origin remote.
4. Push your code to the new origin.
If, for some reason, you skip step #2, that will cause Git to display the "remote origin already exists" message. So a possible solution here would be simply removing the existing remote:
git remote remove origin
Origin is just a name for a remote. It could be a different name for you. To make sure the remote is indeed deleted, you can use the git remote
.
Then, if everything is all right, you can go on to adding the desired remote.
2. Update the Existing Remote's URL
I've just shown you how to remove an existing remote, so you can hopefully add a new one, this time with the correct URL. However, you might be thinking that removing the remote and adding it again with a different link will have an eerily similar result as updating the URL of the existing remote. If that's the case, you're right, of course.
So let's see how to achieve the same result we got in the previous section but in a faster way. You just have to use a single command:
git remote set-url <REMOTE-NAME> <NEW-URL>
As I mentioned earlier, I've been referring to the remote repository as "origin" in this discussion. However, you're free to use any name for your remote repository. Here's an example using "origin" as the remote name and a URL to an actual repository:
git remote set-url origin https://github.com/git/git.git
3. Rename the Existing Remote
Let's say that, for whatever reason, you already have a remote called origin. You want to add a new origin, but you also need to keep the old one. How would you go about it?
Just rename the existing remote before adding the new one and run the following command and you're set:
git remote rename <old-name> <new-name>
So let's say you want to rename your origin remote to backup. You'd simply run:
git remote rename origin backup
Then you can add your new remote called origin normally, and you should no longer see the "remote origin already exists" error.
4. Do Nothing!
This is not a joke, I promise you. Here's the thing: Sometimes, you might get the "remote origin already exists" error when following a tutorial that has some step asking you to add a remote called origin. If you try to run the command and get the error message, it's possible that you've already executed that command and don't remember.
To check whether that's really the case, you can use the Git remote command with the verbose option:
git remote -v
That will allow you to see the existing remotes along with the URLs they point to. If the existing remote already has the same URL provided by the tutorial, that means your repo is ready to go and you don't need to do anything else.
Top comments (0)