error: remote origin already exists. — the repository already has a remote called
origin, and git remote add only creates new names. Run
git remote -v to see where it points. If the URL is right, skip the step; if it is
wrong, change it with git remote set-url origin <url>.
The other way out is git remote remove origin followed by the
git remote add you meant to run. If you want both remotes, give the new one another
name, such as backup or upstream.
The error
$ git remote add origin ../old.git
$ git remote add origin ../old.git
error: remote origin already exists.
$ git remote add origin ../new.git
error: remote origin already exists.
Exit code 3, and nothing changed. The second line was the same command run twice; the third asked for a different URL and got the same answer. Windows PowerShell 5.1 printed the identical message.
Why it happens
A remote is a named entry in .git/config: a name, origin by convention,
and the URL Git fetches from and pushes to. git remote add creates an entry and
refuses a name that is already taken, even when the URL is the same. It never overwrites, so a
typo in the first add cannot be fixed by running add again.
There are two common causes. The first is a clone: git clone always creates
origin, pointing at the place you cloned from, so a setup guide's
git remote add origin ... step fails in a cloned folder:
$ git clone old.git copy
Cloning into 'copy'...
done.
$ git remote -v
origin C:/.../t3/old.git (fetch)
origin C:/.../t3/old.git (push)
$ git remote add origin ../new.git
error: remote origin already exists.
The second is a guide's "add the remote" step run twice, or run after a typo in the URL, as in the error block above. The path in the clone output is shortened here; nothing else is changed.
The fix
Look first, then point the existing remote at the right URL:
$ git remote -v
origin ../old.git (fetch)
origin ../old.git (push)
$ git remote set-url origin ../new.git
$ git remote -v
origin ../new.git (fetch)
origin ../new.git (push)
$ git push -u origin main
branch 'main' set up to track 'origin/main'.
To ../new.git
* [new branch] main -> main
Or remove it and add it again:
$ git remote remove origin
$ git remote -v
$ git remote add origin ../old.git
$ git remote -v
origin ../old.git (fetch)
origin ../old.git (push)
$ git push -u origin main
branch 'main' set up to track 'origin/main'.
To ../old.git
* [new branch] main -> main
git ls-remote --heads origin listed refs/heads/main on the remote after
each fix. set-url is the smaller change: a second test showed it keeps the remote's
other settings, the origin/main tracking branch and the upstream of main.
remove deletes all three, which is why the second fix pushes with -u
again. In the cloned folder, the fix that kept the clone's origin was a second name:
git remote add backup ../backup.git, after which git push backup main
printed * [new branch] main -> main.
How it was reproduced
Git for Windows 2.45.2 (git version 2.45.2.windows.1) in Git Bash 5.2.26, with a
cross-check in Windows PowerShell 5.1. Two local bare repositories, old.git and
new.git, stood in for two hosted remotes, so the output shows paths where you would
see URLs such as https://github.com/.... One repository was made with
git init and given origin by hand, the other with
git clone. The personal global config was replaced by an empty file
(GIT_CONFIG_GLOBAL).
Frequently asked
- How do I fix 'remote origin already exists' in Git?
- Run git remote -v to see the URL origin points to. If it is wrong, run git remote set-url origin followed by the correct URL. If it is already right, skip the git remote add step and push.
- How do I change the remote URL of a Git repository?
- Use git remote set-url origin followed by the new URL, then check it with git remote -v. The alternative is git remote remove origin and git remote add origin with the new URL.
- Can a Git repository have two remotes?
- Yes. Each remote needs its own name, so add the second one under another name, for example git remote add backup followed by its URL, and push to it with git push backup main.
More decoded errors in the Fixes category. The opposite problem, a push to a remote that is not there: 'origin' does not appear to be a git repository.