Sub topic 1
How Forks, Clones, and Pull Requests Fit Together
Prerequisites
This chapter assumes you have:
- A GitHub account.
- Git installed and configured with your name and email.
- Working knowledge of
git add,git commit,git status, andgit log. - Comfort using a terminal.
The problem forks solve
Most open source projects do not give write access to strangers. If you could push straight to someone else's repository, anyone could break it. Yet the project still wants your fixes.
The fork-and-pull-request workflow solves this. You make your own copy, change that copy, and then ask the maintainers to take your changes.
Three terms, three places
Three terms look similar but name different things.
- A fork is a copy of a repository that lives in your GitHub account. You create it on GitHub, and you have full write access to it.
- A clone is a copy of a repository on your own computer. Cloning downloads the full history so you can work locally.
- A branch is a movable pointer to a line of commits inside one repository. Branches let you keep separate lines of work apart.
A fork and a clone are copies in different places. A branch is not a copy of the repository. It lives inside a repository, whether that is the fork or the clone.
| Term | Where it lives | Created by |
|---|---|---|
| Upstream repository | The original owner's GitHub account | The project |
| Fork | Your GitHub account | Clicking Fork on GitHub |
| Clone | Your computer | `git clone` |
| Branch | Inside a repository | `git switch -c` |
How the pieces connect
You work with three repositories. The upstream repository is the original project. Your fork is your public copy of it. Your clone is the working copy on your machine.
In your clone, Git names remote repositories with short labels. By convention, origin is your fork, and upstream is the original project. Chapter 5 sets these up in detail. For now, the important point is the direction of each arrow in the diagram below.
Notice that you never push to upstream. You push to your fork, then ask upstream to pull from it. That request is why the feature is called a pull request.
What a pull request is
A pull request (PR) is a proposal on GitHub to merge the commits of one branch into another branch. In this workflow, the source is a branch in your fork and the target is a branch, usually main, in upstream.
A pull request is not a Git feature. It is a GitHub feature built on top of Git. It adds a page where maintainers can read your diff, comment on lines, request changes, and merge when satisfied.
A pull request stays linked to your branch. If you push more commits to that branch, the pull request updates automatically. This is how you respond to review feedback later.
The workflow at a glance
The whole cycle has these steps, and the rest of this tutorial covers each one.
- Read the project's contribution guidelines.
- Fork the repository.
- Clone your fork.
- Add
upstreamas a second remote. - Create a feature branch.
- Commit a focused change.
- Push the branch to your fork.
- Open a pull request.
- Respond to review feedback.
- Sync your fork and clean up after the merge.
Common mistakes to avoid
- Committing on
mainin your fork. Your fork'smainshould track upstream. Work on a feature branch instead, so syncing stays simple. - Confusing
originandupstream. You can push toorigin. You usually cannot push toupstream. - Expecting a fork to update itself. A fork is a snapshot. It does not follow upstream unless you sync it.
Exercise
You run git clone on a repository in your own GitHub account that is a fork of a project. Which remote is origin pointing to, and where do you send changes so that the original maintainers can review them?
Show answer
origin points to your fork. You push your branch to origin and then open a pull request on GitHub from your fork's branch to the upstream repository's main branch.
Exercise
Is a branch a copy of the repository? Explain how a branch differs from a fork and a clone.
Show answer
No. A branch is a pointer to a line of commits inside one repository. A fork is a copy of a repository on GitHub, and a clone is a copy on your computer. Both a fork and a clone contain branches.