Skip to content

1.2.3 Remote Repositories

Git local-remote sync diagram

In this lesson, you will connect your local Git repository to GitHub. You will understand why remote repositories serve three purposes at the same time—backup, collaboration, and portfolio display—and learn how to sync code to the cloud with push, pull, and clone.

  • Create a repository on GitHub
  • Configure SSH connections (no more password prompts)
  • Master git push, git pull, and git clone
  • Write a good README.md

So far, your Git history has only existed on your own computer. If the hard drive fails, all your code and history will be gone.

A remote repository is a copy of your code stored in the cloud, usually on GitHub. It has three core benefits:

  1. Backup — if your computer breaks, your code is still in the cloud
  2. Collaboration — multiple people can push code to the same repository
  3. Showcase — your GitHub profile is your code portfolio, and employers will look at it during interviews

  1. Open github.com
  2. Click Sign up and register with your email
  3. It is recommended to use an English username that is short and easy to remember (for example, zhangsan-dev), because it will appear in your project links

Every time you push code to GitHub, you need to verify your identity. SSH is the most convenient method—configure it once, and you will no longer need to enter a password.

Terminal window
ssh-keygen -t ed25519 -C "[email protected]"

You will be asked a few questions. Just press Enter for all of them (use the default values):

Enter file in which to save the key (/Users/your-username/.ssh/id_ed25519): [Enter]
Enter passphrase (empty for no passphrase): [Enter]
Enter same passphrase again: [Enter]
Terminal window
# macOS
cat ~/.ssh/id_ed25519.pub | pbcopy
# Linux
cat ~/.ssh/id_ed25519.pub
# Then manually copy the output
# Windows PowerShell
Get-Content ~/.ssh/id_ed25519.pub | Set-Clipboard

The output will look like this (this is the public key and can be safely shared):

Terminal window
ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAI... [email protected]
  1. Open github.com/settings/keys
  2. Click New SSH key
  3. Fill in My Laptop in the Title field (or any name that helps you recognize which computer it is)
  4. Paste the public key you just copied into the Key field
  5. Click Add SSH key
Terminal window

If you see:

Hi zhangsan! You've successfully authenticated, but GitHub does not provide shell access.

then the setup was successful!


Example: Push our earlier AI project to GitHub

Section titled “Example: Push our earlier AI project to GitHub”

Method 1: Create the repository on GitHub first, then connect your local project

Section titled “Method 1: Create the repository on GitHub first, then connect your local project”

Step 1: Create a repository on GitHub

  1. Open github.com/new
  2. Set the Repository name to ai-image-classifier
  3. Set the Description to “A simple image classification project using CNN”
  4. Choose Public (so others can see your work)
  5. Do not check “Add a README file” (we already have one locally)
  6. Click Create repository

Step 2: Connect the local repository to GitHub

GitHub will show you a set of commands. We need the section for “push an existing repository”:

Terminal window
cd ai-image-classifier
# Connect the remote repository (replace zhangsan with your GitHub username)
git remote add origin [email protected]:zhangsan/ai-image-classifier.git
# Push the local code to GitHub
git push -u origin main

git remote add origin means: give the remote repository the name origin (this is the conventional name), and use the URL that comes after it.

-u origin main means: associate the local main branch with the remote main branch. After that, you only need to run git push; you no longer need to type the full command.

Step 3: Verify

Refresh the GitHub page, and you should see your code, commit history, and README there.

Method 2: Clone an empty repository first, then add files to it

Section titled “Method 2: Clone an empty repository first, then add files to it”

If you do not have local code yet, you can do the reverse:

Terminal window
# Clone an empty repository from GitHub (or someone else's project)
git clone [email protected]:zhangsan/my-new-project.git
cd my-new-project
# Write code inside it...
echo "print('hello')" > main.py
# Commit and push
git add .
git commit -m "Add main program"
git push

After you connect the remote repository, daily work becomes very simple:

git push: push new local commits to the remote

Section titled “git push: push new local commits to the remote”
Terminal window
# Write some new code
echo "new feature" >> src/utils.py
git add .
git commit -m "Add data preprocessing function"
# Push to GitHub
git push

git pull: pull remote updates to your local machine

Section titled “git pull: pull remote updates to your local machine”
Terminal window
# Suppose you made changes on another computer (or a teammate did) and pushed them to GitHub
# You need to pull the latest code down
git pull
Terminal window
# Before starting work each day: pull the latest code
git pull
# Write code and make changes...
# After finishing a feature: commit and push
git add .
git commit -m "Complete the data augmentation module"
git push
# Keep writing code...
# Finish another feature
git add .
git commit -m "Add training log recording"
git push

git clone: download someone else’s project

Section titled “git clone: download someone else’s project”

This may be the first Git operation you use: downloading an open-source project from GitHub:

Terminal window
# Clone an AI-related open-source project
git clone [email protected]:ultralytics/yolov5.git
cd yolov5
ls

git clone does three things:

  1. Creates a folder with the same name as the project
  2. Downloads all the code and the full commit history
  3. Automatically configures the remote repository connection
Terminal window
# View the commit history of this project
git log --oneline -10 # View the latest 10 entries
# See which branches exist
git branch -a
# View the remote repository URL
git remote -v

The homepage of every GitHub project automatically displays the contents of README.md. A good README is the front door of your portfolio.

# Project Name
A one-sentence introduction to what this project does.
## 📋 Project Overview
Use 2–3 sentences to describe the project background, the problem it solves, and the method it uses.
## ✨ Key Features
- Feature 1: trains a baseline image classifier
- Feature 2: saves metrics and example predictions
- Feature 3: documents how to rerun the experiment
## 🛠️ Tech Stack
- Python 3.11
- PyTorch 2.0
- Other libraries used
## 🚀 Quick Start
### Environment setup
​```bash
git clone [email protected]:yourname/project.git
cd project
pip install -r requirements.txt
​```
### Run
​```bash
python src/train.py
​```
## 📊 Experimental results
| Model | Accuracy | Training time |
|------|:-----:|:------:|
| SimpleCNN | 85.2% | 10 min |
| ResNet18 | 92.7% | 30 min |
## 📁 Project structure
​```
project/
├── data/ # Data files
├── models/ # Trained models
├── src/
│ ├── model.py # Model definition
│ ├── train.py # Training script
│ └── utils.py # Utility functions
├── requirements.txt
└── README.md
​```
## 📄 License
MIT

Example: Update the README for our project

Section titled “Example: Update the README for our project”
Terminal window
# Write a README using the template above (simplified version)
cat > README.md << 'READMEEOF'
# AI Image Classifier
An introductory project that uses CNN to classify images on the CIFAR-10 dataset.
## Tech Stack
- Python 3.11
- PyTorch 2.0
## Quick Start
```bash
git clone [email protected]:zhangsan/ai-image-classifier.git
cd ai-image-classifier
pip install -r requirements.txt
python src/train.py
ai-image-classifier/
├── data/ # Data files (ignored by git)
├── models/ # Model weights (ignored by git)
├── src/
│ ├── model.py # CNN model definition
│ ├── train.py # Training script
│ └── utils.py # Utility functions
├── .gitignore
├── requirements.txt
└── README.md

READMEEOF

git add README.md git commit -m “Improve README: add project description and usage” git push

---
## Evidence to Keep
Keep this page's proof of learning as a small evidence card:
```text
repo_state: git status before and after the operation
operation: init, add, commit, branch, merge, remote, pull, or push command used
history: git log or branch graph showing what changed
failure_check: untracked files, wrong branch, merge conflict, or remote/auth issue
Expected_output: a clean Git trace that another learner can replay safely
Review notes and pass criteria

This page passes when the relationship between local and remote repositories can be checked by another person, not just when the commands have been typed.

Minimum evidence:

  1. git remote -v shows origin pointing to the intended repository.
  2. git status is clean after the push.
  3. git log --oneline -3 shows the latest README or code commit.
  4. The GitHub page shows the same commit and README.
  5. If push is rejected, you can explain the remote difference before running git pull and resolving it.

Use git push -u origin main only for the first upstream link. After that, the daily rhythm is pull -> edit -> add -> commit -> push.

! [rejected] main -> main (fetch first)

This means the remote repository has commits that you do not have locally (maybe you changed them on another computer, or a teammate pushed new code). The solution is:

Terminal window
git pull # First pull the remote updates
git push # Then push again

Cloning GitHub projects can be slow in some regions. Here are a few solutions:

Terminal window
# Option 1: Clone only the latest version (do not download full history), which is much faster
git clone --depth 1 [email protected]:yourname/ai-image-classifier.git
# Option 2: Use a mirror for acceleration
# Replace github.com with a mirror site (please search for the latest available mirror URL)
Terminal window
# Check the currently connected remote repository
git remote -v
# Change the remote repository URL
git remote set-url origin [email protected]:correct-username/correct-repository-name.git

CommandPurposeWhen to use
git remote add origin URLConnect a remote repositoryBefore the first push for a new project
git pushPush local commits to the remoteAfter finishing a feature
git pullPull remote updates to localBefore starting work
git clone URLDownload a remote repository to localWhen you get a project for the first time

Daily workflow: pull → write code → add → commit → push. It’s that simple.