Fork: create your own server-side copy of someone else’s repository on a platform like GitHub

  • so you can make changes safely and submit them back with a pull request.

Fork is mainly a GitHub feature, not a basic local Git command.

When you fork a repository, GitHub creates your own copy of someone else’s repository under your account.

Use a fork when:

  • you do not have direct write access to the original repository
  • you want to make changes in your own copy first
  • you plan to open a Pull Request back to the original repository

Fork vs Clone

Fork:

  • happens on GitHub
  • creates a copy under your GitHub account

Clone:

  • happens on your computer
  • downloads a repository to your local machine

In a normal contribution flow, you usually:

  1. Fork on GitHub
  2. Clone your fork locally
  3. Make changes
  4. Push to your fork
  5. Open a Pull Request

1) Fork the repository on GitHub

  1. Open the original repository on GitHub.
  2. Click the Fork button.
  3. Choose your GitHub account as the destination.
  4. Wait for GitHub to create the fork.

After this, you will have a new repository like:

https://github.com/<your-username>/<repo-name>

2) Clone your fork to your computer

Copy the URL of your fork, then run:

git clone https://github.com/<your-username>/<repo-name>.git
cd <repo-name>

3) Check the remote repository

git remote -v

Usually, origin will point to your fork.

Example:

origin  https://github.com/<your-username>/<repo-name>.git

4) Add the original repository as upstream

This is recommended so you can pull later updates from the original project.

git remote add upstream https://github.com/<original-owner>/<repo-name>.git

Check again:

git remote -v

Now you will usually have:

  • origin = your fork
  • upstream = the original repository

5) Create a new branch for your work

git checkout -b my-change

Use a short branch name that describes your work.

Examples:

  • fix-typo
  • update-docs
  • add-fork-guide

6) Make changes, commit, and push

git status
git add .
git commit -m "Describe what you changed"
git push -u origin my-change

This pushes your branch to your fork on GitHub.

7) Open a Pull Request

After pushing, go to your fork on GitHub.

GitHub will usually show a button like Compare & pull request.

Open the Pull Request from:

  • your fork branch

to:

  • the original repository

Keep Your Fork Updated

If the original repository changes, you may want to sync your fork.

First fetch updates from upstream:

git fetch upstream

Then update your local main branch:

git checkout main
git merge upstream/main

Then push the updated main branch to your fork:

git push origin main

Note: some repositories use master instead of main.

Quick Version

# Fork on GitHub first
git clone https://github.com/<your-username>/<repo-name>.git
cd <repo-name>
git remote add upstream https://github.com/<original-owner>/<repo-name>.git
git checkout -b my-change
git add .
git commit -m "Describe what you changed"
git push -u origin my-change

Common Problems

I cloned the original repository instead of my fork

You may not be able to push if you do not have write access.

Check:

git remote -v

If origin points to the original repository, that is the problem.

I get a permission error when pushing

You are probably trying to push to the original repository instead of your fork.

Check:

git remote -v

Make sure:

  • origin points to your fork
  • upstream points to the original repository

My fork is behind the original repository

Sync from upstream:

git fetch upstream
git checkout main
git merge upstream/main
git push origin main