7. Collaboration and pull requests
Author: Ángel Fernández Caravaca
Date: August 14, 2026
Chapter 6 ended with a repository able to synchronise with a copy of itself, and with one restriction still standing: every git push we typed assumed you were allowed to write to the other side. That assumption breaks as soon as you want to contribute to someone else's project, and it also breaks within a team, where being able to push to main doesn't mean anyone wants you to do it without review. In this chapter we will learn the two shapes collaboration takes, what a fork really is, what a pull request actually does, how a review works from both sides and the daily cycle that ties it all together.
Two ways to collaborate¶
Before any command, it is worth separating the two situations, since they use different remotes and mixing them up is the source of most beginner stumbles. The question that separates them is simply do you have write access to the repository?
| Shared repository | Fork and pull | |
|---|---|---|
| Write access | Yes, you are a collaborator | No, you are an outsider |
| Where your branch lives | In the project's own repository | In your personal copy of it |
| Remotes you set up | Only origin |
origin (yours) and upstream (theirs) |
| Typical setting | A company, a team, your own projects | Open source, a project you found on GitHub |
Both end up in the same place, that is, a pull request that someone reviews and merges, and everything from the review onwards is identical. The only thing that differs is the plumbing of the first two steps, so the rest of this chapter describes the fork case in detail and points out where the shared-repository case is shorter.
Note
Neither model is more "professional" than the other. A team of five with a shared repository has no reason to fork, since it would only add a hop with no benefit, whereas a project with two thousand contributors can't give write access to all of them. Choose according to the access you have, not out of preference.
The fork: a copy you can write to¶
A fork is a clone made on the server, under your account. It is the same operation as the git clone from Chapter 6, with one difference that changes everything: the copy lives on GitHub instead of on your laptop, so it has a URL you can push to. Your fork is a complete repository, with its own branches, its own issues and its own history, and GitHub simply remembers where it came from.
You fork from the web interface, with the Fork button, or from the terminal if you have the GitHub CLI installed:
Once you have the fork, you clone your copy, not the original:
At this point you have exactly the setup from Chapter 6, that is, a remote called origin pointing to your fork. That is enough to work, but it leaves you blind to everything happening in the original project, which will keep moving while you write your contribution. The solution is a second remote, and this is where the name upstream comes back, which you saw with a different meaning in Chapter 6:
Reading those four lines as a map of your permissions makes the rest of the chapter obvious:
originis yours, so you canfetchandpushfreely.upstreamis theirs, so you canfetchfrom it but notpush. Trying to fails with a permissions error, and that failure is the whole reason forks exist.
Caution
The word upstream now means two different things, and it is worth
pinning them down. In Chapter 6, a branch's upstream was the remote branch a
local branch is paired with, that is, configuration. Here, upstream is the name of a remote,
chosen by convention for the original repository. They are unrelated, and nothing forces you to use
that name, that is, you could call the remote original and everything would work the same.
Keeping your fork in sync¶
A fork is a snapshot, not a subscription. The moment it is created it stops updating on its own, so the original project moves on and your copy silently falls behind. A branch written on top of a two-month-old main is the classic cause of the painful conflicts Chapter 5 warned about, and keeping the fork up to date is the cheapest way to avoid them.
The routine is three commands, all from Chapter 6, and it is worth running before starting any new piece of work:
Notice there is nothing new here. upstream/main is a remote-tracking branch exactly like origin/main, that is, a local cache of where the original project's main was at your last fetch, and the merge is the normal merge from Chapter 4. Since you never commit directly on main, that merge is almost always a fast-forward and finishes without a word.
The same in a single command, if you use the CLI:
Tip
Treat your fork's main as read-only, that is, as a mirror of the original project that
you only update with the four commands above. Each piece of work goes on its own
branch, created from a freshly synced main. This single habit eliminates the most
common mess in fork workflows, that is, a main that has diverged from the project it
is supposed to mirror and can no longer fast-forward.
With the fork up to date, the branch is created as Chapter 3 taught and pushed to your fork with the -u from Chapter 6:
The pull request: what it really is¶
This is the first thing to be clear about: a pull request is not a Git feature. There is no git pull-request command, and nothing in your .git folder knows the concept exists. It is a GitHub feature, that is, a wrapper built on top of the operations you already know, and understanding what it wraps takes away most of the mystery.
A pull request is a request for one branch to be merged into another, plus a page to discuss it. It names exactly two things:
- The base, that is, the branch that would receive the work, usually the original repository's
main. - The head or compare branch, that is, the branch that provides the work, usually your branch on your fork.
Read that pair with Chapter 4 in mind and everything becomes familiar: the pull request is a proposal to run git merge <head> while on <base>, kept open so people can review it first. GitHub does the merge for you when it is approved, on the server, and the merge is the same operation whether a button or a command triggers it.
Important
The name is confusing and worth translating once. You are not asking anyone to run git pull, and you are certainly not "pulling" anything yourself. You are asking the maintainers to pull in your work, that is, to download your branch and merge it. GitLab calls the same thing a merge request, which describes it more accurately.
You open it from the web interface, which offers you a prompt as soon as it detects you have pushed a branch, or from the terminal:
The property that surprises people is that a pull request is live, not a submission. It doesn't capture your branch at the moment you open it; it watches it, so any commit you push afterwards shows up in it automatically:
That is why the right response to "the reviewer asked for a change" is never to close the pull request and open another. You push to the same branch and the conversation carries on where it was.
Writing a pull request that can be reviewed¶
A pull request is read by someone who doesn't have your context, and its quality decides how fast it gets merged far more than the code itself. A few habits do almost all of the heavy lifting:
-
Keep it small and about one thing. A pull request that touches four files with a single purpose is reviewed in minutes, whereas one that touches forty with three unrelated purposes waits days and gets a worse review when it finally does. If you fixed a bug and also renamed some variables, that is two pull requests.
-
Write a title that says what changed, that is, Fix crash when the config file is missing instead of Update main.py.
-
Use the description to explain the why. The diff already shows what you did, and the why is the one thing the reviewer can't reconstruct on their own: what problem you were solving, what alternative you ruled out and what you deliberately left out of scope.
-
Link the issue it resolves. Writing
Closes #42in the description makes GitHub close that issue automatically when the pull request is merged, and links both pages in both directions. -
Say how you tested it. One line is enough, and it is the difference between a reviewer who trusts the change and one who has to reproduce your work.
-
Read your own diff before asking anyone else to. Open the Files changed tab and look at it as a stranger would. Debug prints, commented-out code and stray conflict markers from Chapter 5 are found in ten seconds that way.
If the work isn't finished but you want opinions on the direction, say so explicitly instead of apologising in a comment:
Tip
Many repositories include a .github/PULL_REQUEST_TEMPLATE.md, and the description box
arrives pre-filled with it. That template is the maintainers telling you exactly what
they need to review your work, so filling it in properly is the
cheapest thing you can do to get it merged quickly. The same goes for a
CONTRIBUTING.md file, which is worth reading before you write a single line of code.
The review¶
The review is the point of the whole mechanism, that is, the reason we didn't simply push to main. On GitHub it happens in the Files changed tab, where any line of the diff can carry a comment, and it ends with the reviewer choosing one of three verdicts:
| Verdict | What it means | What happens next |
|---|---|---|
| Comment | Remarks without a judgement | Nothing is blocked, the discussion continues |
| Approve | The reviewer is happy with the change | It can be merged, subject to the repository's rules |
| Request changes | Something must be fixed first | The merge is blocked until a new review is submitted |
Two features of the interface are worth knowing because they save an enormous amount of time. The first is the suggestion, that is, a comment containing a replacement code block that the author can apply with a button, which turns a paragraph of "could you rename this variable?" into a click and a commit. The second is resolving conversations, that is, marking a thread as settled, which keeps a long review readable by hiding what has already been dealt with.
The same operations exist from the terminal:
Reviewing a diff on a web page is enough for most changes, but sometimes you need to actually run the code. A pull request's branch can be checked out locally, and the CLI does it in a single command:
That second form is a nice confirmation of Chapter 6: GitHub exposes the pull request's branch as a normal ref, so downloading it is the usual fetch with an explicit refspec, that is, take pull/42/head from the remote and write it into my local pr-42.
Note
Being on the receiving end of a review is a skill in its own right. A comment on your diff is about the code and not about you, "why did you do it this way?" is usually a sincere question and not an accusation, and disagreeing is perfectly fine as long as you argue your case. The habit worth building is replying to every thread, even if only to say you have applied the change, since silence leaves the reviewer not knowing whether you read it.
When the base branch moves under your feet¶
While your pull request waits, the base branch keeps moving, and eventually GitHub tells you:
Nothing has gone wrong, and it is exactly the situation from Chapter 5, with the two branches being yours and the project's. GitHub offers to resolve simple cases in the browser, but doing it locally is both safer and easier to test:
Notice the direction, since it is what people get backwards: you merge the base into your branch, not your branch into the base. Your branch is where the disagreement is settled, that is, in your own copy, where a mistake costs nothing and can be aborted, and the pull request remains the single proposal it was.
Warning
The alternative is git rebase upstream/main, which produces a cleaner history and is what many projects ask for. It rewrites your commits, so afterwards it requires a git push --force-with-lease, that is, the flag from Chapter 6, and here it is safe only because the branch is yours alone. Never rebase a branch other people have based work on. Chapter 8 covers the whole topic, and until then merging is the option that can't hurt you.
How the pull request is finally merged¶
When everything is approved, someone presses the merge button, and GitHub offers three ways to do it. All three are Chapter 4 operations run on the server, so you already know what each one produces:
| Button | The Git operation behind it | What the history looks like |
|---|---|---|
| Create a merge commit | git merge --no-ff |
Every commit on the branch is kept, plus a merge commit that joins the two lines |
| Squash and merge | git merge --squash followed by a commit |
A single new commit on main containing the whole change |
| Rebase and merge | git rebase followed by a fast-forward |
Each commit is replayed onto main, so the history stays a straight line |
Which one a project uses is a matter of policy rather than correctness, and it is usually configured once and applied to everything. Squash and merge is by far the most common in practice, because it makes main read as one commit per change no matter how messy the branch was, and it means you don't have to worry about your intermediate wip and fix typo commits.
Important
Both squash and rebase create new commits with new hashes, so the commits on main are not the ones on your branch. That is why, after a squash merge, git branch --merged won't list your branch and git branch -d will refuse to delete it, exactly as Chapter 4 explained. It is not a sign that something failed, so delete the branch with -D once the pull request page says it has been merged.
After the merge, the clean-up is the one from Chapters 4 and 6, and GitHub offers to do half of it for you:
The GitHub Flow¶
Everything above comes together in a cycle with a name of its own, the GitHub Flow, and its value is that it is short enough to remember. There is a single long-lived branch, main, which can always be deployed, and every change is a short-lived branch that becomes a pull request:
- Sync
mainwith the project, that is,git fetch upstream && git merge upstream/main. - Create a branch from there, with a name that says what it does, that is,
git switch -c fix-empty-config. - Commit on that branch, in small steps and with readable messages.
- Push it, that is,
git push -u origin fix-empty-config. - Open a pull request and describe what you did and why.
- Discuss and review, pushing new commits to the same branch until it is approved.
- Merge, and then delete the branch on both sides.
- Go back to step 1, since
mainhas just moved.
Compare it with the whole course so far and you will see that steps 1 to 4 and 7 are Chapters 3, 4 and 6, and only 5 and 6 are new. That is this chapter's real thesis: collaborating on GitHub is not a new set of Git concepts, but the ones you already have plus a web page where the merge is discussed before it happens.
Tip
The two habits that matter most in this cycle are creating the branch from a freshly
synced main and keeping the branch short. A branch that is opened, reviewed and
merged within a day rarely conflicts with anything, whereas a branch that stays open
for three weeks has to reconcile three weeks of other people's work, and does so at
the worst possible moment.
Rules the repository enforces¶
Most established repositories don't trust everyone to remember the flow; they configure GitHub to enforce it. You will come across these rules as messages on your pull request rather than as things you configure yourself, so recognising them is enough:
- Protected branches, that is,
mainrejects a directgit pushoutright, so a pull request is the only way in. This is what turns the flow from a convention into a rule. - Required reviews, that is, the merge button stays disabled until there are one or more approvals, sometimes from specific people.
- Required status checks, that is, an automated pipeline runs the tests and the linter on your branch and the merge is blocked while anything is red. Chapter 9 says a bit more about where they fit in.
CODEOWNERS, that is, a file that maps paths to people, so that touching a directory automatically requests a review from whoever is responsible for it.
None of this changes a single command you type, and that is the idea worth keeping: the rules live on the server and decide when the merge is allowed, while your part of the work is the same eight steps as always.
Summary¶
| Command | What it does |
|---|---|
gh repo fork owner/project --clone |
Creates your copy of a repository on the server and clones it |
git remote add upstream <url> |
Adds the original repository as a second, read-only remote |
git fetch upstream |
Downloads what the original project has done since your last sync |
git merge upstream/main |
Brings the original project's work into your branch |
gh repo sync you/project --branch main |
Updates your fork's branch from the original in a single command |
git switch -c <branch> |
Opens a branch for the unit of work you are about to do |
git push -u origin <branch> |
Publishes the branch to your fork and records the pairing |
gh pr create --fill |
Opens a pull request using the commit message as title and body |
gh pr create --draft |
Opens it as a draft, that is, visible but not mergeable |
gh pr ready |
Marks a draft as ready for review |
gh pr list / gh pr status |
Lists the open pull requests, or the ones that concern you |
gh pr checkout <n> |
Checks out a pull request's branch locally |
git fetch origin pull/<n>/head:pr-<n> |
The same without the CLI, as an explicit refspec |
gh pr review --approve |
Approves a pull request |
gh pr review --request-changes --body "..." |
Blocks the merge and explains what to fix |
gh pr comment <n> --body "..." |
Adds a comment to the conversation |
gh pr merge <n> --squash --delete-branch |
Merges it as a single commit and removes the branch |
git push --force-with-lease |
Safely publishes a rewritten branch, after a rebase (Chapter 8) |
git branch -D <branch> |
Deletes the local branch after a squash merge, which -d refuses |
git push origin --delete <branch> |
Deletes the branch on your fork once it has been merged |
You can now contribute to a repository that isn't yours, and the cycle is complete: fork, branch, commit, push, propose, review, merge. However, twice in this chapter we hit the same wall, that is, keeping a branch up to date and cleaning up its commits both pointed to an operation we kept putting off, and both came with a warning about rewriting history. That is Chapter 8: what git rebase really does to your commits, how it differs from the merge in Chapter 4, what an interactive rebase lets you rewrite and the one rule about when you must never do it.