DaleSchool

Remote Repositories

Beginner25min

Learning Objectives

  • Add and verify a remote repository
  • Upload changes with git push
  • Explain the difference between git fetch and git pull
  • Understand remote-tracking branches (origin/main)

Working Code

Example 1: Connecting a remote repository

Link your local repository to a GitHub remote:

cd ~/my-project
git remote add origin https://github.com/username/my-project.git

Verify the connection:

git remote -v

Output:

origin  https://github.com/username/my-project.git (fetch)
origin  https://github.com/username/my-project.git (push)
  • origin: The nickname for the remote repository (convention is to use "origin")
  • (fetch): URL for downloading
  • (push): URL for uploading

Example 2: First push

git push -u origin main

Output:

Enumerating objects: 3, done.
Counting objects: 100% (3/3), done.
Writing objects: 100% (3/3), 234 bytes | 234.00 KiB/s, done.
Total 3 (delta 0), reused 0 (delta 0), pack-reused 0
To https://github.com/username/my-project.git
 * [new branch]      main -> origin/main
Branch 'main' set up to track remote branch 'main' from 'origin'.

The -u flag sets the upstream. After this, you can just run git push.

Example 3: Push after changes

echo "Updated content" >> README.md
git add README.md
git commit -m "docs: update README"
git push

Output:

To https://github.com/username/my-project.git
   a1b2c3d..e4f5g6h  main -> origin/main

Try It Yourself

Understanding remote-tracking branches

After pushing, Git manages two branches:

Local:   main        <- Your working branch
Remote:  origin/main <- A copy of the remote repository's state
git branch -a   # All branches (local + remote)

Output:

* main
  remotes/origin/main

The remote-tracking branch (origin/main) remembers the last synced state with the remote. You cannot modify it directly.

fetch vs pull

When a teammate has pushed new commits to the remote:

Remote (origin/main): A -> B -> C -> D  (teammate added C, D)
Local (main):         A -> B            (you don't know yet)

git fetch: Downloads remote changes but does not modify your local branch

git fetch origin
git log --oneline --all

Output:

* d5e6f7g (origin/main) feat: teammate's new feature
* c1d2e3f feat: another teammate commit
* b1c2d3e (HEAD -> main) docs: update README

origin/main is updated but your local main still has the old version.

# Check what the remote has that you don't
git log main..origin/main   # Commits only in origin/main

git pull: fetch + merge (or rebase)

git pull

Output:

Updating b1c2d3e..d5e6f7g
Fast-forward
 new-feature.js | 5 +++++
 1 file changed, 5 insertions(+)

Working with remote branches

# List all remote branches
git branch -r

# Check out a remote branch locally
git checkout -b feature-x origin/feature-x

# Push a local feature branch to remote
git push origin feature-login

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

Starting with clone

When joining a team project:

git clone https://github.com/team/project.git
cd project
git log --oneline --all

Clone automatically:

  1. Downloads the repository
  2. Sets origin as the remote
  3. Configures local main to track origin/main

"Why?" -- Why remote repositories matter

| Scenario | Role of Remote Repository | | ----------------------------- | ---------------------------------------- | | Working from another computer | Clone and pull for the latest state | | Team collaboration | Share changes | | Backup | Recover if local machine fails | | Deployment | CI/CD detects pushes for auto-deployment |

Remote-tracking branch diagram

GitHub (remote):
  main: A -> B -> C -> D

Your computer (local):
  origin/main: A -> B -> C  <- As of last fetch/pull
  main:        A -> B -> E  <- Your work (not yet pushed)

After git push:

GitHub (remote):
  main: A -> B -> C -> D -> E

Your computer (local):
  origin/main: A -> B -> C -> D -> E  <- Synced
  main:        A -> B -> C -> D -> E

Common Mistakes

Mistake 1: Pushing without pulling first

git push
# error: failed to push some refs
# hint: Updates were rejected because the remote contains work...

A teammate pushed first:

git pull   # Fetch remote changes first
git push   # Then push

Mistake 2: Force push

# Dangerous: overwrites remote history
git push --force

# Teammates may lose their work!

Never use --force on shared branches (main, develop).

Mistake 3: Confusing remote and local branch names

# Push local feature-login to remote feature-login
git push origin feature-login

# Push local feature-login to remote feature-v2 (different name)
git push origin feature-login:feature-v2

Deep Dive

SSH vs HTTPS authentication

Two ways to connect to GitHub:

HTTPS (URL: https://github.com/...)

  • Authenticate with a Personal Access Token
  • Easy initial setup
  • May prompt for token each time (can save in keychain)

SSH (URL: git@github.com:...)

  • Authenticate with SSH keys
  • Once set up, no password needed

Generate an SSH key:

ssh-keygen -t ed25519 -C "your@email.com"
cat ~/.ssh/id_ed25519.pub   # Add the public key to GitHub

Go to GitHub Settings -> SSH and GPG keys -> New SSH key and paste the public key.

Changing the remote URL

When switching from HTTPS to SSH or migrating repositories:

# Check current URL
git remote -v

# Change URL
git remote set-url origin git@github.com:username/repo.git

# Verify
git remote -v
Multiple remote repositories

A common pattern when contributing to open source:

# Your fork (push target)
git remote add origin git@github.com:myname/project.git

# Original repository (fetch latest changes)
git remote add upstream git@github.com:original/project.git

git remote -v
# origin    git@github.com:myname/project.git (fetch)
# origin    git@github.com:myname/project.git (push)
# upstream  git@github.com:original/project.git (fetch)
# upstream  git@github.com:original/project.git (push)

# Sync with the original
git fetch upstream
git merge upstream/main
  1. Create a new repository on GitHub (empty, without a README).
  2. Link it locally with git remote add origin URL.
  3. Run git push -u origin main for your first push.
  4. Edit a file directly on GitHub, then run git fetch origin and check with git log --oneline --all.
  5. Run git pull to update your local main.

Q1. What two commands does git pull combine?

  • A) git fetch + git push
  • B) git clone + git merge
  • C) git fetch + git merge
  • D) git remote + git update