Skip to main content

Cloning and Forking Repositories

Now that you've connected your local repositories to GitHub, let's explore how to work with existing repositories. In this lesson, you'll learn how to bring remote repositories to your local machine and create your own copies of other people's projects.

Learning Goals

By the end of this lesson, you'll be able to:

  • Clone repositories to create local copies
  • Fork repositories to create your own version
  • Understand when to use cloning vs forking
  • Work with forked repositories effectively

What is Cloning?

Cloning creates a complete copy of a remote repository on your local machine. This includes all files, commit history, and branches. It's the primary way to start working with an existing project.

Basic Cloning

Clone a repository
git clone https://github.com/username/repository-name.git

This command creates a directory with the repository name and downloads everything inside.

Cloning to a Specific Directory

Clone to custom directory
git clone https://github.com/username/repository-name.git my-project

This creates the repository in a folder called my-project instead of the default repository name.

tip

You can find the clone URL on any GitHub repository page by clicking the green "Code" button. You can use either HTTPS or SSH URLs depending on your authentication setup.

Understanding Forking

Forking creates a copy of someone else's repository under your GitHub account. This is essential for contributing to open-source projects or when you want to experiment with changes without affecting the original project.

How to Fork

  1. Navigate to the repository on GitHub
  2. Click the "Fork" button in the top-right corner
  3. GitHub creates a copy under your account

After forking, you'll typically clone your forked version to work on it locally:

Clone your forked repository
git clone https://github.com/your-username/repository-name.git

Cloning vs Forking: When to Use Each

  • You have direct access to the repository
  • You're starting work on a team project
  • You want to run or examine code locally
  • You're setting up a development environment

Working with Forked Repositories

Setting Up Remote Relationships

After cloning your fork, it's good practice to connect to the original repository (often called "upstream"):

Add upstream remote
git remote add upstream https://github.com/original-owner/repository-name.git

This allows you to sync with changes from the original project:

Fetch updates from upstream
git fetch upstream
git merge upstream/main

The Fork and Pull Workflow

This is the standard workflow for contributing to open-source projects:

  1. Fork the repository on GitHub
  2. Clone your fork locally
  3. Create a branch for your changes
  4. Make and commit your changes
  5. Push to your fork
  6. Create a Pull Request to the original repository
Complete fork workflow example
# Clone your fork
git clone https://github.com/your-username/project.git
cd project

# Add upstream remote
git remote add upstream https://github.com/original-owner/project.git

# Create feature branch
git checkout -b new-feature

# Make changes, then commit
git add .
git commit -m "Add new feature"

# Push to your fork
git push origin new-feature
note

After pushing to your fork, visit GitHub to create a pull request. Your fork's branch will be compared against the original repository's main branch.

Common Pitfalls

  • Forgetting to set upstream: Without connecting to the original repository, your fork can become outdated
  • Working directly on main: Always create feature branches, even in your own forks
  • Confusing clone and fork URLs: Make sure you're cloning from your fork, not the original, if you plan to contribute
  • Not pulling latest changes: Regularly sync your local clone with upstream changes to avoid merge conflicts
  • Large repository issues: Some repositories are very large; consider using --depth 1 for a shallow clone if you only need recent history

Summary

Cloning and forking are fundamental Git operations that serve different purposes. Cloning brings a remote repository to your local machine, while forking creates your own copy of someone else's repository on GitHub. Remember:

  • Clone for direct access and team collaboration
  • Fork for contributing to others' projects or creating derivatives
  • Always set up upstream remotes for forks to stay synchronized
  • Use the fork and pull workflow for open-source contributions

Quiz

Cloning vs Forking – Quick Check

What is the primary difference between cloning and forking?

Question 1/4