Forks, issues and open source
30 min · Beginner
Need help? Ask a tutor on WhatsApp (opens in a new tab)Showing steps for Windows. They are marked “On Windows” below.
Most projects on GitHub do not let you push to them. A fork gives you your own copy on GitHub that you can push to; a pull request from it asks the original project to take your change. This is how anyone can contribute to open source.
Fork: your own copy on GitHub
A fork is your own copy of someone else's repository, made on GitHub under your account. You can push to your fork freely, and send changes back to the original project with a pull request.
The college coding club keeps code-club/practice-exercises, a set of practice exercises. Priya is not a collaborator, so a push to it fails with Permission to code-club/practice-exercises.git denied.
She clicks Fork → Create fork on the club's repository. GitHub makes priya-dev/practice-exercises, marked "forked from code-club/practice-exercises". A clone, by contrast, is a copy on her laptop.
Clone your fork and add upstream
upstream is the usual name for a second remote that points to the original repository you forked. You fetch the project's new work from upstream and push your own work to origin, your fork.
Priya clones her fork, as in Clone, push, pull and fetch (opens in a new tab), so origin is the fork. Then she adds the club's repository under a second name, upstream.
Example 1 · Add the original as upstream
Runs in: Your terminal
PS C:\Users\Priya\practice-exercises> git remote add upstream https://github.com/code-club/practice-exercises.gitPS C:\Users\Priya\practice-exercises> git remote -vOutputorigin https://github.com/priya-dev/practice-exercises.git (fetch) origin https://github.com/priya-dev/practice-exercises.git (push) upstream https://github.com/code-club/practice-exercises.git (fetch) upstream https://github.com/code-club/practice-exercises.git (push)
What this code does
originis Priya's fork, set bygit clone: she can push there.upstreamis the club's repository. She only fetches from it; a push there would be refused.- Two remotes, two names: always check which one a command uses.
Contribute to open source
Open source software is software whose source code is public, with a licence that lets anyone use, study, change and share it. People around the world improve it through forks and pull requests.
First read the project's README.md and CONTRIBUTING.md: they say how the maintainers want changes. Then work as in your own repository: a branch, a commit, a push to origin.
Example 2 · A branch for the contribution, pushed to the fork
Runs in: Your terminal
PS C:\Users\Priya\practice-exercises> git switch -c add-leap-yearPS C:\Users\Priya\practice-exercises> git add exercises/leap-year.mdPS C:\Users\Priya\practice-exercises> git commit -m "Add leap year exercise"PS C:\Users\Priya\practice-exercises> git push -u origin add-leap-yearOutputSwitched to a new branch 'add-leap-year' [add-leap-year c4fc69b] Add leap year exercise 1 file changed, 1 insertion(+) create mode 100644 exercises/leap-year.md Enumerating objects: 6, done. Counting objects: 100% (6/6), done. Delta compression using up to 12 threads Compressing objects: 100% (3/3), done. Writing objects: 100% (4/4), 396 bytes | 198.00 KiB/s, done. Total 4 (delta 0), reused 0 (delta 0), pack-reused 0 (from 0) remote: remote: Create a pull request for 'add-leap-year' on GitHub by visiting: remote: https://github.com/priya-dev/practice-exercises/pull/new/add-leap-year remote: To https://github.com/priya-dev/practice-exercises.git * [new branch] add-leap-year -> add-leap-year branch 'add-leap-year' set up to track 'origin/add-leap-year'.
What this code does
- The new exercise goes on its own branch,
add-leap-year, never onmain. git push -u originsends it to Priya's fork; the club's repository is not touched.- The
remote:lines give the link to open the pull request.
On GitHub, Compare & pull request now offers a pull request across forks: base repository code-club/practice-exercises, base main ← head repository priya-dev/practice-exercises, compare add-leap-year. A maintainer (a person who looks after the project) reviews and merges it, exactly as in the Pull requests lesson.
Keep your fork up to date
Your fork does not update itself. While Priya waits, the club adds a sum-of-digits exercise, and her fork falls behind. GitHub's Sync fork → Update branch button fixes the fork on GitHub; from the terminal, the same job is four commands.
On Windows
In PowerShell, in practice-exercises:
PS C:\Users\Priya\practice-exercises> git fetch upstreamPS C:\Users\Priya\practice-exercises> git switch mainPS C:\Users\Priya\practice-exercises> git merge upstream/mainPS C:\Users\Priya\practice-exercises> git push origin mainOutputremote: Enumerating objects: 6, done. remote: Counting objects: 100% (6/6), done. remote: Compressing objects: 100% (3/3), done. remote: Total 4 (delta 0), reused 0 (delta 0), pack-reused 0 (from 0) From https://github.com/code-club/practice-exercises * [new branch] main -> upstream/main Switched to branch 'main' Your branch is up to date with 'origin/main'. Updating 2799538..b0273a1 Fast-forward exercises/sum-of-digits.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 exercises/sum-of-digits.md Enumerating objects: 6, done. Counting objects: 100% (6/6), done. Delta compression using up to 12 threads Compressing objects: 100% (3/3), done. Writing objects: 100% (4/4), 405 bytes | 202.00 KiB/s, done. Total 4 (delta 0), reused 0 (delta 0), pack-reused 0 (from 0) To https://github.com/priya-dev/practice-exercises.git 2799538..b0273a1 main -> main
git fetch upstreamdownloaded the club's new commit intoupstream/main. Nothing on GitHub changed.git merge upstream/mainfast-forwarded Priya'smain: she never commits onmain, so it is always a clean copy.git push origin mainsends the same commit to her fork, so the fork on GitHub is up to date too.
main. Keep it a clean copy of the original and do every change on a branch, so syncing is always a fast-forward.Issues: report a bug or plan a task
An issue is a numbered note in a repository's Issues tab that reports a bug, asks for a feature or plans a task. Each issue has its own page for discussion and can be closed by a pull request.
After the GST pull request, Rahul opens bill.txt and sees that the README's sample bill no longer matches it. He opens Issues → New issue on shop-bill and writes steps, expected and actual result, so Priya can see the bug herself.
Example 3 · A clear bug report
Title: README sample bill is out of date**Steps**1. Print the file with `cat bill.txt`2. Compare with **Sample bill** in README.md**Expected:** the same lines as the README**Actual:** bill.txt also has `GST 5%: Rs. 4.30`, and the total is `Rs. 90.30`, not `Rs. 86`README sample bill is out of date <- issue title, number #3, label Open Steps (bold) 1. Print the file with cat bill.txt 2. Compare with Sample bill in README.md Expected: the same lines as the README Actual: bill.txt also has GST 5%: Rs. 4.30, and the total is Rs. 90.30, not Rs. 86
What this code does
- The title names the problem in a few words, so it is easy to find in the list.
- Steps let anyone see the bug on their own laptop; expected vs actual shows exactly what is wrong.
- The issue box takes Markdown, as in the README lesson.
GitHub gives it a number, #3. When Priya fixes it, she writes Fixes #3 in the pull request description. When that pull request is merged, GitHub closes issue #3 by itself. Closes #3 and Resolves #3 work the same way; a plain #3 only links the two pages.
Contributing politely
Maintainers are often busy volunteers, so a good contribution is easy to accept. Many projects explain their rules in CONTRIBUTING.md.
- Read
CONTRIBUTING.mdand the README before you start. - Check the open issues first, and say that you are working on one.
- Keep the change small, and run the project's tests before you push.
- Accept feedback calmly: a request for changes is normal, not an insult.
Next step
Check your understanding
9 quick questions