A remote repository is a version of your project hosted online or on a network, allowing you to collaborate, back up your code, and track changes across environments.
π§ Command to Add a Remote Repository
git remote add origin <remote-url>
origin
is the default name for the remote (you can rename it if needed)<remote-url>
is the HTTPS or SSH link to your repository (e.g., from GitHub)
β Step-by-Step: Adding a Remote
1. Create or Navigate to Your Local Git Repository
If your project isn’t yet a Git repo:
git init
Then check your status with:
git status
2. Copy the Remote Repository URL
From GitHub/GitLab/Bitbucket:
- HTTPS:
https://github.com/username/repo.git
- SSH:
gi*@gi****.com:username/repo.git
Choose SSH if you have SSH keys set up; otherwise, use HTTPS.
3. Add the Remote
git remote add origin https://github.com/username/repo.git
Verify it was added:
git remote -v
Expected output:
origin https://github.com/username/repo.git (fetch)
origin https://github.com/username/repo.git (push)
4. Push Your Code to the Remote
If this is your first push and the remote repo is empty:
git push -u origin main
(Replace main
with master
or your branch name as needed)
The -u
flag sets origin/main
as the default upstream branch.
π Updating or Removing a Remote
- Change the remote URL:
git remote set-url origin <new-url>
- Remove the remote:
git remote remove origin
π§ Summary Table
Command | Description |
---|---|
git remote add origin <url> | Add a new remote named origin |
git remote -v | View existing remotes |
git remote set-url origin <new-url> | Update remote URL |
git push -u origin main | Push to remote and set tracking |
π Final Thoughts
Adding a remote repository connects your local Git project to a cloud-hosted repo, enabling collaboration, versioning, and CI/CD. Make sure to use the correct URL format and branch name.