Skip
Arish's avatar

32. Exercise - Adding a Remote


Exercise: Adding a Remote

Practice working with remote repositories.

Setup

Use the portfolio project from Task 1, or create a new one:

bash
1mkdir remote-practice
2cd remote-practice
3git init
4echo "# Remote Practice" > README.md
5git add README.md
6git commit -m "Initial commit"

Exercise 1: View Existing Remotes

Task

Check if any remotes are configured.

Steps

bash
1# List remotes
2git remote
3
4# If empty, no output
5
6# List with URLs
7git remote -v
8
9# Also empty if no remotes

Exercise 2: Add a Remote

Task

Add a remote called origin.

Steps

bash
1# Add remote (use your own GitHub URL)
2git remote add origin https://github.com/your-username/remote-practice.git
3
4# Verify
5git remote -v
6
7# Output:
8# origin  https://github.com/your-username/remote-practice.git (fetch)
9# origin  https://github.com/your-username/remote-practice.git (push)

Exercise 3: Add a Second Remote

Task

Add a backup remote pointing to GitLab.

Steps

bash
1# Add backup remote
2git remote add backup https://gitlab.com/your-username/remote-practice.git
3
4# View all remotes
5git remote -v
6
7# Output:
8# backup  https://gitlab.com/your-username/remote-practice.git (fetch)
9# backup  https://gitlab.com/your-username/remote-practice.git (push)
10# origin  https://github.com/your-username/remote-practice.git (fetch)
11# origin  https://github.com/your-username/remote-practice.git (push)

Exercise 4: Rename a Remote

Task

Rename origin to github.

Steps

bash
1# Rename remote
2git remote rename origin github
3
4# Verify
5git remote -v
6
7# Output now shows 'github' instead of 'origin'

Exercise 5: Change Remote URL

Task

Change the github remote from HTTPS to SSH.

Steps

bash
1# Current URL (HTTPS)
2git remote -v
3# github  https://github.com/... (fetch)
4
5# Change to SSH
6git remote set-url github git@github.com:your-username/remote-practice.git
7
8# Verify
9git remote -v
10# github  git@github.com:your-username/remote-practice.git (fetch)

Exercise 6: View Remote Details

Task

See detailed information about a remote.

Steps

bash
1# Show remote details
2git remote show github
3
4# Output includes:
5# - Fetch/Push URLs
6# - HEAD branch
7# - Remote branches
8# - Tracking info

Exercise 7: Remove a Remote

Task

Remove the backup remote.

Steps

bash
1# Remove remote
2git remote remove backup
3
4# Verify it's gone
5git remote -v
6
7# Only 'github' remains

Exercise 8: Reset to Standard Setup

Task

Rename github back to origin (standard convention).

Steps

bash
1# Rename back to origin
2git remote rename github origin
3
4# Verify
5git remote -v
6
7# Standard setup:
8# origin  git@github.com:your-username/remote-practice.git

Challenge: Fork Workflow Setup

Task

Set up a typical fork workflow with origin (your fork) and upstream (original).

Steps

bash
1# Assuming you forked a repo and cloned your fork
2# Your fork is already set as origin
3
4# Add the original repo as upstream
5git remote add upstream https://github.com/original-author/original-repo.git
6
7# Verify both remotes
8git remote -v
9
10# Output:
11# origin    https://github.com/your-user/forked-repo.git (fetch)
12# origin    https://github.com/your-user/forked-repo.git (push)
13# upstream  https://github.com/original-author/original-repo.git (fetch)
14# upstream  https://github.com/original-author/original-repo.git (push)

Typical Fork Workflow

bash
1# Fetch from upstream (original repo)
2git fetch upstream
3
4# Merge upstream changes
5git merge upstream/main
6
7# Push to your fork
8git push origin main

Self-Check Questions

  1. What command lists all remotes?
  2. How do you add a new remote?
  3. How do you change a remote's URL?
  4. What's the difference between origin and upstream?

Answers

  1. git remote or git remote -v
  2. git remote add <name> <url>
  3. git remote set-url <name> <new-url>
  4. origin is your repo, upstream is the original (for forks)

Summary

You've practiced:

  • ✅ Viewing remotes
  • ✅ Adding remotes
  • ✅ Renaming remotes
  • ✅ Changing remote URLs
  • ✅ Removing remotes
  • ✅ Fork workflow setup

Continue to Project Task 2!