Forking a Repository

Updated · 10 min read
mediumGit 2.53gitforkupstreamopen-sourceinterview

A school library won't let you write in its books. But you can photocopy a chapter, mark your corrections on the copy, and hand the copy to the librarian with a note: "I think these pages have mistakes, here is what I'd change." The librarian decides whether to fix the real book.

A fork is that photocopy on a hosting platform: your own copy of someone else's repository, under your account, where you can push. You make your changes there, then open a pull request from your fork to the original project. The maintainers review it and decide whether to merge it. This is how almost all open-source contributions work, and many companies use it too, so that only a few people can push to the main repository.

In this lesson, you will contribute a change to a project through a fork, from the first click to keeping the fork up to date afterward.

Fork or Branch?

You have push access to the repository?Contribute with
Yes, you are a member of the teamA branch in the same repository, and a pull request ("Pull Requests and Merge Requests")
No, it is someone else's projectA fork, and a pull request from the fork
The fork workflow, start to finish

Step 1: Fork on the Platform

The school's official Fee Tracker belongs to Advitha's account. Navika can read it but not push to it. She forks it:

text
1. Open the repository and click Fork (top right).
2. Owner: your account. Keep the repository name.
3. Keep "Copy the main branch only" checked, unless you need other branches.
4. Click Create fork.

The platform copies the repository, with its history, to <your-account>/fee-tracker, and remembers that it is a fork of the original.

Step 2: Clone Your Fork and Add upstream

As in the previous lessons, folders stand in for the platform. school-fee-tracker.git is the original and navika-fee-tracker.git is Navika's fork, made with git clone --bare, which copies a repository the way the Fork button does:

Terminal
git init --bare --quiet school-fee-tracker.git
mkdir advitha navika
cd advitha
git init --quiet fee-tracker
cd fee-tracker
git config user.name "Advitha Anamaneni"
git config user.email "advitha@example.com"
echo 'Term 1 fee: $12,000' > fees.txt
git add fees.txt
git commit --quiet -m "Add the term fee"
git remote add origin ../../school-fee-tracker.git
git push --quiet -u origin main
cd ../..
git clone --bare --quiet school-fee-tracker.git navika-fee-tracker.git
cd navika
git clone --quiet ../navika-fee-tracker.git fee-tracker
cd fee-tracker
git remote add upstream ../../school-fee-tracker.git
git fetch --quiet upstream
git remote -v
Output
origin	/home/navika/projects/navika/../navika-fee-tracker.git (fetch)
origin	/home/navika/projects/navika/../navika-fee-tracker.git (push)
upstream	../../school-fee-tracker.git (fetch)
upstream	../../school-fee-tracker.git (push)

Always clone your fork, so origin is where you can push. Add the original as upstream, so you can fetch its newest work, as in "Working with Several Remotes".

Step 3: Make the Change on a Branch

Never work on main in a fork. Keep main as an exact copy of the original, and put each contribution on its own branch:

Terminal
git switch --quiet -c fix/late-fee
echo 'Late fee: $250' >> fees.txt
git commit --quiet -am "Add the missing late fee"
git push -u origin fix/late-fee
Output
To /home/navika/projects/navika/../navika-fee-tracker.git
 * [new branch]      fix/late-fee -> fix/late-fee
branch 'fix/late-fee' set up to track 'origin/fix/late-fee'.

The branch is on Navika's fork, not on the original.

Step 4: Open a Pull Request to the Original

The pull request goes across repositories: from a branch of the fork into main of the original:

text
1. Open your fork. A banner offers "Compare & pull request". Click it.
2. Check the top line:
   base repository: <owner>/fee-tracker, base: main
   head repository: <you>/fee-tracker, compare: fix/late-fee
3. Title and description. Check "Allow edits by maintainers"
   so they can fix small things for you.
4. Click Create pull request.

From here, everything works as in the previous lesson: the maintainers review, you push more commits to fix/late-fee on your fork, and the pull request updates.

Step 5: The Maintainer Merges

Advitha reviews and merges it. On the platform, that is the merge button. Here, her clone does the same: it fetches Navika's branch from the fork and merges it into the original's main:

Terminal
cd ../../advitha/fee-tracker
git fetch --quiet ../../navika-fee-tracker.git fix/late-fee
git merge --quiet --no-ff --no-edit -m "Merge pull request #1 from navika/fix/late-fee" FETCH_HEAD
git push --quiet
git log --oneline --graph
Output
*   5195d4f (HEAD -> main, origin/main) Merge pull request #1 from navika/fix/late-fee
|\
| * 80a79bc Add the missing late fee
|/
* e84c90d Add the term fee

git fetch <url> <branch> fetches a branch from a repository that isn't even a saved remote. The merge commit message mimics the one GitHub writes.

Step 6: Sync Your Fork

The original has moved on. Navika's fork doesn't update itself. She syncs it with the three steps from "Working with Several Remotes", and deletes her finished branch:

Terminal
cd ../../navika/fee-tracker
git switch --quiet main
git fetch --quiet upstream
git merge --ff-only upstream/main
git push --quiet origin main
git branch -d fix/late-fee
git push --quiet origin --delete fix/late-fee
Output
Updating e84c90d..5195d4f
Fast-forward
 fees.txt | 1 +
 1 file changed, 1 insertion(+)
Deleted branch fix/late-fee (was 80a79bc).

Her main, her fork and the original are now the same, and her contribution is in all three. The platforms also have a button for this, which updates the fork on the server (you still pull it locally afterward): Sync fork on GitHub, Update fork on GitLab, and a sync option on a Bitbucket fork that is behind.

Common Mistakes

Cloning the original instead of your fork. Your push is refused, because origin is the original. Fix it with git remote rename origin upstream and git remote add origin <your-fork-url>.

Committing on main in the fork. Your main then diverges from the original, and every sync needs merges. Move the commits to a branch (git branch fix/x, git reset --hard upstream/main on main).

Letting the fork fall months behind. Your pull request then conflicts with the original. Sync before you start each new branch.

Opening the pull request into your own fork's main. Check the base repository on the pull request page: it must be the original.

Forking when you are a team member. You already can push. A branch in the original is simpler.

Under the Hood

A fork is a server-side copy that the platform links to the original. On GitHub, forks even share storage with the original behind the scenes, which is how a fork of a huge project appears in seconds. To Git on your computer, a fork is just another repository URL: that is why origin, upstream and git fetch <url> are all you need.

The pull request lives in the original repository, and the platform makes the fork's branch available there: on GitHub as refs/pull/<n>/head in the original, as you saw in the previous lesson. That is how maintainers can fetch and test your branch without adding your fork as a remote.

Interview Corner

What is a fork?

A copy of a repository on the hosting platform, under your own account, linked to the original. You can push to it, and contribute back to the original through pull requests. It is a platform feature, not a Git command.

What is the difference between a fork and a clone?

A fork is a copy on the server under your account. A clone is a copy on your computer. You usually fork on the website, then clone your fork.

How do you contribute to an open-source project?

Fork it, clone your fork, add the original as upstream, create a branch, commit, push the branch to your fork, and open a pull request to the original. Respond to reviews by pushing more commits to the same branch.

How do you keep a fork up to date?

git fetch upstream, git switch main, git merge --ff-only upstream/main (or rebase), then git push origin main. Or use the platform's sync button and then pull.

Practice

Challenge: a second contribution. Advitha has meanwhile added the hostel fee to the original. Navika wants to contribute the exam fee. Sync main from upstream first, then create feature/exam-fee, commit Exam fee: $800, and print the one-line log of the last three commits.

What does Git print? Before you open the solution, decide whether Advitha's hostel fee commit is in the log, and where.

Show solution
Terminal
cd ../../advitha/fee-tracker
echo 'Hostel fee: $9,000' >> fees.txt
git commit --quiet -am "Add the hostel fee"
git push --quiet
cd ../../navika/fee-tracker
git fetch --quiet upstream
git merge --quiet --ff-only upstream/main
git push --quiet origin main
git switch --quiet -c feature/exam-fee
echo 'Exam fee: $800' >> fees.txt
git commit --quiet -am "Add the exam fee"
git log --oneline -3
Output
ce93350 (HEAD -> feature/exam-fee) Add the exam fee
d7571a9 (upstream/main, upstream/HEAD, origin/main, origin/HEAD, main) Add the hostel fee
5195d4f Merge pull request #1 from navika/fix/late-fee

The hostel fee commit is directly under the new exam fee commit, because Navika synced main before branching. Her pull request will contain only her own change, with no conflict with the original.

Checkpoint. Fork a public practice repository (many projects keep one for first-time contributors), clone your fork, add upstream, make a small change on a branch, and open a pull request to the original. Then sync your fork's main from upstream.

Quick check: Forking

4 questions
1When do you need a fork?
2After forking, which URL do you clone?
3Where does the pull request from your fork go?
4How do you keep your fork's main up to date?

Key Takeaways

Quick reference

CommandIt is used to
git clone <your-fork-url>Clone your fork
git remote add upstream <original-url>Connect the original project
git switch -c <branch>Start a contribution
git push -u origin <branch>Push it to your fork
git fetch upstream then git merge --ff-only upstream/mainSync main
git push origin mainUpdate your fork

Was this tutorial helpful?

Lesson 48 of 106