Lesson 5 · Git workflow in the age of agents
Make main say no
Review is a habit. A ruleset makes the rules mandatory. About 35 minutes.
Today’s win: a CI check that runs your tests on every PR, and a ruleset that blocks direct pushes to main and merges with failing tests. You’ll watch it block you, which means it would block an agent using your login too.
Warm-up: last lesson’s loose ends
From memory. These are the steps that were missing when you finished PR #6.
Why rules, not just reviews
An agent that uses your gh login has your permissions. It can push to main, merge its own PR, or delete a branch, and GitHub can’t tell it apart from you. In lesson 4 you found out you can’t approve your own PR, so a rule like “require one approval” would block you as well. What a solo developer can enforce is:
- Every change goes through a PR, with zero approvals required. “The pull request doesn’t necessarily have to be approved, but it must be opened.”
- The PR’s check must pass. “If a protected branch requires status checks, they must pass before the pull request can be merged.”
Quotes: GitHub Docs, “Available rules for rulesets” and “About status checks”.
A check is a workflow file
GitHub Actions runs any YAML file in .github/workflows/. “You must save the workflow files in a directory called .github/workflows.” Each job in the file shows up on the PR as one check, with a green tick or a red cross.
Quote and example: GitHub Actions quickstart. Keys: workflow syntax reference.
name: Tests # workflow name
on: # when it runs
pull_request: # every PR
push:
branches: [main] # and every push to main
jobs:
test: # job id = the check’s name
runs-on: ubuntu-latest # a fresh GitHub-hosted machine
steps:
- uses: actions/checkout@v7 # get the repo onto it
- uses: actions/setup-python@v7 # install Python
with:
python-version: "3.13"
- run: pip install pytest
- run: pytest -q # fails the job if a test fails
Remember the job id, test. That’s the name you’ll type into the ruleset as the required check. uses: runs an action someone else wrote, and run: runs a shell command. Each run starts on a clean machine, which is why you check out the code and install Python first.
A ruleset is a list of “no”s
A branch ruleset targets branches (here, the default branch) and turns on rules. You’ll turn on four:
| Rule | What it blocks |
|---|---|
| Restrict deletions | Deleting main on GitHub. Selected by default. |
| Block force pushes | git push --force rewriting main. Enabled by default. |
| Require a pull request before merging | Any push straight to main. Set required approvals to 0. |
| Require status checks to pass | Merging a PR unless test is green. |
The bypass list starts out empty, and that includes you. Admins are only eligible for bypass. They aren’t exempt by default. Leave the list empty: if you add yourself, you’ve added every agent that uses your login. Steps and bypass: GitHub Docs, “Creating rulesets for a repository”.
“A green check means the PR is safe.” A check proves only what its tests test. The PR’s own branch also supplies the workflow file and the tests that run against it. An agent that deletes a failing test, or edits tests.yml, can turn the check green. Also, “a skipped job reports ‘Success’,” even when it’s a required check. You still need to review changes to .github/workflows/ and to the tests.
Why a new repo? On the Free plan, GitHub enforces rulesets only on public repos. Your private git-sandbox returned “Upgrade to GitHub Pro or make this repository public.” Its history also carries your personal email on every commit, which a public repo should not. So this Lesson uses a fresh public repo, git-sandbox-ci: it contains the merged word-count code from PR #6 and is set up to commit with your GitHub noreply address.
Commit email and privacy: GitHub Docs, “Setting your commit email address”.
Drill: predict the guardrail
All four rules are active. Is each action allowed or blocked?
Build it, then try to break it
-
Add the workflow on a branch
cd ~/Work/git-sandbox-ci git switch -c ci/run-tests mkdir -p .github/workflows nvim .github/workflows/tests.yml # type in the workflow above (don’t paste it)Typing it yourself makes you read every key, and YAML cares about indentation. Commit it, push the branch, and open a PR, all from memory (lesson 1).
-
Watch the check run
gh pr checks --watchPredict first: what name will the check show? If it fails, open the log with
gh run view --log-failed, fix the file, commit, and push again. The PR re-runs its checks on every push. Once it’s green, merge with--merge --delete-branch. -
Sync, without being given the commands
Get your local
mainup to date with the merge, and clean up the local branch and the stale remote-tracking branch. Check your work withgit branch -a: you should see onlymainandorigin/main. -
Create the ruleset
gh repo view --web # then Settings → Rules → Rulesets → New ruleset → New branch ruleset- Name
protect-main· Enforcement status Active - Target branches → Add target → Include default branch
- Keep Restrict deletions and Block force pushes ticked
- Tick Require a pull request before merging. Required approvals: 0
- Tick Require status checks to pass → Add checks →
test. Leave “Require branches to be up to date” unticked for now (lesson 6 covers it) - Leave the bypass list empty → Create
gh ruleset check main # lists the rules that now apply to maingh ruleset: gh manual. - Name
-
Push to
mainlike a careless agentecho "CI runs on every PR." >> README.md git commit -am "Note CI in README" git push # predict first: what happens?Read GitHub’s rejection message. Your commit is safe on local
main, butmainis now one commit ahead oforigin/main. Branches are pointers (lesson 1), so give the commit its own branch and movemainback:git switch -c docs/ci-note # new label on the same commit; HEAD moves with it git branch -f main origin/main # move the main label back to origin/main git log --oneline --graph --all -3 # check: where do main, origin/main and HEAD point?Then push
docs/ci-note, open a PR, wait for the check to go green, and merge. This is the only waymaincan move now. -
Watch a red check block a merge
git switch main && git pull git switch -c break/split-bug # in wordcount.py, change text.split() back to text.split(" ") git commit -am "Reintroduce split bug on purpose" git push -u origin break/split-bug gh pr create --fill gh pr checks --watch gh pr merge --merge # predict firstThe bug is the same one you caught in PR #6, and this time a machine catches it. Afterwards, close the PR without merging it:
gh pr close --delete-branch. Then switch back tomainand delete the local branch withgit branch -D break/split-bug. The capital-Dis needed because this branch was never merged. -
Get graded
In Claude Code, say “check lesson 5”. I’ll check through the API that the workflow is on
main, read the ruleset’s settings, and check that you have a merged green PR and a closed red one.
What this stops, and what it doesn’t. An agent with your login can no longer push to main, force-push it, or merge a PR with failing tests. It can still run gh pr merge on a green PR, because you can, and it can weaken the tests inside its own PR. Closing those gaps is lesson 9: Claude Code hooks that block gh pr merge and pushes to main before they run, and an agent identity separate from yours.
Recall it
No scrolling up. Spaced review: retry the Lesson 4 quiz before this one.
Go deeper
Primary source: GitHub Docs, “Available rules for rulesets” (about 15 minutes). Skim it for rules you might want later, such as “Require linear history” and “Require signed commits”. For the YAML side, read the Actions quickstart.
Reference: Cheat sheet (new checks and rulesets section) · Glossary.
Stuck on a YAML error, unsure what a rule does, or wondering how a team would set this up? Ask me in Claude Code. I can read your workflow run logs and ruleset through the API.