Remote Repositories

GitHub, push, pull, clone, collaboration

What Are Remote Repositories?

A remote is a copy of your repository hosted somewhere else - usually on GitHub, GitLab, or Bitbucket. It’s how you backup code and work with others.

Your Computer              Cloud (GitHub)
┌─────────────┐           ┌─────────────┐
│   Local     │  push ──▶ │   Remote    │
│   Repo      │ ◀── pull  │   Repo      │
└─────────────┘           └─────────────┘
                               │
                    Other developers clone/pull

Setting Up GitHub

1. Create a GitHub Account

Go to github.com and sign up. It’s free.

2. Create a New Repository

  1. Click the + icon → New repository
  2. Name it (e.g., my-project)
  3. Choose Public or Private
  4. Don’t initialize with README (if you have local code)
  5. Click Create repository

3. Authentication Setup

GitHub requires authentication. Two main options:

Option A: HTTPS + Personal Access Token

# GitHub.com → Settings → Developer settings → Personal access tokens
# Generate token with 'repo' scope
# Use token as password when pushing
# Generate SSH key
ssh-keygen -t ed25519 -C "your@email.com"

# Start SSH agent
eval "$(ssh-agent -s)"
ssh-add ~/.ssh/id_ed25519

# Copy public key
cat ~/.ssh/id_ed25519.pub
# Add this to GitHub → Settings → SSH Keys

# Test connection
ssh -T git@github.com

Connecting Local to Remote

Scenario 1: Local Repo First

You have local code and want to push to GitHub:

# Add remote (name it 'origin')
git remote add origin git@github.com:username/my-project.git

# Verify
git remote -v

# Push for the first time (-u sets upstream)
git push -u origin main

Scenario 2: GitHub Repo First

You want to download an existing repo:

# Clone repository
git clone git@github.com:username/my-project.git

# This automatically:
# - Downloads all files and history
# - Sets up 'origin' remote
# - Creates local 'main' branch tracking origin/main

Managing Remotes

# List remotes
git remote -v

# Add a remote
git remote add origin <url>

# Change remote URL
git remote set-url origin <new-url>

# Remove remote
git remote remove origin

# Rename remote
git remote rename origin upstream

Push & Pull

Push: Send Changes to Remote

# Push current branch to remote
git push

# Push specific branch
git push origin feature-login

# First push of a new branch (set upstream)
git push -u origin feature-login

Pull: Get Changes from Remote

# Pull changes from remote
git pull

# Pull is actually: fetch + merge
git fetch origin    # Download changes
git merge origin/main  # Merge into local

Fetch vs Pull

git fetch                    git pull
┌──────────────────┐        ┌──────────────────┐
│ Downloads new    │        │ Downloads AND    │
│ commits but      │        │ merges changes   │
│ doesn't merge    │        │ automatically    │
└──────────────────┘        └──────────────────┘
   Safe, inspect first         Quick, might conflict

Remote Branches

See All Branches

# Local branches only
git branch

# Remote branches only
git branch -r

# All branches
git branch -a

Work on a Remote Branch

# Fetch all remote branches
git fetch --all

# Create local branch tracking remote
git checkout -b feature-login origin/feature-login

# Or with newer Git
git switch -c feature-login origin/feature-login

Push a New Branch

# Create and push new branch
git checkout -b feature-payment
# ... make commits ...
git push -u origin feature-payment

Delete Remote Branch

# Delete remote branch
git push origin --delete feature-old

# Clean up stale remote tracking branches
git fetch --prune

Collaboration Workflow

The Pull Request Workflow

1. Clone/Pull latest main
2. Create feature branch
3. Make changes and commit
4. Push feature branch
5. Open Pull Request on GitHub
6. Team reviews code
7. Merge PR into main
8. Delete feature branch

Step by Step

# 1. Get latest main
git checkout main
git pull origin main

# 2. Create feature branch
git checkout -b feature/add-login

# 3. Work and commit
echo "login code" > login.js
git add login.js
git commit -m "Add login functionality"

# 4. Push to remote
git push -u origin feature/add-login

# 5. Go to GitHub → Open Pull Request

# After PR is merged:
# 6. Update local main
git checkout main
git pull origin main

# 7. Delete local feature branch
git branch -d feature/add-login

Handling Remote Conflicts

When Push is Rejected

# You try to push...
git push origin main
# ! [rejected] main -> main (non-fast-forward)

# Someone else pushed first. Your local is behind.

Solution: Pull First

# Pull changes (may cause merge conflict)
git pull origin main

# If conflict, resolve it:
# 1. Edit conflicted files
# 2. git add .
# 3. git commit

# Now push
git push origin main

Better: Pull with Rebase

# Cleaner history - replay your commits on top
git pull --rebase origin main

# If conflict during rebase:
# 1. Fix the conflict
# 2. git add .
# 3. git rebase --continue

# Then push
git push origin main

Forking (Open Source)

Fork = Your personal copy of someone else’s repo. Used for contributing to projects you don’t have write access to.

Fork Workflow

# 1. Fork on GitHub (click Fork button)

# 2. Clone YOUR fork
git clone git@github.com:YOUR-USERNAME/project.git

# 3. Add original repo as 'upstream'
git remote add upstream git@github.com:ORIGINAL-OWNER/project.git

# 4. Create feature branch
git checkout -b fix-bug

# 5. Make changes and push to YOUR fork
git push origin fix-bug

# 6. Open Pull Request from your fork to original repo

Keep Fork Updated

# Fetch from original
git fetch upstream

# Merge upstream changes into your main
git checkout main
git merge upstream/main

# Push to your fork
git push origin main

Quick Reference

# Remotes
git remote -v                 # List remotes
git remote add origin <url>   # Add remote
git remote set-url origin <url>  # Change URL

# Clone
git clone <url>              # Download repo
git clone <url> folder-name  # Clone to specific folder

# Push
git push                     # Push to tracked branch
git push -u origin branch    # Push and set upstream
git push origin --delete branch  # Delete remote branch

# Pull & Fetch
git fetch                    # Download without merging
git pull                     # Fetch + merge
git pull --rebase            # Fetch + rebase (cleaner)

# Remote Branches
git branch -r                # List remote branches
git fetch --all              # Fetch all remotes
git checkout -b local origin/remote  # Track remote branch
git fetch --prune            # Remove stale references

Practice Exercise

# 1. Create a GitHub repository (do this on github.com)

# 2. Connect local project to GitHub
mkdir remote-practice && cd remote-practice
git init
echo "# My Project" > README.md
git add . && git commit -m "Initial commit"

# 3. Add remote and push
git remote add origin git@github.com:YOUR-USERNAME/remote-practice.git
git push -u origin main

# 4. Simulate teammate (edit README on GitHub directly)
# Add a line through GitHub's web interface

# 5. Pull the changes
git pull origin main

# 6. Create a feature branch and push
git checkout -b feature/add-docs
echo "Documentation here" > docs.md
git add . && git commit -m "Add documentation"
git push -u origin feature/add-docs

# 7. Go to GitHub and create a Pull Request!