Skip to content

/errors/git/remote-origin-already-exists

error: remote origin already exists.

verified onenvironmentlocal · windows 10.0.26200 arm64 · GNU bash 5.3.15 (Git Bash) · git version 2.55.0.windows.5updated
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

bash
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.git

Output · exit 3

error: remote origin already exists.

Inspect and fix

bash
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 origin

Output

origin	https://example.invalid/first.git (fetch)
origin	https://example.invalid/first.git (push)
https://example.invalid/second.git
GoalCommand
Show remotes and their URLsgit remote -v
Change where origin pointsgit remote set-url origin URL
Delete origingit remote remove origin
Add a second remote under another namegit 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.

bash
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 -v

Output

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.

bash
git init -q -b main repo
cd repo
git remote set-url origin https://example.invalid/second.git

Output · exit 2

error: No such remote 'origin'

How to recognise the cause

SituationWhat git remote -v shows
You cloned the repository, then followed "push an existing repository" instructionsorigin already points to the URL you cloned from
A setup script ran twiceorigin points to the same URL you tried to add
The project was copied from a template or another projectorigin points to the template or the old project
You are adding a fork and the originalorigin 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.

Sources