While setting up a repository, Git throws error: remote origin already exists. What’s the correct way to resolve this without breaking the repo configuration?
This normally occurs when the remote named origin has been configured. It is also best to look at what is already established before making any changes. The URLs will be displayed by running the command git remote -v. The repo is normally okay, most of the time, you are simply attempting to add a remote which already exists.
It has happened to me a couple of times, when I clone a repo and start adding origin just because it is a habit. In case of an incorrect URL, there is no need to delete the repo or to re-clone it. It is possible to just update the existing remote, rather than to add one.
To make the matter worse, the neatest fix is often the modification of the remote URL instead of taking things off without a reason. It is not that Git commits and branches will fail due to the update of the remote reference. The remote origin already exists error message consists of a more of a warning that the name is taken and not that the name is corrupted.
I would not delete the remote unless you are certain that you do not need it. In Projects involving a team, it is easy to get lost. Changing the name of the remote or changing its URL preserves the configuration and does not have any side effects.
One more hint: in case you do require using several remotes (e.g. one GitHub remote and another GitLab one), you can maintain the same origin with another remote of different name. The remote origin already exists only error simply indicates that a particular name is already used not that you cannot add additional remotes.
Concisely, there is no such thing as any broken thing when you witness this mistake. Test out the remotes they have, ensure that they have the correct URL, and update it or use another remote name. Such a solution stores your history of repository and configuration.