Himanshu SharmaSoftware engineer
Let’s talk

Git and GitHub · Lesson 18 of 30

Forks and code review on GitHub

Contribute to a project you cannot write to by forking it, keep the fork in sync with an upstream remote, and give and receive code review kindly.

13 minRemotes and GitHubUpdated 14 November 2026

Awaiting technical review. This lesson is readable but not yet listed in search engines.

By the end of this lessonExplain how forking works, sync a fork with the original repository using an upstream remote, and take part in code review constructively.

What you can do after this lesson

You will know how people contribute to projects where they have no write access, and how to keep their copy up to date. You will also have a practical sense of what good code review looks like from both sides. The Git commands were verified with local bare repositories playing the roles of the original project and the fork; GitHub's buttons are described in words only.

Why forks exist

Public projects cannot let every stranger push to them. A fork solves this: on GitHub, you press the fork button and the service makes a personal copy of the repository under your account. You have full write access to your copy. You then propose your changes back to the original with a pull request, and the project's maintainers decide whether to accept them.

A fork is a GitHub concept built on ordinary Git. It is a server-side clone that GitHub remembers is related to the original.

The two remotes

After forking, you clone your fork to your computer. That clone's remote is called origin, pointing at the fork. To follow the original project you add a second remote, conventionally named upstream:

bash
git remote -v
git remote add upstream ../origin.git
git remote -v
text
origin	/home/you/pr/fork.git (fetch)
origin	/home/you/pr/fork.git (push)
origin	/home/you/pr/fork.git (fetch)
origin	/home/you/pr/fork.git (push)
upstream	../origin.git (fetch)
upstream	../origin.git (push)

The first two lines are the state before adding; the next four are after. In a real case upstream would be the GitHub address of the original repository. In this local rehearsal ../origin.git plays that part.

The contribution flow

  1. Fork on GitHub, then clone your fork.
  2. Add the upstream remote.
  3. Create a topic branch from an up-to-date main.
  4. Commit, push the branch to your fork (git push -u origin my-branch).
  5. On GitHub, open a pull request from your fork's branch to the original's main.
  6. Respond to review by pushing more commits to the same branch.
  7. After it merges, sync your fork and delete the branch.

Read the project's CONTRIBUTING.md first if it has one. It states conventions, the licence under which contributions are accepted, and often asks you to open an issue before starting large work.

Keeping the fork in sync

The original moves on while you work. Bring its changes into your fork's main:

bash
git fetch upstream
git switch main
git merge upstream/main
git push origin main
text
From ../origin
 * [new branch]      fix-typo-in-readme -> upstream/fix-typo-in-readme
 * [new branch]      main               -> upstream/main
Already on 'main'
Updating 81a4abb..688e1b7
Fast-forward
 README.md | 2 ++
 news.txt  | 1 +
 2 files changed, 3 insertions(+)
 create mode 100644 news.txt
To /home/you/pr/fork.git
   81a4abb..688e1b7  main -> main

Hashes differ in your run. git merge upstream/main fast-forwarded because nobody committed directly to the fork's main. That is the reason for the rule: keep your fork's main as a clean mirror and do your work on branches. GitHub also offers a sync button for forks on its website, which does the equivalent. To update a topic branch that fell behind, either merge upstream/main into it or rebase it, if only you use that branch.

Code review: what it is for

Review is a second pair of eyes before code lands. Good reviews look for:

  • Correctness: does it do what it says, including odd inputs?
  • Clarity: could the next person understand it? Are names sensible?
  • Fit: does it match the project's style and structure?
  • Risk: security, performance in hot paths, and effects on other code.
  • Tests and docs: is the change covered and explained?

On GitHub, a reviewer can comment on individual lines, then submit a review marked as a comment, an approval, or a request for changes. Reviewers can also suggest an exact replacement that the author can accept with one click.

Reviewing kindly and usefully

  • Review the code, not the person. Say "this function" not "you".
  • Be specific and explain why. "This skips the last item when the list has one element" teaches; "this is wrong" does not.
  • Offer a direction where you can.
  • Mark preferences as such, for example "nit:" or "optional:", so the author knows what is blocking.
  • Ask questions when you are unsure. They are often the most useful comments.
  • Say what is good too.

Receiving a review

  • Assume good intent. Comments are about the change.
  • Reply to each comment: fix it, or explain why not.
  • Push fixes as new commits to the same branch; GitHub updates the PR.
  • Do not take it personally, and do not argue about taste at length. If two approaches are equally valid, follow the project's preference.
  • Say thank you. Reviewers give their time.

When a change you were asked to make is finished, mark the conversation resolved. If you tidy history before merging, agree with the reviewer first, because rewriting a PR branch while someone is mid-review makes their comments harder to follow.

Common mistakes

  • Committing directly to your fork's main. It then diverges from upstream and syncing needs a merge.
  • Forgetting to add upstream. Without it you have no easy way to get the original's changes.
  • Starting a huge change without discussion. Maintainers may not want it. Open an issue first.
  • Vague, harsh review comments. They slow everyone down.
  • Force-pushing over a branch under review. Use new commits unless asked otherwise.

Exercise

  1. Set up three local repositories: a bare original.git, a bare fork.git created with git clone --bare original.git fork.git, and a clone of the fork.
  2. Add original.git as upstream in your clone.
  3. Commit something new in a clone of the original and push. In your fork clone, fetch and merge upstream/main, then push to origin.
  4. Write three sample review comments on a function of your choice: one praise, one specific problem and one optional suggestion.
Show one possible answer
bash
git clone --bare original.git fork.git
git clone fork.git mine && cd mine
git remote add upstream ../original.git
git fetch upstream
git merge upstream/main
git push origin main

Sample comments: "Nice, the function name makes the intent obvious." "This loop skips the last element when the list has exactly one item; using <= should fix it." "nit: optional, you could extract the discount calculation into its own function."

Next: Writing commit messages people can use later.

Key points

  • A fork is your own copy of someone else's repository on GitHub, which you can push to and use to propose changes back.
  • Convention: origin is your fork, upstream is the original repository.
  • To sync, fetch upstream, merge or rebase its main into yours, and push to your fork.
  • Code review checks correctness, clarity and fit; the aim is a better change, not a verdict on the person.
  • Authors respond to comments with new commits and replies; reviewers explain reasons and separate must-fix from preferences.

Check yourself

1. Why would you fork a repository instead of cloning it directly?
2. In the usual convention, what is the upstream remote?
3. How do you bring new commits from the original project into your fork's main?
4. Which review comment is most helpful?