Cloning is how you download existing repositories from GitHub to your local machine. Whether you're joining a team project, contributing to open source, or learning from others' code, cloning is your starting point. In this lesson, you'll learn how to clone repositories, understand what happens during a clone, work with cloned projects, and know when to fork vs clone. By the end, you'll be able to work with any repository on GitHub.
What Is Cloning?
Cloning creates a complete local copy of a remote repository. When you clone, you get:
- All files in the repository
- Complete commit history
- All branches
- All tags
- Automatic remote connection (called "origin")
Think of it like:
Downloading a ZIP file gives you the current files. Cloning gives you the entire project history and the ability to contribute back!
When to Clone
- Joining a team project: Get the project to work on
- Contributing to open source: Download to make changes
- Learning from code: Study how others build things
- Using a template: Start with an existing project structure
- Working on multiple computers: Get your own projects on different machines
How to Clone a Repository
Step 1: Find the Repository URL
On GitHub, navigate to the repository you want to clone:
- Go to the repository page on GitHub
- Click the green "Code" button
- You'll see URL options:
- HTTPS:
https://github.com/username/repo.git - SSH:
git@github.com:username/repo.git - GitHub CLI:
gh repo clonecommand
- HTTPS:
- Copy the URL (HTTPS is easiest for beginners; SSH if you've set it up)
Step 2: Clone the Repository
In your terminal, navigate to where you want the project:
# Navigate to your projects folder
cd ~/Documents/projects
# Clone with HTTPS
git clone https://github.com/username/repository.git
# Or clone with SSH
git clone git@github.com:username/repository.gitWhat Happens During Clone
You'll see output like:
Cloning into 'repository'...
remote: Enumerating objects: 125, done.
remote: Counting objects: 100% (125/125), done.
remote: Compressing objects: 100% (85/85), done.
remote: Total 125 (delta 45), reused 98 (delta 32), pack-reused 0
Receiving objects: 100% (125/125), 1.23 MiB | 2.45 MiB/s, done.
Resolving deltas: 100% (45/45), done.Git:
- Creates a new folder with the repository name
- Downloads all objects (commits, files, history)
- Sets up the remote connection (origin)
- Checks out the default branch (usually main or master)
Step 3: Navigate into the Repository
cd repository
# You're now in the cloned repository!
ls -la # See all files including .gitSuccess! You now have a complete working copy of the repository with full Git history.
Clone Options and Variations
Clone into a Different Directory Name
# Clone into a folder with a different name
git clone https://github.com/user/repo.git my-project-name
# Creates folder 'my-project-name' instead of 'repo'Clone a Specific Branch
# Clone only a specific branch
git clone -b branch-name https://github.com/user/repo.git
# Example: clone the 'develop' branch
git clone -b develop https://github.com/user/repo.gitShallow Clone (Faster, Less History)
# Clone with limited history (faster for large projects)
git clone --depth 1 https://github.com/user/repo.git
# Only gets the latest commit, not entire history
# Good for: quick testing, CI/CD, large repositoriesShallow clone limitations:
- Can't see full history
- Some Git operations won't work properly
- Use only when you don't need history
Clone Without Checking Out Files
# Clone repository structure without working files
git clone --bare https://github.com/user/repo.git
# Creates a .git folder with no working directory
# Useful for: server-side repositories, mirrorsClone a Subdirectory (Sparse Checkout)
# Clone only specific folders (Git 2.25+)
git clone --filter=blob:none --sparse https://github.com/user/repo.git
cd repo
git sparse-checkout set path/to/folder
# Only downloads the specified folderWhat to Do After Cloning
1. Verify the Clone
# Check remote is set up
git remote -v
# Output:
# origin https://github.com/user/repo.git (fetch)
# origin https://github.com/user/repo.git (push)
# Check current branch
git branch
# Check commit history
git log --oneline -52. Explore the Repository
# See all branches (local and remote)
git branch -a
# Check repository status
git status
# View available tags
git tag
# Read documentation
cat README.md3. Install Dependencies (If Applicable)
Most projects require installing dependencies:
# For Node.js projects
npm install
# or
yarn install
# For Python projects
pip install -r requirements.txt
# For Ruby projects
bundle install
# Check the README for specific instructions!4. Create Your Own Branch for Changes
# Don't work directly on main
git switch -c feature/my-changes
# Now you can make changes safelyFork vs Clone: Understanding the Difference
What Is Forking?
Forking creates a copy of someone else's repository in your GitHub account. It's a GitHub feature, not a Git command.
What Is Cloning?
Cloning downloads a repository to your local computer. It's a Git command.
Key Differences
Fork (GitHub):
- Creates a copy on GitHub (in your account)
- Maintains connection to original repository
- Lets you propose changes via Pull Requests
- Done through GitHub website
Clone (Git):
- Creates a copy on your computer
- Connects to the repository you cloned from
- Lets you work locally
- Done through Git command line
Typical Workflow
Original Repo → Fork (your copy on GitHub) → Clone (your copy locally)
(GitHub) (GitHub) (Your computer)
You can't push to → You can push to → You work here
the original your fork and commitWhen to Fork vs Clone Directly
Fork first, then clone when:
- Contributing to open source
- You don't have write access to the original repository
- You want to experiment without affecting the original
- You plan to suggest changes back (via Pull Request)
Clone directly (no fork needed) when:
- It's your own repository
- You're a collaborator with write access
- You just want to study the code (read-only)
- It's a team project where you have permission
The Fork-and-Clone Workflow
Here's the complete workflow for contributing to someone else's project:
Step 1: Fork on GitHub
- Go to the repository you want to contribute to
- Click the "Fork" button (top-right)
- GitHub creates a copy in your account
- You now have:
https://github.com/YOU/repo(forked fromhttps://github.com/ORIGINAL/repo)
Step 2: Clone Your Fork
# Clone YOUR fork (not the original)
git clone https://github.com/YOUR-USERNAME/repo.git
cd repoStep 3: Add Upstream Remote
Add the original repository as "upstream" so you can get updates:
# Add the original repo as 'upstream'
git remote add upstream https://github.com/ORIGINAL-OWNER/repo.git
# Verify remotes
git remote -v
# You should see:
# origin https://github.com/YOUR-USERNAME/repo.git (fetch)
# origin https://github.com/YOUR-USERNAME/repo.git (push)
# upstream https://github.com/ORIGINAL-OWNER/repo.git (fetch)
# upstream https://github.com/ORIGINAL-OWNER/repo.git (push)Step 4: Keep Your Fork Updated
# Fetch changes from the original repository
git fetch upstream
# Merge them into your local main branch
git switch main
git merge upstream/main
# Push to your fork
git push origin mainStep 5: Make Your Changes
# Create a feature branch
git switch -c feature/my-contribution
# Make changes, commit
git add .
git commit -m "Add my awesome feature"
# Push to YOUR fork
git push origin feature/my-contributionStep 6: Create a Pull Request
- Go to your fork on GitHub
- Click "Compare & pull request"
- Write a description of your changes
- Submit the pull request to the original repository
🎯 The Complete Fork Workflow
This workflow lets you contribute to any public repository on GitHub! You work in your fork, and when ready, propose your changes to be merged into the original via a Pull Request.
Cloning Your Own Repositories
You'll often clone your own repositories to work on different computers:
Scenario: Working from Multiple Computers
# On your work computer
git clone https://github.com/YOUR-USERNAME/your-project.git
# Work and push changes
git push
# Later, on your laptop
git clone https://github.com/YOUR-USERNAME/your-project.git
# Your work computer's changes are there!
# Make more changes on laptop
git push
# Back on work computer
git pull # Gets laptop changesMultiple Clones = Same Repository
You can have multiple clones of the same repository. They're all independent copies that sync through the remote:
Computer A Clone ←→ GitHub Repository ←→ Computer B Clone
↓ ↓
(git push/pull) (git push/pull)
All clones stay in sync through push and pull!Best Practices for Cloning
1. Organize Your Repositories
# Create a dedicated projects folder
mkdir -p ~/Documents/projects
cd ~/Documents/projects
# Clone repositories here
git clone https://github.com/user/repo.git
# Result:
# ~/Documents/projects/
# ├── repo1/
# ├── repo2/
# └── repo3/2. Read the README First
After cloning, always read the README:
- Setup instructions
- Dependencies to install
- How to run the project
- Contribution guidelines
3. Use SSH for Repositories You Contribute To
# If you'll be pushing often, use SSH
git clone git@github.com:username/repo.git
# More convenient than HTTPS for frequent pushes4. Don't Modify Main Directly
# After cloning, create a branch for your work
git switch -c feature/my-changes
# Keep main clean and in sync with upstream5. Keep Your Fork Updated
If you forked a repository, regularly sync with the original:
# Add upstream if not already added
git remote add upstream https://github.com/original/repo.git
# Regular sync
git fetch upstream
git switch main
git merge upstream/main
git push origin mainPractice Clone Commands
Try these commands to practice cloning and exploring repositories:
Practice Git Clone
Explore cloned repository structure
Try these examples:
Troubleshooting Clone Issues
Issue: Permission Denied
Error: "Permission denied (publickey)" or "Authentication failed"
Solutions:
- For SSH: Make sure your SSH key is set up and added to GitHub
- For HTTPS: Ensure you're using a Personal Access Token, not password
- Check if repository is private and you have access
- Try HTTPS instead of SSH if SSH continues to fail
Issue: Repository Not Found
Error: "Repository not found" or "404 Not Found"
Solutions:
- Double-check the URL for typos
- Verify the repository exists on GitHub
- Check if it's a private repo you don't have access to
- Ensure the username/organization name is correct
Issue: Clone is Very Slow
Problem: Large repository taking forever to clone
Solutions:
# Use shallow clone
git clone --depth 1 https://github.com/user/large-repo.git
# Clone specific branch only
git clone -b main --single-branch https://github.com/user/repo.gitIssue: Out of Disk Space
Error: Clone fails due to disk space
Solutions:
- Free up disk space
- Clone to a different location with more space
- Use shallow clone to reduce size
Command Reference
Here's a quick reference of clone commands:
# Basic Cloning
git clone <url> # Clone repository
git clone <url> <directory> # Clone into specific folder
git clone -b <branch> <url> # Clone specific branch
# Clone Options
git clone --depth 1 <url> # Shallow clone (latest commit only)
git clone --single-branch <url> # Clone only default branch
git clone --bare <url> # Clone without working directory
git clone --mirror <url> # Clone mirror (for backups)
# After Cloning
git remote -v # View configured remotes
git remote add upstream <url> # Add original repo as upstream
git fetch upstream # Get updates from upstream
git branch -a # See all branches
git log --oneline -10 # View recent commits
# Fork Workflow
git remote add upstream <original-url> # Add original as upstream
git fetch upstream # Fetch from original
git merge upstream/main # Merge original changes
git push origin main # Update your forkKey Takeaways
git clonedownloads a complete copy of a repository- Cloning automatically sets up the
originremote - Forking creates a copy on GitHub; cloning creates a copy on your computer
- Fork-and-clone workflow is standard for contributing to open source
- Always add
upstreamremote when working with forks - Read the README after cloning to understand setup requirements
- Use
--depth 1for faster clones of large repositories - You can clone your own repositories on multiple computers
- Create feature branches after cloning, don't work on main directly
- Keep forks updated by regularly fetching and merging from upstream
What's Next?
Excellent work! You now know how to clone repositories, understand the fork-and-clone workflow, and can work with existing projects from GitHub. You're ready to contribute to any repository!
In the next lesson, we'll dive into GitHub README & Documentation. You'll learn how to write effective README files, master Markdown formatting, create professional project documentation, and make your repositories stand out. Good documentation is crucial for collaboration and showcasing your work!
🎯 Practice Assignment
Before the next lesson, practice cloning:
- Clone one of your own repositories on a different computer or folder
- Find an interesting open-source project and fork it, then clone your fork
- Add the original repository as
upstreamand practice fetching updates - Explore the repository structure, branches, and commit history
- Try a shallow clone of a large repository to see the speed difference
- Read the README and try to run the project locally