Cloning and Forking
There are two ways to get a copy of a repository: cloning (for your own repos or to work locally) and forking (to contribute to others' repos).
Cloning Repositories
Cloning creates a local copy of a remote repository.
Basic Clone
# HTTPS
git clone https://github.com/owner/repository.git
# SSH (recommended)
git clone git@github.com:owner/repository.git
# GitHub CLI
gh repo clone owner/repository
What Clone Does
- Creates a directory named after the repo
- Downloads all files and history
- Sets up
originremote pointing to the source - Checks out the default branch
Clone Options
# Clone to specific folder
git clone https://github.com/owner/repo.git my-folder
# Clone specific branch
git clone -b develop https://github.com/owner/repo.git
# Shallow clone (only recent history)
git clone --depth 1 https://github.com/owner/repo.git
# Clone with submodules
git clone --recursive https://github.com/owner/repo.git
After Cloning
cd repository
# See the remote
git remote -v
# origin https://github.com/owner/repository.git (fetch)
# origin https://github.com/owner/repository.git (push)
# See branches
git branch -a
# Start working
git checkout -b my-feature
Forking Repositories
A fork is your personal copy of someone else's repository on GitHub.
Why Fork?
| Use Case | Description |
|---|---|
| Contributing | Make changes to propose back |
| Experimentation | Try ideas without affecting original |
| Starting point | Use as base for your own project |
| Archiving | Keep a copy of important code |
How to Fork
- Go to the repository on GitHub
- Click the Fork button
- Choose where to fork (your account or organization)
- Wait for GitHub to copy the repo
Fork vs Clone
| Fork | Clone |
|---|---|
| Creates copy on GitHub | Creates copy on your computer |
| Lives in the cloud | Lives locally |
| Linked to original | Just downloads files |
| For contributing | For local work |
Typical workflow: Fork on GitHub, then clone your fork locally.
The Fork Workflow
Step 1: Fork on GitHub
Click "Fork" on the original repository.
Step 2: Clone Your Fork
git clone git@github.com:your-username/repository.git
cd repository
Step 3: Add Upstream Remote
git remote add upstream https://github.com/original-owner/repository.git
git remote -v
# origin git@github.com:your-username/repository.git (fetch)
# origin git@github.com:your-username/repository.git (push)
# upstream https://github.com/original-owner/repository.git (fetch)
# upstream https://github.com/original-owner/repository.git (push)
Step 4: Keep Your Fork Updated
# Fetch updates from original
git fetch upstream
# Merge into your main branch
git checkout main
git merge upstream/main
# Push to your fork
git push origin main
Step 5: Make Changes
# Create feature branch
git checkout -b fix-typo
# Make changes
# ...
# Push to your fork
git push -u origin fix-typo
Step 6: Create Pull Request
On GitHub, create a PR from your fork's branch to the original repository.
Syncing a Fork
Via Command Line
# Fetch upstream changes
git fetch upstream
# Merge into your branch
git checkout main
git merge upstream/main
# Push to your fork
git push origin main
Via GitHub UI
- Go to your fork on GitHub
- Click "Sync fork" button
- Click "Update branch"
Handling Sync Conflicts
If your fork has diverged:
# Fetch upstream
git fetch upstream
# Rebase your changes on top of upstream
git checkout main
git rebase upstream/main
# Force push (your fork only!)
git push --force origin main
When to Clone vs Fork
Clone When
- It's your own repository
- You have direct push access
- You just want a local copy to browse
- You're working on a private team project
Fork When
- You want to contribute to someone else's project
- You don't have push access
- You want your own copy to modify
- You want to propose changes via pull request
GitHub CLI for Cloning and Forking
# Clone a repository
gh repo clone owner/repo
# Fork and clone in one step
gh repo fork owner/repo --clone
# Fork without cloning
gh repo fork owner/repo
# Clone your own repo
gh repo clone my-repo
Submodules
Some repositories contain other repositories:
# Clone with submodules
git clone --recursive https://github.com/owner/repo.git
# Initialize submodules after clone
git submodule init
git submodule update
# Update submodules
git submodule update --remote
Large Repositories
For very large repositories:
# Shallow clone (faster, less history)
git clone --depth 1 https://github.com/owner/large-repo.git
# Later, get more history if needed
git fetch --unshallow
# Partial clone (Git 2.22+)
git clone --filter=blob:none https://github.com/owner/large-repo.git
Troubleshooting
Authentication Issues
# If HTTPS prompts for password, use token
git clone https://token@github.com/owner/repo.git
# Or switch to SSH
git remote set-url origin git@github.com:owner/repo.git
Permission Denied
- Check SSH key is added to GitHub
- Verify repository access permissions
- Use HTTPS with personal access token
Slow Clone
- Use shallow clone:
--depth 1 - Clone specific branch only
- Check network connection
Summary
- Clone: Creates local copy for working on
- Fork: Creates GitHub copy for contributing
- Use SSH for passwordless authentication
- Add
upstreamremote to track original repo - Keep forks synced with upstream
- Use shallow clones for faster downloads
- The fork workflow is essential for open source contribution
In the next lesson, we'll learn how to set up your GitHub profile.

