Introduction to Remotes
So far, we've worked with local repositories. To collaborate with others, you need to connect to remote repositories—copies of your project hosted on a server.
What is a Remote?
A remote is a reference to a repository hosted elsewhere (like GitHub, GitLab, or Bitbucket).
Your Computer Server (GitHub)
┌─────────────────┐ ┌─────────────────┐
│ Local Repo │ ←──────────→│ Remote Repo │
│ .git/ │ │ origin/ │
│ - Your commits │ push/pull │ - All commits │
│ - Your branches│ │ - All branches │
└─────────────────┘ └─────────────────┘
Why Use Remotes?
| Benefit | Description |
|---|---|
| Backup | Your code is safely stored in the cloud |
| Collaboration | Share code with team members |
| Deployment | Deploy from a central location |
| CI/CD | Trigger automated tests and builds |
| Code Review | Review changes before merging |
The Origin Remote
When you clone a repository, Git automatically creates a remote called origin:
git clone https://github.com/user/repo.git
cd repo
git remote -v
# origin https://github.com/user/repo.git (fetch)
# origin https://github.com/user/repo.git (push)
origin is just a convention—the default name for the primary remote.
Managing Remotes
View Remotes
# List remote names
git remote
# List with URLs
git remote -v
# Show detailed info
git remote show origin
Add a Remote
git remote add upstream https://github.com/original/repo.git
Now you have two remotes:
origin- Your forkupstream- The original repo
Rename a Remote
git remote rename origin github
Change Remote URL
git remote set-url origin git@github.com:user/repo.git
Remove a Remote
git remote remove upstream
Remote-Tracking Branches
When you fetch from a remote, Git creates remote-tracking branches:
Local branches: Remote-tracking branches:
main origin/main
feature origin/feature
origin/develop
Remote-tracking branches (like origin/main) are read-only references to where the remote branches were last time you fetched.
View Remote-Tracking Branches
# All branches including remote-tracking
git branch -a
# Remote-tracking only
git branch -r
Output:
* main
feature
remotes/origin/HEAD -> origin/main
remotes/origin/main
remotes/origin/develop
Understanding Remote URLs
HTTPS URLs
https://github.com/username/repository.git
- Works through firewalls
- Requires username/password or token
- Easier to set up
SSH URLs
git@github.com:username/repository.git
- Requires SSH key setup
- No password prompts after setup
- More secure
Switching Between Protocols
# Check current URL
git remote -v
# Switch to SSH
git remote set-url origin git@github.com:user/repo.git
# Switch to HTTPS
git remote set-url origin https://github.com/user/repo.git
Exercise: Explore Remotes
Practice working with remote references:
Multiple Remotes
You can have multiple remotes for different purposes:
# Your fork
git remote add origin git@github.com:you/project.git
# Original project (to get updates)
git remote add upstream git@github.com:org/project.git
# Deployment server
git remote add production ssh://deploy@server.com/var/git/project.git
Common remote names:
origin- Your primary remote (your fork)upstream- Original project you forked fromproduction- Production serverstaging- Staging server
The Fetch, Push, and Pull Cycle
┌──────────────────────┐
│ Remote Server │
│ (e.g., GitHub) │
└──────────────────────┘
↑ ↓
push │ │ fetch
│ ↓
┌──────────────────────┐
│ Your Local Repo │
│ │
│ origin/main ←────── fetched data
│ main ←────────────── your work
│ │
└──────────────────────┘
- fetch: Download changes (doesn't merge)
- pull: Fetch + merge
- push: Upload your changes
Remote Configuration
Remote settings are stored in .git/config:
[remote "origin"]
url = git@github.com:user/repo.git
fetch = +refs/heads/*:refs/remotes/origin/*
[remote "upstream"]
url = https://github.com/org/repo.git
fetch = +refs/heads/*:refs/remotes/upstream/*
[branch "main"]
remote = origin
merge = refs/heads/main
You can edit this directly or use git config:
git config remote.origin.url
git config remote.origin.url "new-url"
Summary
- A remote is a reference to a repository on a server
originis the default name for your primary remote- Use
git remote -vto see configured remotes - Remote-tracking branches (like
origin/main) track the remote state - You can have multiple remotes for different purposes
- Use SSH URLs for passwordless authentication
In the next lesson, we'll learn how to push and pull changes.
التالي · الدرس 14 من 40
Pushing and Pulling
قراءة 4 دقيقة
أنهيت 13 من 40 درسًا

اسأل Byte عن هذا الدرس
محادثة خاصة حول هذا الدرس. قرأ Byte الدرس كاملًا، ويجيب خلال ثوانٍ، ويُظهر القسم الذي تأتي منه كل إجابة. يمكنك السؤال بأي لغة.
الأسئلة والأجوبة
أسئلة عامة حول هذا الدرس. يجيب AI Tutor فورًا بلغتك، ويراجع فريق FreeAcademy الإجابات، ويمكن لكل المتعلمين قراءتها.

