🐍 Python for Everyone

← Back to interview questions

Chapter 29 Project 55 min build

Open-Source-Style Collaboration Workflow

A guided simulation of a real GitHub contribution: fork, feature branch, push, pull request, code review, merge, and syncing your fork afterward. Every remote/branch/commit/merge command below is a real git command that actually runs against a real local Git repository — not a description of GitHub's UI. The steps that genuinely can't be scripted (opening a PR, a reviewer leaving comments, clicking "Approve," clicking "Merge pull request") are marked documented-only below, since they live inside github.com's web UI, which this offline project has no way to reach.

Why a local bare repo stands in for GitHub This site is static (GitHub Pages, no backend, nowhere for a learner's machine to push to). So instead of describing fictional GitHub commands, this project builds a real stand-in for "GitHub" using git init --bare — a genuine Git repository with no working directory, structurally identical to what GitHub hosts on its own servers for every repo. Cloning it, pushing to it, and fetching from it are the exact same git operations you'd run against a real github.com URL — only the URL is a local folder path instead of https://github.com/....

Example run

Output
=== Chapter 29 Project: GitHub Collaboration Workflow ===

Scenario ready.
  upstream (stands in for the real GitHub repo you don't own): .../scenario/upstream.git
  my-fork  (stands in for YOUR fork of it on GitHub):          .../scenario/my-fork.git

--- Step 1: clone your fork (not upstream) ---
$ git clone .../my-fork.git scenario/workdir
...
--- Step 5: simulate the maintainer merging the approved PR ---
$ git merge --no-ff pr-1-add-pancake-servings -m "Merge pull request #1 ..."
Merge made by the 'ort' strategy.
 recipes.py | 7 +++++++

Upstream history after the merge:
*   e11dccf Merge pull request #1 from learner/add-pancake-servings
|\
| * a34b3a4 Add servings_for() to scale a recipe by number of people
|/
* 0a69be7 Initial commit: recipe box skeleton

=== Done. Your fork's main branch now matches upstream's main, ... ===

Your own commit hashes will differ — the shape of the history is what matters.

How to run Requires a real git executable on your PATH (check with git --version). Download starter.py below into an empty folder, then run python3 starter.py. Standard library only otherwise — no pip installs needed. Everything happens inside a scenario/ subfolder, safe to delete any time; re-running the script wipes and rebuilds it automatically.

The workflow, step by step

Nine steps total. The first four and last four are real, executed git commands. The middle steps (5-6) happen on github.com and are documented-only — but step 7 (the merge itself) reproduces the mechanical result of that review with real commands, so you see exactly what git does underneath GitHub's "Merge pull request" button.

The core piece: run_git() and setup_scenario()

Every step below calls run_git() — it's the whole trick: a thin wrapper around subprocess that runs a real git command and prints exactly what happened. setup_scenario() is provided, building the two bare repos before your work starts.

run_git.py
import subprocess

def run_git(args, cwd=None, check=True):
    """Run a real git command and return the CompletedProcess."""
    print(f"$ git {' '.join(args)}" + (f"   (in {cwd})" if cwd else ""))
    result = subprocess.run(
        ["git"] + args,
        cwd=cwd,
        check=check,
        capture_output=True,
        text=True,
    )
    if result.stdout.strip():
        print(result.stdout.strip())
    if result.stderr.strip():
        print(result.stderr.strip())
    return result

Thought process: the whole project hinges on making every command genuinely visible and genuinely real, not narrated — printing the exact command before running it, and its real stdout/stderr after, is what turns this into something you can trust rather than a script that just claims steps happened. check=True matters too: if any git command actually fails, the script should crash loudly right there instead of silently continuing with a broken repo state.

Write the prompt you'd give an AI assistant to build this:

Show a strong version of this prompt
Write a Python function run_git(args, cwd=None, check=True) using only the standard library subprocess module. It should run a real git command (["git"] + args), print the exact command being run (and the cwd if one was given) before executing it, capture stdout and stderr as text, print any non-empty stdout/stderr after the command finishes, propagate a real CalledProcessError if the command fails and check=True (do not swallow failures silently), and return the CompletedProcess object so callers can inspect it further.

Steps 5-6, written up for real (since they happen on github.com)

These are the steps this script cannot execute, because they require a real github.com account and browser session. Here's exactly what you'd do on the real site, in order, right after Step 4's push:

  1. Open a pull request. Go to your fork on github.com. GitHub detects the recently-pushed branch and shows a "Compare & pull request" banner — click it. You'll land on a compare view showing base: upstream/main ← compare: your-fork/add-pancake-servings.
  2. Write a PR description. Give it a clear title ("Add servings_for() to scale a recipe by number of people") and a body explaining what changed and why — see this chapter's interview questions for what makes a PR description actually useful to a reviewer.
  3. Click "Create pull request." This opens PR #1 against the upstream repo. GitHub now shows a "Files changed" diff view and a comment thread.
  4. A reviewer reviews it. On github.com, a reviewer opens "Files changed," reads the diff, and either leaves inline comments, requests changes, approves, or just comments — see this chapter's interview questions for the difference between those three review actions.
  5. The reviewer approves. Once satisfied, they submit a review with "Approve" selected.
  6. The maintainer merges. With an approval in hand, the "Merge pull request" button becomes available (assuming no branch protection rule blocks it). They pick a merge strategy (merge commit, squash-and-merge, or rebase-and-merge — see interview questions for the tradeoffs) and click through.

Step 7 in the script reproduces the mechanical result of that merge — a real merge commit built from real git commands — so you can see exactly what git itself did underneath GitHub's button, even though the button itself isn't something a script can click.

The pieces

Ideas to make it your own

Why this project matters

Fork/clone, remotes, PRs, and code review are the daily mechanics of essentially every professional software job that uses Git — not just open source. The GitHub UI parts (opening a PR, reviewing, clicking merge) are easy to read about and hard to internalize without practice; the git mechanics underneath them (what a fork actually is, what git fetch really does, what a merge commit actually looks like) are the part a script can make completely real, so that's exactly what this project does.

More project ideas

From Chapter 7 onward, you get a genuine choice of what to build. The Collaboration Workflow above is fully built out with a starter and reference solution — the four ideas below are not. Each is a real, grounded use case solvable with only what's been taught through Chapter 29. No starter or solution files are provided on purpose — building one of these unassisted is the point.

1 Sync a Fork That's Fallen Badly Behind

Problem: A fork hasn't been synced in a long time — upstream has several new commits on main, and the fork's main is now many commits behind.

What it should do: Using the same git init --bare technique from this project, build a bare "upstream" repo with 4-5 sequential commits on main, a "fork" bare repo cloned from an early point in that history (so it's genuinely behind), and a working clone of the fork. Practice the real sync sequence — git fetch upstream, git merge upstream/main, git push origin main — and print the before/after git log --oneline --graph to show the fork catching up.

Suggested approach: Reuse this project's run_git()/setup_scenario() pattern, but seed upstream with multiple commits after cloning the fork instead of before, so there's a real gap to close.

2 Write a PR Description From a Bug Report

Problem: A bug report says: "The servings_for() function crashes with a ZeroDivisionError if people is 0, instead of returning a sensible message." You've fixed it — now you need a PR description a reviewer would actually find useful.

What it should do: Write the actual code fix (a real Python function with a guard clause for people <= 0), then write a real PR description using this template: What changed, Why, How to test it, Anything a reviewer should pay special attention to. No starter file — write both the fix and the description from scratch, in a plain text or Markdown file.

Suggested approach: Base the description on this chapter's interview-question answer about what makes a PR description genuinely useful to a reviewer, not just a summary of the diff.

3 Write Three Kinds of Review Comments for One Diff

Problem: The same small, buggy diff needs three different kinds of code review comments — a real reviewer doesn't write every comment the same way.

What it should do: Take this diff (a function with a real bug — mutating a default mutable argument, straight from Chapter 8's lesson):

diff.py
def add_topping(topping, toppings=[]):
    toppings.append(topping)
    return toppings

Write three separate comments a reviewer might leave on it: one nitpick (something small and non-blocking, like a naming or docstring suggestion), one blocking comment (the actual mutable default argument bug — specific enough that the author knows exactly what to change), and one piece of praise (something genuinely good about the code, stated specifically rather than generically).

Suggested approach: Use this chapter's interview-question answers on "what reviewers look for" and "approve vs. request-changes vs. comment" to calibrate tone and specificity for each of the three.

4 Simulate a Full Issue-to-PR Workflow

Problem: A real open-source contribution usually starts with an issue, not a PR — someone reports a bug, a contributor claims it, fixes it, and links the fix back to the original report.

What it should do: Using the same bare-repo technique as this project, write up (in a Markdown file) a realistic issue report for a bug in recipes.py (choose your own — e.g. list_recipes() doesn't handle an empty RECIPES dict cleanly), then actually build the fix as a real feature branch + commit against a bare-repo scenario you set up yourself, with a commit message and PR description that includes a closing keyword (e.g. Fixes #12) linking back to the issue number.

Suggested approach: Reuse this project's setup_scenario()/run_git() pattern for the real git mechanics; write the issue and PR description as plain Markdown files since GitHub issues themselves can't be created without a real repo.

← Interview Questions Now Pair With an AI →