Sub topic 16
Contributing through forks
This chapter assumes you can branch, commit, push, and open a pull request inside a repository you can push to. Here you apply those skills to a repository you do not own.
Why forks exist
You usually cannot push branches to someone else's repository. A fork solves this. A fork is your own copy of a repository, stored under your account on GitHub. You push to the fork, then open a cross-repository pull request. In that request, the base is a branch in the original repository (called upstream). The head is a branch in your fork.
Setting up
Fork the repository with the Fork button, then clone your fork. Add the original repository as a second remote named upstream.
git clone https://github.com/YOUR-USER/project.git
cd project
git remote add upstream https://github.com/ORIGINAL-OWNER/project.git
git remote -vorigin points at your fork. upstream points at the original. With the GitHub CLI, gh repo fork --clone does the fork, clone, and remote setup in one step.
Making and proposing a change
Always work on a new branch, not on your fork's main. Then push the branch to origin.
git fetch upstream
git switch -c fix-typo-in-readme upstream/main
# edit files, then:
git add .
git commit -m "Fix typo in README"
git push -u origin fix-typo-in-readmeAfter the push, GitHub shows a banner offering to open a pull request. Check that the base repository is the upstream one and that the base branch is the one maintainers expect. Then write the description as you would for any pull request. Also read CONTRIBUTING.md first, because many projects have specific rules for commits, tests, and templates.
Keeping your fork current
The upstream repository moves while your pull request is open. If it picks up conflicting changes, update your branch and resolve the conflicts locally.
git fetch upstream
git rebase upstream/main
git push --force-with-lease origin fix-typo-in-readmeRebasing rewrites your branch's commits, so the push needs a force. --force-with-lease refuses to overwrite commits on the fork that you have not seen. If you prefer not to rewrite history, run git merge upstream/main and push normally.
Things that differ from same-repository pull requests
- Maintainers can usually push fixes to your branch. This depends on the "Allow edits by maintainers" checkbox, which is on by default.
- Workflows triggered by
pull_requestfrom a fork run with a read-only token and no access to repository secrets. This protects the upstream repository from untrusted code. First-time contributors may also need a maintainer to approve the workflow run. - Your fork's
maindoes not update itself. Sync it withgit push origin upstream/main:main, or use Sync fork on GitHub.
Exercise
You opened a pull request from your fork. A maintainer says it conflicts with upstream main. Which commands bring your branch up to date, and why is --force-with-lease used after a rebase?
Show answer
Run git fetch upstream, then git rebase upstream/main, resolve any conflicts, and push with git push --force-with-lease origin <branch>. The rebase rewrites your commits, so a plain push is rejected. --force-with-lease allows the overwrite only if the remote branch is still what you last fetched. That protects against erasing commits someone else added, such as a maintainer who pushed a fix to your branch.