What Is a Fork on GitHub? How to Fork a Repo and Contribute

A GitHub fork is your own copy of someone else's repository. Learn how it differs from a clone, how to fork, open a pull request, and keep your fork in sync.

What Is a Fork on GitHub? How to Fork a Repo and Contribute

A fork on GitHub is a copy of someone else’s repository that lives in your own GitHub account. It contains all of the original code, commit history, and branches, but it is entirely under your control, so you can change anything you like without affecting the original project. If you later want your changes to become part of the original project, you send its owner a pull request.

This mechanism is what allows open source projects to be developed by thousands of people who have never met. The project owner does not need to give every contributor write access, because everyone works in their own fork and only the changes that are approved end up being merged into the main repository.

When you need a fork

There are two common situations. The first is when you want to contribute to someone else’s project, for example to fix a bug you ran into, add a small feature, or correct a typo in the documentation. Since you are not allowed to change that repository directly, you make your changes in a fork and propose them through a pull request.

The second is when you want to use someone else’s project as the starting point for your own. Many developers fork a template or starter project and then take it in a different direction. In that case your fork may never send a pull request back to the original, which is perfectly fine as long as the project’s license allows it.

Fork vs. clone

These two terms are often confused, even though they happen in different places. A fork happens on GitHub’s servers, when GitHub creates a copy of someone else’s repository under your account. A clone happens on your computer, when Git downloads a repository from GitHub so you can work on it locally.

In a typical contribution workflow you use both, one after the other. You fork first so that you have a copy on GitHub that you are allowed to change, and then you clone that fork so you can edit it on your computer. If you clone the original repository directly without forking, you can still change the code locally, but you will not be able to push, because you do not have permission to write to that repository.

How to fork a repository on GitHub

For practice, you can use the octocat/Spoon-Knife repository, which GitHub provides specifically so people can try forking and pull requests without any risk of breaking a real project.

  1. Open the repository you want to fork and click the Fork button near the top right of the page.
  2. GitHub shows a Create a new fork page. Choose your account as the owner, keep the repository name as it is, and leave Copy the main branch only checked unless you need the other branches.
  3. Click Create fork and wait a few seconds while GitHub makes the copy.

When it is done, you are taken to the new repository at github.com/your-username/Spoon-Knife, and under its name a small line reading “forked from octocat/Spoon-Knife” shows where the copy came from.

Clone your fork to your computer

Your fork only exists on GitHub so far, so the next step is to download it. On the fork’s page, click the Code button, copy the HTTPS address, and run the following commands in a terminal, replacing your-username with your own account name.

git clone https://github.com/your-username/Spoon-Knife.git
cd Spoon-Knife

A step beginners often skip is connecting this local copy to the original repository. The clone only knows about your fork, which it calls origin, so you need to add the original repository as a second remote, usually named upstream. You will use this remote later to pull in the latest changes from the project owner.

git remote add upstream https://github.com/octocat/Spoon-Knife.git
git remote -v

If you prefer a graphical tool, GitHub Desktop can clone the fork through File > Clone repository. It asks whether you plan to contribute to the parent project and then sets up the upstream remote for you.

Make changes and push them to your fork

Before you change any code, get into the habit of creating a new branch. A separate branch keeps your pull request tidy and leaves the main branch of your fork clean, which makes it much easier to keep in sync with the original project.

git switch -c update-homepage

Now edit the files you want to change in your favourite editor, for example by adding your name to index.html in the Spoon-Knife repository. When you are done, record the change as a commit and push the branch to your fork on GitHub.

git add index.html
git commit -m "Add my name to the homepage"
git push -u origin update-homepage

This push sends the branch to origin, which is your own fork, not the original repository. That is exactly what you want, because your changes only reach the original project once its owner accepts your pull request.

Open a pull request

After pushing, open your fork on GitHub. You will usually see a yellow banner saying your branch was recently pushed, with a Compare & pull request button. Click it, and GitHub opens a page comparing your branch with the main branch of the original repository.

Give the pull request a title that briefly describes the change, then write a description that answers three questions: what problem you are fixing, how you fixed it, and how the maintainer can check that the change works. A clear description makes a real difference, because open source maintainers often receive many pull requests and respond faster to the ones they can understand quickly. Then click Create pull request.

A pull request is not the end of the process but the start of a conversation. The maintainer may ask you to rename a variable, add a test, or split the change into smaller parts. You simply fix the code on the same branch, commit, and push again, and the pull request updates automatically without having to be recreated.

Keep your fork in sync with the original

The original repository keeps moving after you fork it, while your fork stays exactly as it was. If you leave it too long, your fork falls far behind and your next pull request becomes much more likely to run into conflicts.

The easiest fix is to open your fork on GitHub and click Sync fork, then Update branch, and afterwards run git pull on your computer so the local copy is updated too. If you prefer the terminal, fetch the changes from upstream and merge them into your main branch.

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

It is a good habit to sync like this every time you start a new change, so that each new branch is created from the most recent code.

Fork faster with the GitHub CLI

If you contribute often, the GitHub CLI (gh) can fork, clone, and set up the upstream remote in a single command. After signing in with gh auth login, run the following.

gh repo fork octocat/Spoon-Knife --clone

From inside the project folder, you can also open a pull request straight from the terminal with gh pr create, which asks for the title and description interactively.

Frequently asked questions

What does fork mean on GitHub, in simple terms?

A fork is a GitHub feature that copies someone else’s repository into your own account. You are free to change that copy and, if you want, propose your changes to the original repository through a pull request.

Does the owner know when someone forks their repository?

Yes. The number of forks is shown on the repository page, and the owner can see the list of accounts that forked it. Forking is a normal part of GitHub, so you do not need to ask permission before forking a public repository.

Do changes in my fork automatically go to the original project?

No. Changes in your fork stay in your account until you open a pull request and the project owner approves and merges it.

Can I fork a private repository?

A private repository can only be forked by people who have been given access to it, and the fork stays private. A public repository can be forked by anyone with a GitHub account.

What is the difference between a fork and a branch?

A branch is a line of development inside the same repository, and only people with write access to that repository can create one there. A fork creates a new repository in your account, which makes it the right choice for contributing to projects that do not give you write access.

If you want your forked project, or one of your own, to be easier for others to understand, continue with how to document your project on GitHub with a README and a wiki.

Advertisement
Tips and TricksGit
Building a product that has to stay reliable? I help founders and product teams scope, build and scale web and mobile products, with testing built in from day one.
Book a call

Keep reading

All articles