Forking Repositories and Cloning to Local Machine (git clone)
Forking Repositories and Cloning to Local Machine (git clone)
Two of the most frequent ways to obtain code on GitHub are cloning and forking. While both involve copying a repository, they serve distinct roles in open-source development and enterprise collaboration.
1. Cloning a Repository: git clone
Cloning downloads an entire existing Git repository from a remote server (like GitHub) to your local computer:
- It downloads all files, directories, commits, and branches.
- It automatically configures the remote alias named
originpointing back to the cloned URL. - It automatically checks out the default branch (
main).
Cloning into a Custom Directory Name:
By default, Git creates a folder named after the repository (react). You can specify a custom directory name as the last argument:
Shallow Cloning (--depth 1):
For massive repositories (like Linux or Chromium) with 15 years of history, downloading every commit can take hours and gigabytes of disk space. A shallow clone downloads only the latest snapshot:
2. Forking a Repository
What if you want to contribute to an open-source project (like vuejs/core or django/django), but you are not an employee and do not have write access to push commits to their repository? This is where Forking comes in!
What is a Fork?
- A Fork is a complete copy of a repository that lives entirely on your personal GitHub account.
- Because it lives under your account, you have full administrative write and push access to it.
- You make your changes, commit them to your fork, and then submit a Pull Request (PR) back to the original author's repository.
How to Fork a Repo:
- 1Navigate to the public repository on GitHub (e.g.,
github.com/msk-institute/python-cheatsheet). - 2In the top-right corner of the page, click the Fork button.
- 3Select your personal account and click Create fork.
- 4GitHub duplicates the repository under your username in seconds!
3. Cloning vs. Forking: Quick Summary
| Feature | git clone | GitHub Fork |
|---|---|---|
| Where it happens | On your local computer disk | On GitHub's cloud servers |
| Tool Used | Git command line (git clone) | GitHub Web UI button |
| Permissions needed | Read access to the repository | Read access to the public repository |
| Primary purpose | Get files onto your laptop to run and develop | Create a personal cloud copy to propose contributions to upstream repos |
Practice Quiz
git clone do?git clone <url>, what name does Git automatically assign to the remote bookmark?Creating and Managing Pull Requests (PRs) on GitHub
Continue learning with hands-on practice, examples, and exercises in the upcoming topic.
Related Lessons
Practice Quiz
Test your understanding of this lesson with 5 questions. Each question has one correct answer.