/errors/git/remote-origin-already-exists
error: remote origin already exists.
- tool
- Git
- exit code
- 3
- versions
- Git 2.x
- os
- linux, macos, windows
error: remote origin already exists. means a remote with that name is already configured in the repository, so git remote add refuses to overwrite it. List the remotes with git remote -v, then change the URL with git remote set-url or use a different name.
Reproduce it
git init -q -b main repo
cd repo
git remote add origin https://example.invalid/first.git
git remote add origin https://example.invalid/second.gitOutput · exit 3
error: remote origin already exists.
Inspect and fix
git init -q -b main repo
cd repo
git remote add origin https://example.invalid/first.git
git remote -v
git remote set-url origin https://example.invalid/second.git
git remote get-url originOutput
origin https://example.invalid/first.git (fetch) origin https://example.invalid/first.git (push) https://example.invalid/second.git
| Goal | Command |
|---|---|
| Show remotes and their URLs | git remote -v |
Change where origin points | git remote set-url origin URL |
Delete origin | git remote remove origin |
| Add a second remote under another name | git remote add upstream URL |
Keep the old remote under another name
git remote rename origin old-origin keeps the old remote under a new name and frees origin. Branch tracking settings of origin move with it.
git init -q -b main repo
cd repo
git remote add origin https://example.invalid/first.git
git remote rename origin old-origin
git remote add origin https://example.invalid/second.git
git remote -vOutput
old-origin https://example.invalid/first.git (fetch) old-origin https://example.invalid/first.git (push) origin https://example.invalid/second.git (fetch) origin https://example.invalid/second.git (push)
The opposite error
git remote set-url and git remote remove need the remote to exist. On a repository without origin (for example right after git init) they fail with error: No such remote 'origin' and exit status 2; in that case git remote add is the right command.
git init -q -b main repo
cd repo
git remote set-url origin https://example.invalid/second.gitOutput · exit 2
error: No such remote 'origin'
How to recognise the cause
| Situation | What git remote -v shows |
|---|---|
| You cloned the repository, then followed "push an existing repository" instructions | origin already points to the URL you cloned from |
| A setup script ran twice | origin points to the same URL you tried to add |
| The project was copied from a template or another project | origin points to the template or the old project |
| You are adding a fork and the original | origin is your fork; the original should be added as upstream |
Prevent it
Run git remote -v before adding a remote, and in scripts use git remote get-url origin to test whether it exists: it exits non-zero when it does not. Then choose between add and set-url instead of always calling add.
FAQ
Does git remote remove origin delete anything on the server?
No. It only removes the remote from the local repository: its configuration and its remote-tracking branches such as origin/main. The repository on the server is not touched.
Does git remote set-url change the push URL too?
Yes, by default it replaces the URL used for both fetch and push. Add --push to change only the push URL, which is how you fetch from one place and push to another.
Why is the exit code 3?
git remote exits with status 3 when the remote you try to add already exists, and with status 2 when the remote you try to change or remove does not exist. Scripts can test these codes instead of parsing the message.