[Git] Error: fatal: origin does not appear to be a git repository — How to Fix It

Summary

The Git error fatal: origin does not appear to be a git repository appears when the local repository has no valid remote named origin, or the stored URL points to a non-existent or unauthorized location. It’s frequent after manual folder copies, renames, or repository migrations. The fix involves re-adding or correcting the remote URL, validating authentication (SSH or HTTPS with Personal Access Token), and ensuring the correct branch is pushed to the restored remote.

Context

Git associates each local repository with remote URLs stored in .git/config. The default remote is named origin, pointing to a repository on GitHub, GitLab, or another service. When a project folder is copied manually without the hidden .git directory, or when the remote is deleted or corrupted, Git loses this mapping and cannot push or fetch. On systems running Git 2.34+ (Linux or macOS), the issue typically appears right after executing git push or git fetch in a directory that visually looks like a repository but lacks full remote configuration.

Probable Cause

  • Remote “origin” missing. The .git/config file does not include a remote section due to a manual folder copy or a corrupted configuration.
  • Incorrect remote URL. The repository was renamed, moved to another organization, or converted from SSH to HTTPS, leaving an invalid path in origin.
  • Authorization failure. The SSH key or HTTPS token no longer grants access, causing fetch/push operations to fail.
  • Outdated branch reference. Attempting to push master when the remote uses main, or vice versa, triggers confusion during the initial push.

Quick Fix

  1. List remotes. Check whether origin exists or points to a valid URL.
git remote -v
  1. If empty or invalid, remove and re-add the remote. Replace <repo_url> with your actual HTTPS or SSH link.
# Remove old origin if exists
git remote remove origin

# Add correct HTTPS remote (requires PAT for private repos)
git remote add origin https://github.com/username/project.git
  1. For SSH users: verify authentication and ensure the key is loaded.
ssh -T git@github.com
  1. Set the correct upstream branch. GitHub now defaults to main.
git push -u origin main
  1. For HTTPS with Personal Access Token (PAT): use tokens instead of passwords when prompted. Generate one in GitHub → Settings → Developer settings → Personal access tokens → Tokens (classic), then:
git push https://<username>:<TOKEN>@github.com/<username>/project.git
  1. Confirm remote and push access. If the repository is private, ensure your PAT includes repo or workflow scopes.

Full Example

Scenario: A user cloned a repository months ago, renamed the local folder, and deleted .git/config. When trying git push, they get the fatal error.

$ git push
fatal: 'origin' does not appear to be a git repository
fatal: Could not read from remote repository.

Diagnosis: No remote entry found.

$ git remote -v
# (no output)

Fix: Re-add origin with HTTPS, authenticate, and push to main.

$ git remote add origin https://github.com/lmiguel/sample-repo.git
$ git branch -M main
$ git push -u origin main
Username for 'https://github.com': lmiguel
Password for 'https://lmiguel@github.com': <enter PAT token>
Enumerating objects: 12, done.
Counting objects: 100% (12/12), done.
Compressing objects: 100% (8/8), done.
Writing objects: 100% (12/12), done.
To https://github.com/lmiguel/sample-repo.git
 * [new branch]      main -> main

Verification:

$ git remote -v
origin  https://github.com/lmiguel/sample-repo.git (fetch)
origin  https://github.com/lmiguel/sample-repo.git (push)
$ git ls-remote origin

Once confirmed, the repository is properly linked and can fetch or push without errors. The same procedure works on Linux and macOS; on Windows, run these commands in Git Bash or PowerShell using equivalent syntax.

Pitfalls & Debug

  • Symptom → Remote still invalid after re-adding. Fix → Check for typos and ensure URL uses correct protocol (git@github.com: vs https://).
  • Symptom → “Permission denied (publickey)”. Fix → Verify SSH key is added to GitHub: ssh-add -l and ssh -T git@github.com.
  • Symptom → Push asks for password repeatedly. Fix → Configure credential helper: git config --global credential.helper store or use PAT for HTTPS.
  • Symptom → Still pushing to master. Fix → Rename branch: git branch -M main and re-push.
  • Symptom → Repo cloned but not initialised. Fix → Ensure .git/ folder exists; if missing, re-initialise: git init before adding remote.

Validation & Next Steps

Confirm the remote works and synchronization is clean:

git remote -v
git ls-remote origin
git push -u origin main

If all succeed, your remote configuration is restored. Use SSH for stability on Linux/macOS and HTTPS with PATs for CI/CD or automation. To prevent recurrence, back up .git/config or use git clone instead of manual copies when moving projects.

Sources

https://git-scm.com/docs/git-remote

https://docs.github.com/en/authentication/connecting-to-github-with-ssh

https://docs.github.com/en/authentication/keeping-your-account-and-data-secure/creating-a-personal-access-token

Labels

Tool/Git, OS/Linux, Topic/Remote