Remote Repositories
Sync with the World
Why it matters
Git is distributed, but teams need a shared hub. Understanding remotes, push, pull, and fetch is essential for collaboration.
Key concepts
- Remote — A Git repository on another computer
- Origin — Default name for the remote you cloned from
- Push — Upload local commits to remote
- Pull — Download and merge remote changes
- Fetch — Download remote changes without merging
The idea
The Sync Analogy
Think of remotes like cloud storage for your Git repository:
- clone: Download the entire cloud copy to your computer
- fetch: Check what’s new in the cloud (but don’t download)
- pull: Download changes and merge into your branch
- push: Upload your commits to the cloud
Origin and Upstream
When you clone a repository, Git names that remote “origin”.
flowchart LR
local["Your Clone (local)"]
origin["origin (remote on GitHub)"]
local -->|push| origin
origin -->|pull / fetch| local
For forks, there’s also “upstream” (the original repo you forked from).
Walkthrough
Remote Commands
# Clone repository (creates 'origin' remote)
git clone https://github.com/user/repo.git
git clone git@github.com:user/repo.git # SSH
# View remotes
git remote -v
# Add remote
git remote add origin https://github.com/user/repo.git
git remote add upstream https://github.com/original/repo.git
# Fetch (download without merging)
git fetch origin # Get all branches
git fetch origin main # Get specific branch
# Pull (fetch + merge)
git pull origin main
git pull --rebase origin main # Fetch + rebase instead
# Push
git push origin main # Push to remote
git push -u origin feature # Push and set upstream
git push # Push to tracked upstream
Tracking Branches
# See tracking relationship
git branch -vv
# Set upstream for existing branch
git branch --set-upstream-to=origin/feature feature
# Push and set upstream in one command
git push -u origin feature
Typical Workflow
# 1. Start fresh from remote
git fetch origin
git switch -c feature origin/main
# 2. Do work, commit
git add .
git commit -m "Add feature"
# 3. Stay in sync
git fetch origin
git rebase origin/main
# 4. Push your work
git push -u origin feature
Key takeaways
- Clone creates ‘origin’ remote automatically
- Fetch downloads but doesn’t merge
- Pull = fetch + merge
- Push -u sets upstream tracking
Dos & don’ts
✅ DO
- Fetch before starting work: See if there are updates
- Pull before pushing: Avoid rejected pushes
- Use SSH keys: More secure than passwords
- Set upstream with -u: Makes future pushes easier
❌ DON’T
- Don’t force-push to shared branches: Unless you know what you’re doing
- Don’t push incomplete work to main: Use feature branches
- Don’t ignore push rejections: Pull first, resolve conflicts
Going deeper
SSH vs HTTPS: HTTPS works everywhere but requires tokens/passwords. SSH uses key-based auth—set it up once and never enter credentials again.
Fetch vs Pull:
Experienced developers often fetch then manually inspect changes before merging.
This gives more control: git log origin/main..main shows what’s different.
Common mistakes
“Updates were rejected”:
Someone pushed before you. Solution: git pull --rebase then push again.
Pushing to wrong remote:
Always verify with git remote -v. You might have multiple remotes.