Git & GitHub ยท Chapter 30 of 42

Forking a Repository

A fork is your own personal copy of someone else's GitHub repository. Forking is common for contributing to open-source projects you don't have direct write access to.

After forking, you clone your fork, make changes on a branch, push to your fork, and then open a pull request from your fork back to the original repository.

Syntax
git clone https://github.com/your-username/forked-repo.git
git remote add upstream https://github.com/original-owner/repo.git

Creating a fork

Click the 'Fork' button on a GitHub repository page to create a full copy under your own account, which you can clone and modify freely.

Contributing back via pull request

After pushing changes to your fork, open a pull request comparing your fork's branch to the original repository's main branch, requesting your changes be merged upstream.

Example 1 (bash)
git clone https://github.com/alexsmith/awesome-project.git
git remote add upstream https://github.com/original-owner/awesome-project.git

Clones your fork and adds the original repository as an 'upstream' remote to track its updates.

Example 2 (bash)
git fetch upstream
git merge upstream/main
Output
Fast-forward
 README.md | 4 ++--

Fetches the latest changes from the original project and merges them into your fork's local main branch.

Key points

  • A fork is your own copy of someone else's repository.
  • Forking is common for contributing to open-source projects.
  • 'upstream' is the conventional name for the original repository's remote.
  • Changes flow back via a pull request from your fork to the original repo.
๐Ÿ’ก Note: Keep your fork updated by regularly fetching and merging from the upstream remote to avoid falling far behind.

๐Ÿ“ Quick Quiz

1. What is a fork on GitHub?

2. What is the conventional name for the original repo's remote in a fork?

3. How do you contribute changes from a fork back to the original project?