[Fixed] fatal: remote origin already exists in Git: Step-by-Step Troubleshooting Guide

Executive Summary & Root Cause Analysis: The error error: remote origin already exists (or fatal: remote origin already exists) occurs when you run git remote add origin <url> in a local repository that already has a remote named origin registered in .git/config. Common root causes include: (1) cloning a repository—which automatically creates an origin remote—and then running repository setup commands from a README, (2) migrating a project from one Git provider or organization to another (e.g., Bitbucket to GitHub or HTTPS to SSH), and (3) repeating an initialization script after an earlier setup step failed.

Step 1: Quick Fix – Update the Existing Remote URL with set-url

First, inspect what URL is currently bound to origin. If you want to replace the old remote URL with your new repository URL (for instance, switching from HTTPS to SSH or pointing to a new repo), use git remote set-url:

# 1. Inspect currently configured remote repositories
git remote -v

# 2. Update the 'origin' URL directly in place
git remote set-url origin git@github.com:<username>/<repository>.git

# Or if using HTTPS:
# git remote set-url origin https://github.com/<username>/<repository>.git

Step 2: Remove and Re-add the Origin Remote Cleanly

If you want to clear out stale fetch/push refspecs or completely reset the remote entry before adding a new one, remove the existing origin first:

# 1. Remove the existing 'origin' remote configuration
git remote remove origin

# 2. Add the desired repository URL cleanly
git remote add origin <new-repository-url>

# 3. Push your local branches to the new remote
git push -u origin main

Note: Removing a remote with git remote remove origin only deletes the URL pointer in .git/config; it does not delete any local files, commits, or branches, nor does it affect the remote server.

Step 3: Alternative Workarounds – Rename Old Remote or Configure Multiple Remotes

If you are working with a fork or migrating gradually and want to keep the original remote accessible alongside the new one:

  1. Rename the existing origin (for example, to upstream or old-origin):
    # Rename existing 'origin' to 'upstream'
    git remote rename origin upstream
    
    # Now add your personal fork or new destination as 'origin'
    git remote add origin <your-fork-url>
  2. Add a second remote with a distinct alias instead of overwriting origin:
    # Keep 'origin' intact and register a secondary remote (e.g., backup or production)
    git remote add mirror <secondary-repository-url>
    git push mirror main

Verification & Testing Steps

Confirm that origin points to the intended repository URL and that Git can authenticate and communicate with the remote server:

# Verify fetch and push URLs for all configured remotes
git remote -v

# Test connectivity and list remote references without modifying local files
git ls-remote origin

# Verify branch tracking and push status
git status -sb

Summary Comparison Table

Resolution Method What Happens to Old URL Best Used For Risk Level
git remote set-url origin <url> Overwrites in place Switching HTTPS ↔ SSH or moving to a new repo Zero (Safe)
git remote remove origin + add Deletes pointer & refspecs Clearing corrupted or stale remote configs Zero (Local commits untouched)
git remote rename origin upstream Preserves under new alias Working with open-source forks or gradual migrations Zero (Keeps both remotes)
git remote add <alias> <url> Leaves origin untouched Pushing to multiple remotes (GitHub + Heroku/mirror) Zero (Safe)

Leave a Reply