How to Add a Remote Repository to Git
In this tutorial, you'll learn about How to Add a Remote Repository to Git. We cover key concepts, practical examples, and best practices.
The Problem
You have a local Git repository and want to push it to GitHub, GitLab, or another remote server, but no remote is configured. Running git push fails with fatal: No configured push destination or fatal: 'origin' does not appear to be a git repository.
Quick Fix
Step 1: Check existing remotes
List all configured remotes:
git remote -v
If nothing appears, no remotes are configured.
Step 2: Add a remote named origin
Connect your local repo to a remote server:
git remote add origin https://github.com/username/my-project.git
Verify it was added:
git remote -v
origin https://github.com/username/my-project.git (fetch)
origin https://github.com/username/my-project.git (push)
Step 3: Push your code to the remote
Push the current branch to the remote and set tracking:
git push -u origin main
Enumerating objects: 5, done.
Counting objects: 100% (5/5), done.
Writing objects: 100% (3/3), 1.2 KiB | 1.20 MiB/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To https://github.com/username/my-project.git
* [new branch] main -> main
Branch 'main' set up to track remote branch 'main' from 'origin'.
Step 4: Rename or remove a misconfigured remote
If you added the wrong URL, fix it:
git remote set-url origin https://github.com/username/correct-repo.git
Or remove and re-add:
git remote remove origin
git remote add origin https://github.com/username/correct-repo.git
Step 5: Add multiple remotes
Push to both GitHub and GitLab:
git remote add github https://github.com/username/repo.git
git remote add gitlab https://gitlab.com/username/repo.git
git push github main
git push gitlab main
Alternative Solutions
Clone with a specific name
Clone a repository and name the remote something other than origin:
git clone -o upstream https://github.com/user/repo.git
Change the remote URL without removing it
Update the remote URL if the repository moves:
git remote set-url origin https://github.com/new-owner/new-repo.git
Common Mistakes to Avoid
Adding the wrong URL. A typo in the remote URL causes push failures. Verify with git remote -v after adding.
Using HTTPS when SSH is required. Some repositories require SSH authentication. Use the SSH format git@github.com:user/repo.git.
Adding a remote before creating the repository on GitHub. The remote URL must point to an existing repository. Create it on the hosting service first.
Pro Tips
Use SSH instead of HTTPS for remotes. SSH keys are more secure and avoid credential prompts. Convert HTTPS URLs with git remote set-url origin git@github.com:user/repo.git.
Name remotes descriptively. Use origin for your fork, upstream for the original repository, and backup for alternative remotes.
Use git remote -v to verify connections. After adding a remote, always verify the URL with git remote -v to catch typos.
Prevention
- Use SSH URLs (
git@github.com:user/repo.git) instead of HTTPS to avoid credential prompts. - Verify the remote URL before pushing with
git remote -v. - Name remotes descriptively (
origin,upstream,backup) for multi-remote workflows.
Built by the developers of DodaTech
Doda Browser, DodaZIP & Durga Antivirus Pro