← all lessons
Collaboration · lesson 7 of 18

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

The idea

The Sync Analogy

Think of remotes like cloud storage for your Git repository:

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

A remote is just another clone — most teams host theirs on GitHub, GitLab, or Bitbucket.
# 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

Dos & don’ts

✅ DO

❌ DON’T

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.