Forks and contributing

Topic 2 of 4 in Team workflows5 min readBeginnerFree course

Overview

On your own repositories and your team’s, you push branches directly. But you can’t push to a project you don’t belong to — the React or Node.js repositories, say. Open-source contribution works through forks: your own copy of the project, where you can push, and from which you can open a pull request back to the original.

Contributing to someone else’s project. upstream, github.com/nodejs/node, is the original: you can read it, not push. Step 1: fork it on GitHub, creating origin, your fork at github.com/asha/node, which you can push to. Step 2: clone your fork to your computer. Step 3: push your branch to your fork. Step 4: open a pull request from your fork to upstream. Step 5: fetch from upstream to stay up to date, after git remote add upstream https://github.com/nodejs/node.git.

The setup

1. Fork

On the project’s GitHub page, click Fork. GitHub creates github.com/<you>/<project> — a full copy under your account.

2. Clone your fork

Terminal
git clone git@github.com:asha/node.git
cd node

This repository’s origin is your fork.

3. Add the original as upstream

Terminal
git remote add upstream https://github.com/nodejs/node.git
git remote -v
Text
origin    git@github.com:asha/node.git (fetch)
origin    git@github.com:asha/node.git (push)
upstream  https://github.com/nodejs/node.git (fetch)
upstream  https://github.com/nodejs/node.git (push)

gh repo fork nodejs/node --clone does all three steps at once and names the remotes the same way.

Keeping your fork in sync

The original project keeps moving. Your fork doesn’t update itself. Before starting new work:

Terminal
git fetch upstream
git switch main
git merge --ff-only upstream/main
git push origin main

Because you never commit to your fork’s main, this is always a fast-forward. (GitHub’s Sync fork button and gh repo sync do the same on the server.)

Making a contribution

Terminal
git switch -c docs/fix-install-typo        # always branch from an up-to-date main
# …make the change, commit…
git push -u origin docs/fix-install-typo   # push to YOUR fork
gh pr create --repo nodejs/node --base main

On the website, GitHub offers “Compare & pull request” on your fork; check that the base repository is the original project and the head repository is your fork.

If upstream moves on while your PR is open, update your branch:

Terminal
git fetch upstream
git rebase upstream/main
git push --force-with-lease

Maintainers can usually push small fixes to your PR branch themselves (“Allow edits by maintainers” is on by default), which saves a round trip.

Being a good contributor

  • Read CONTRIBUTING.md (and the code of conduct). It tells you how the project wants issues, branches, commit messages, tests and PRs.
  • Discuss big changes first. Open or comment on an issue before writing a large feature — the maintainers may have plans, or reasons not to.
  • Start small. Documentation fixes and issues labelled good first issue are ideal first contributions.
  • One change per PR, with tests, following the project’s style and formatter.
  • Write a clear description linking the issue (Fixes #123).
  • Be patient and polite. Maintainers are often volunteers. If a PR goes quiet for a couple of weeks, a friendly ping is fine.
  • Sign what’s required. Some projects need a CLA (contributor licence agreement) or a Signed-off-by: line in each commit (git commit -s) — the PR’s checks will tell you.

Forks inside companies

Most companies don’t use forks internally — team members have write access and push branches to the shared repository. Forks appear when you contribute to repositories you don’t own, or when an organisation wants to keep a long-lived modified copy of an open-source project.

The two-remote habit: origin is yours, upstream is theirs. Fetch from upstream, push to origin, open PRs back to upstream.

Exercise

Contribute through a fork

Simulate open-source contribution with local bare repositories. upstream.git is the original project; you create your fork fork.git (on GitHub this is the Fork button). Clone your fork, add the original as the upstream remote, and create a branch docs/fix-install-typo with a fix. Meanwhile the maintainers push a new commit to upstream. Before opening your PR: sync your fork’s main with upstream, rebase your branch on it, and push the branch to your fork. Check that your branch sits directly on top of upstream’s latest commit.

Terminal
# The original project
git init -q --bare -b main upstream.git
git clone -q upstream.git maintainer 2>/dev/null && cd maintainer
printf "# Tool\n\nInstal with: npm i tool\n" > README.md && git add . && git commit -qm "Initial release" && git push -qu origin main
cd ..

# 1. "Fork" it (on GitHub: the Fork button / gh repo fork)
git clone -q --bare upstream.git fork.git

# 2. Clone your fork and add upstream

# 3. Branch and fix the typo

# Meanwhile, the maintainers push something new
(cd maintainer && echo "MIT" > LICENSE && git add . && git commit -qm "Add licence" && git push -q)

# 4. Sync your fork's main with upstream, rebase your branch, push it to your fork

Summary

  • A fork is your own copy of someone else’s repository on GitHub; you push there and open PRs back to the original.
  • Convention: origin is your fork, upstream is the original project.
  • Sync often: fetch upstream, fast-forward your main, push it to your fork (or use Sync fork / gh repo sync).
  • Always branch from an up-to-date main, and never commit directly to your fork’s main.
  • Read CONTRIBUTING.md first, keep PRs small, and follow the project’s conventions.