Forks and contributing
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.
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
git clone git@github.com:asha/node.git
cd nodeThis repository’s origin is your fork.
3. Add the original as upstream
git remote add upstream https://github.com/nodejs/node.git
git remote -vorigin 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:
git fetch upstream
git switch main
git merge --ff-only upstream/main
git push origin mainBecause 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
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 mainOn 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:
git fetch upstream
git rebase upstream/main
git push --force-with-leaseMaintainers 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 issueare 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:
originis yours,upstreamis theirs. Fetch from upstream, push to origin, open PRs back to upstream.
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.
# 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# 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
git clone fork.git my-copy && cd my-copy
git remote add upstream ../upstream.git
git remote -v # origin = your fork, upstream = the original
# 3. Branch and fix the typo
git switch -c docs/fix-install-typo
sed -i.bak 's/Instal with/Install with/' README.md && rm README.md.bak
git commit -qam "Fix typo in installation instructions"
cd ..
# 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
cd my-copy
git fetch upstream
git switch main
git merge --ff-only upstream/main # on GitHub: "Sync fork" or gh repo sync
git push origin main
git switch docs/fix-install-typo
git rebase main
git push -u origin docs/fix-install-typo
# Then: gh pr create --repo <owner>/<project> --base main
git log --oneline --graph -3Summary
- A fork is your own copy of someone else’s repository on GitHub; you push there and open PRs back to the original.
- Convention:
originis your fork,upstreamis 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.