What this error means
You see this when running git remote add origin <url> but a remote named origin is already defined in the repo’s .git/config. It often happens after cloning, re-initializing, or re-running a setup script in CI/CD.
Quickstart
- Inspect existing remotes:
git remote -v - Want to change
originURL:git remote set-url origin git@server:org/repo.git - Want a new remote without touching
origin:git remote add upstream git@server:org/repo.git - Want to replace
origin:git remote remove origin && git remote add origin git@server:org/repo.git - Verify:
git remote -vandgit fetch origin
Minimal working example (MWE)
# Create a new repo and provoke the error
mkdir demo-remote-origin && cd demo-remote-origin
git init
git remote add origin git@server:org/old.git
# Adding origin again triggers the error
if git remote add origin git@server:org/new.git; then
echo "Unexpected: no error"
else
echo "Got expected error: fatal: remote origin already exists"
fi
# Fix by updating the URL instead of adding a new remote
git remote set-url origin git@server:org/new.git
echo "Remotes after fix:"
git remote -v
Expected output after fix includes:
origin git@server:org/new.git (fetch)origin git@server:org/new.git (push)
Step-by-step resolution
- Check current state
git remote -vto list existing remotes and URLsgit remote get-url originandgit remote get-url --push originto see fetch/push URLs
- Decide your intent
- Replace the
originURL? Use set-url. - Keep existing
originand add another remote (e.g.,upstream)? Use add. - Rename
originto something else then create a neworigin? Use rename + add. - Remove and recreate
origin? Use remove + add (be mindful of tracking branches).
- Apply the fix
- Change URL in place:
git remote set-url origin git@server:org/repo.git
- Add a second remote (fork/upstream workflow):
git remote add upstream git@server:org/upstream.git
- Rename
originthen add new origin:git remote rename origin old-origingit remote add origin git@server:org/new.git
- Remove and add:
git remote remove origingit remote add origin git@server:org/new.git
- Verify and fetch
git remote -vgit fetch --prune origin
- Restore/set branch tracking if needed
- If tracking was lost (after remove/add), re-set upstream:
git branch -vvto inspectgit push -u origin main(replacemainwith your branch name)
CI/CD-safe pattern (idempotent)
Use a conditional so re-running the job doesn’t fail:
set -euo pipefail
URL="git@server:org/repo.git"
if git remote get-url origin >/dev/null 2>&1; then
git remote set-url origin "$URL"
else
git remote add origin "$URL"
fi
If you only want to set the push URL (keeping fetch URL intact):
git remote set-url --push origin git@server:org/repo-mirror.git
Which approach should I choose?
| Scenario | Recommended command |
|---|---|
| You cloned the repo and just need a new URL for origin | git remote set-url origin <url> |
| You want to keep origin and add a second remote (e.g., upstream) | git remote add upstream <url> |
| You want to preserve origin config but rename it | git remote rename origin old-origin |
| You want a clean slate for origin | git remote remove origin && git remote add origin <url> |
| Automation must not fail if origin exists | Conditional get-url + set-url/add |
Common pitfalls
- Losing tracking configuration
- Removing
origincan orphan branch tracking (branch.remote/branch.merge). Fix withgit push -u origin <branch>orgit branch --set-upstream-to=origin/<branch>.
- Removing
- Wrong protocol/credentials
- SSH vs HTTPS mismatches cause auth failures. Ensure the URL matches your auth method and keys/tokens are loaded.
- CI scripts that always run
git remote add origin ...- Switch to the idempotent pattern using
git remote get-urlguard.
- Switch to the idempotent pattern using
- Confusing fetch and push URLs
git remote set-url origin <url>sets both. Use--pushor--add --pushfor push-only changes or multiple push URLs.
- Typos in remote names
originvsorginsilently creates an extra remote. Always inspect withgit remote -v.
- Editing .git/config manually
- Safe but easy to break formatting. Prefer
git remoteandgit configcommands.
- Safe but easy to break formatting. Prefer
Performance notes
- All
git remoteoperations shown are local metadata updates and are effectively instantaneous. git fetchafter changing URLs incurs network I/O; use--pruneto drop deleted remote refs.- Using
set-urlorrenameavoids reconfiguring branch tracking and prevents unnecessary network operations compared to remove+add. - Adding/removing remotes does not duplicate objects; repository size is unaffected until you fetch.
- In large monorepos, avoid unnecessary
git fetch --all. Target only the remote you changed:git fetch origin.
Troubleshooting checklist
git remote -v— confirm the remotes and URLsgit remote get-url origin— verify the effective fetch URLgit remote get-url --push origin— verify push URL(s)git branch -vv— confirm upstream trackinggit ls-remote origin— check connectivity and permissions
FAQ
What is “origin”?
- The default name for the primary remote created by clone or initial setup.
How do I see if origin exists?
git remotelists remotes.git remote -vshows their URLs.
Can I have multiple remotes?
- Yes. Common names are
origin(your fork) andupstream(the source repository).
- Yes. Common names are
How do I fix this error in a script?
- Guard with
git remote get-url originand thenset-urloraddaccordingly (see CI/CD-safe pattern).
- Guard with
How do I change only the push URL?
git remote set-url --push origin <url>; usegit remote get-url --push originto verify.
Do I need to re-clone after changing origin?
- No. Update the URL and fetch; re-cloning is unnecessary in almost all cases.