← Back to Git Course | Chapter 7: Collaboration | Lesson 3 of 6

Git Fork & Contribute

A fork is like copying a friend's recipe book to your own shelf so you can try changes freely, and then offering your best ideas back to them.
Syntax
bash
git clone https://github.com/your_name/repo.git
git remote add upstream https://github.com/original_owner/repo.git
git fetch upstream

What is a Fork?

A fork is a full copy of someone else's repository under your own account, including its entire history, that you're free to modify however you like without needing any permission on the original. It's the standard entry point for contributing to a project you don't have push access to.

Example: What is a Fork?

bash
git clone https://github.com/yourname/repo.git

Adding the Upstream Remote

By default your fork only knows about itself — adding the original repository as a remote named upstream (git remote add upstream <url>) gives you a way to fetch new commits from the project you forked from, which your fork doesn't get automatically just by existing.

Example: Adding the Upstream Remote

bash
git remote add upstream https://github.com/original/repo.git

Syncing your Fork

Since your fork is a snapshot from when you created it, the original project keeps moving without you unless you deliberately sync — fetching from upstream and merging (or rebasing) those changes into your local branch keeps your fork from drifting too far out of date to merge back cleanly later.

Example: Syncing your Fork

bash
git fetch upstream
git checkout main
git merge upstream/main

Pushing Changes to your Fork

Your fork on the hosting platform is typically the origin remote, so after committing locally you push to origin (your fork) exactly as you would with any repository you own — this is what makes your branch visible and available for opening a pull request.

Example: Pushing Changes to your Fork

bash
git push origin feature-x

Opening a Pull Request from a Fork

With your branch pushed to your fork, opening a pull request on the hosting platform's website targets the original repository as the destination and your fork's branch as the source — this is the actual proposal that a maintainer reviews and merges.

Example: Opening a Pull Request from a Fork

bash
git push origin feature-x
# then open a pull request from yourname:feature-x to original:main
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
Common Mistakes
  1. Cloning the original repository instead of your fork, so you cannot push your branch.
  2. Working directly on your fork's main branch, which makes syncing with upstream messy; use a feature branch.
  3. Forgetting to add the upstream remote, so git fetch upstream fails and your fork falls behind.
🔒

Chapter Quiz — Complete all 6 topics to unlock

0/6 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.