Skip to main content
Git Basics
CHAPTER 06

Remote Repositories and GitHub

Updated: May 15, 2026
20 min read

# CHAPTER 6

Remote Repositories and GitHub

1. Introduction

Until now, all of our version control has been happening exclusively on our local hard drive. If you drop your laptop in a lake, your code and its entire history are gone forever. Furthermore, working entirely locally makes collaborating with a team impossible. To solve this, we must introduce the concept of a Remote Repository. A remote repository is simply a copy of your Git project hosted on the internet. In this chapter, we will bridge the gap between our local Git engine and the cloud, learning how to connect to GitHub, push our code to the internet, and collaborate securely.

2. Learning Objectives

By the end of this chapter, you will be able to:
  • Define what a Remote Repository is.
  • Connect a local Git repository to GitHub (git remote add).
  • Upload local code to the cloud (git push).
  • Download cloud code to your local machine (git clone).
  • Synchronize your local machine with changes made by others (git pull).

3. Beginner Explanation

Imagine you are writing a book on your laptop.
  • Local Git: Every night, you save a copy of the book to a folder on your desktop. It is safe, but if your house burns down, the book is gone.
  • Remote Git (GitHub): You rent a secure, fireproof safety deposit box at a bank across the country.
  • git push: Every Friday, you drive to the bank and place a copy of your latest chapters into the box. Your work is now backed up off-site.
  • git clone: Your friend wants to help you write. They drive to the bank and make an exact photocopy of everything in the box to take home.
  • git pull: Your friend writes a new chapter and puts it in the bank box. You drive to the bank, see the new chapter, and copy it to your laptop so you are both synchronized.

4. Creating a Remote Repository

To use GitHub, you must create a free account at github.com.
  1. 1. Log in to GitHub and click the "New Repository" button.
  1. 2. Name your repository (e.g., my-website).
  1. 3. Leave it empty (do NOT check "Initialize with a README").
  1. 4. Click "Create repository".
GitHub will now show you a blank screen with a URL (e.g., https://github.com/yourusername/my-website.git). This URL is the address of your new safety deposit box.

5. Connecting and Pushing (git remote & git push)

Now we must tell our local terminal about this URL.

1. Add the Remote:

bash
123
# We name the remote "origin" by default. 
# It points to the GitHub URL.
git remote add origin https://github.com/yourusername/my-website.git

2. Push the Code: We tell Git to "push" our local main branch up to the remote named origin.

bash
12
# The -u flag tells Git to remember this connection for future pushes
git push -u origin main

*Your terminal will ask for your GitHub username and password (or Personal Access Token). Once authenticated, your code will fly into the cloud!*

6. Mini Project: Push Local Project to GitHub

Let's back up the project we've been working on.

Step-by-Step Walkthrough:

  1. 1. Ensure your local commits are saved: git status should say "nothing to commit".
  1. 2. Create a blank repository on GitHub.com. Copy the HTTPS URL.
  1. 3. In your terminal, link them: git remote add origin <PASTEURLHERE>
  1. 4. Push your code: git push -u origin main
  1. 5. Go back to your web browser and refresh the GitHub page.
  1. 6. *The Result:* You will see your index.html file and all your commit messages beautifully displayed on the GitHub website. Your code is officially in the cloud!

7. Downloading Code (git clone & git pull)

If you buy a new laptop, how do you get your code back? You don't use a USB drive. You clone it from GitHub.
bash
1
git clone https://github.com/yourusername/my-website.git

This downloads the entire folder, all the files, and the entire hidden .git history directly to your new laptop.

If your coworker pushes a new commit to GitHub, your local laptop doesn't automatically update. You must manually fetch their changes:

bash
1
git pull origin main

8. Best Practices

  • Always Pull Before You Push: If you are working with a team, someone else might have pushed code to GitHub while you were sleeping. If you try to git push your new code, GitHub will reject it, saying your local machine is "out of sync." You must always git pull the latest changes from the cloud, resolve any conflicts locally, and *then* git push your updates.

9. Common Mistakes

  • Nested Clones: Never run git clone while you are already inside a Git repository. If you are inside FolderA (which has a .git folder) and you clone FolderB (which has its own .git folder) inside it, Git will break, and it won't know which .git database to talk to. Always clone into a clean, non-Git directory.

10. Exercises

  1. 1. What is the operational purpose of the command git remote add origin?
  1. 2. Explain the difference between git clone and git pull. Which one is used for an existing project on your laptop?

11. FAQs

Q: What does the word origin mean? A: origin is just a nickname. Instead of typing the huge https://github... URL every single time you push, Git lets you save that URL under a nickname. By industry convention, everyone names the primary cloud server origin.

12. Summary

In Chapter 6, we liberated our code from the confines of our local hard drive. We introduced the concept of Remote Repositories, utilizing GitHub as our secure, cloud-based backup and collaboration hub. We mastered the critical triad of network commands: connecting our local machine to the cloud (git remote), uploading our history (git push), and downloading collaborative changes (git clone and git pull). By bridging the gap between local execution and cloud hosting, we have unlocked the ability to work asynchronously with developers worldwide.

13. Next Chapter Recommendation

Our code is in the cloud, but what if a bug was introduced three weeks ago? How do we find it? Proceed to Chapter 7: Git Logs and History Management.

Finish this Chapter

Save your progress on your learning path and prepare for coding interview challenges.

Discussion

Join the discussion

Log in or create a free account to participate.

Sort: ·