KhueApps
Home/DevOps/Fixing Git error: fatal: No such remote: origin

Fixing Git error: fatal: No such remote: origin

Last updated: October 07, 2025

Overview

The error "fatal: No such remote: origin" means your repository has no remote named origin, or the name is misspelled in your command or config. This guide shows fast, practical fixes for DevOps workflows (local, CI/CD, submodules, and worktrees).

Quickstart (3 steps)

  1. Verify remotes:
    • Run: git remote -v — if empty or no origin, proceed.
  2. Add or fix origin:
    • Add: git remote add origin <url>
    • Or rename: git remote rename <current> origin
  3. Set upstream and push:
    • git branch -M main
    • git push -u origin main

Minimal Working Example

This creates a repo, triggers the error, then fixes it.

# 1) Create a repo with a commit
mkdir demo && cd demo
git init
printf "hello\n" > README.md
git add README.md
git commit -m "init"

# 2) Trigger the error by pushing to a non-existent remote name
git push origin main  # -> fatal: No such remote: 'origin'

# 3) Fix: add the correct remote and push
# Use HTTPS or SSH URL as appropriate
git remote add origin https://github.com/yourorg/yourrepo.git
git branch -M main
git push -u origin main

# 4) Confirm
git remote -v
git remote show origin

Diagnose the cause

  1. Confirm you're inside a Git repo:
    • git rev-parse --is-inside-work-tree should print true.
  2. List remotes:
    • git remote -v — check if origin exists and has a URL.
  3. Check current branch tracking:
    • git branch -vv — look for [origin/<branch>] after your branch name.
  4. Inspect repository config:
    • git config --show-origin --get-regexp '^remote\..*' — shows where remotes are defined.
    • Or open .git/config and look for a [remote "origin"] section.
  5. For submodules/worktrees/CI:
    • Submodules: cd into the submodule path and repeat steps.
    • Worktrees: ensure you're in a valid worktree root (git worktree list).
    • CI: verify the checkout step populates remotes (e.g., avoid --no-single-branch or shallow config that omits origin if your tool modifies remotes).

Common scenarios and fixes

SymptomLikely causeFix
git remote -v shows nothingNo remotes configuredgit remote add origin <url>
You used git push origin main but only upstream existsRemote name mismatchgit remote rename upstream origin
origin exists but URL is wrongMoved repo or changed protocolgit remote set-url origin <url>
Submodule push fails with errorRunning commands in parent repocd path/to/submodule && git remote -v && git push
CI job fails despite local successCheckout step didn’t configure remotesConfigure CI checkout to fetch with remotes; or add git remote add origin <url> in the job
Worktree lacks originConfig stored in main repo; mispointed GIT_DIRRun from the worktree; avoid overriding GIT_DIR; ensure .git file points correctly

Fix recipes

  • Add origin (new or existing local repo):
git remote add origin https://github.com/org/repo.git
# or SSH
git remote add origin [email protected]:org/repo.git
  • Rename an existing remote to origin:
git remote rename upstream origin
  • Change the URL of origin:
git remote set-url origin https://github.com/org/repo.git
  • Recreate a broken origin entry:
git remote remove origin
git remote add origin <url>
  • Set upstream for your branch (avoids having to specify remote/branch every time):
git branch -M main
git push -u origin main
  • Verify after fixing:
git remote -v
git remote show origin

Submodules and nested repos

  • Each submodule has its own .git directory and remotes. cd into the submodule before adding origin.
  • To fix all submodules:
git submodule foreach 'git remote -v || true'
# If missing in a submodule:
# git submodule foreach 'git remote add origin <url-for-that-submodule>'

Worktrees

  • Worktrees share the same object store and often shared config.
  • If a worktree reports the error, ensure:
    • You are in the worktree root (contains a .git file pointing to the main repo).
    • You didn’t set GIT_DIR to the wrong path.

CI/CD notes

  • Some CI runners perform a shallow fetch with a detached HEAD and may rename or omit remotes.
  • Remedies:
    • Add git remote -v in the job to debug.
    • Explicitly add/reset the remote:
git remote remove origin 2>/dev/null || true
git remote add origin "$CI_REPOSITORY_URL"
  • Set upstream before pushing tags or branches:
git branch -M "$CI_DEFAULT_BRANCH"
git push -u origin "$CI_COMMIT_REF_NAME"

Pitfalls

  • Remote names are case-sensitive: Originorigin.
  • git remote add origin fails if origin already exists. Use set-url or rename.
  • Using a wrong URL schema (SSH vs HTTPS) won’t cause this error, but will cause auth failures later.
  • Running commands from a parent directory of the repo won’t show this error; you’ll see "not a git repository" instead.
  • In monorepos with nested Git repos, ensure you’re in the correct .git root.

Performance notes

  • All remote configuration commands are local and instant; only fetch/push hits the network.
  • Avoid unnecessary fetches when fixing the error; prefer git remote -v and git config queries first.
  • In CI, use shallow clones for speed, but ensure remotes remain configured if you need to push.

FAQ

  • Why did origin disappear?
    Someone removed or renamed it, or you cloned via a tool that overrides remotes.

  • Do I have to name it origin?
    No, but many tools default to origin. If you choose another name, use it consistently.

  • How do I see which branches track origin?
    git branch -vv shows tracking info; git remote show origin lists remote branches.

  • Can I have multiple remotes?
    Yes. Add more with git remote add <name> <url> and push/fetch using that name.

  • Does deleting .git/config help?
    No. Edit it carefully or use git remote commands to avoid corrupting repo settings.

Series: Git

DevOps