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
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
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.
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
- Navigate to the repository on GitHub
- Click the "Fork" button in the top-right corner
- GitHub creates a copy under your account
After forking, you'll typically clone your forked version to work on it locally:
git clone https://github.com/your-username/repository-name.git
Cloning vs Forking: When to Use Each
- Use Cloning When
- Use Forking When
- 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
- You want to contribute to someone else's project
- You don't have write access to the original repository
- You want to experiment with changes independently
- You're creating your own version of a project
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"):
git remote add upstream https://github.com/original-owner/repository-name.git
This allows you to sync with changes from the original project:
git fetch upstream
git merge upstream/main
The Fork and Pull Workflow
This is the standard workflow for contributing to open-source projects:
- Fork the repository on GitHub
- Clone your fork locally
- Create a branch for your changes
- Make and commit your changes
- Push to your fork
- Create a Pull Request to the original repository
# 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
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 1for 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?