Day 6, 30 minutes · beginner
Why it matters
Custom instructions ask Copilot to follow a rule. A hook makes sure of it. A hook is a small command that Copilot runs at a fixed moment, for example just before it uses a tool. The hook sees what Copilot is about to do and can say no. GitHub puts the difference plainly: hooks run code rather than asking the model to follow a rule.
That matters once more people use Copilot on the same project. One person approves a command too quickly, another runs a script with broad permissions, and the cloud agent works with every tool already approved. A hook in the repository is the one rule that travels with the code to every one of those sessions.
Today you add one hook to the cheese shop repository that blocks recursive deletes, test it with a throwaway folder, and write a short team checklist that ties the whole course together.
How a preToolUse hook works
Hooks live in JSON files in .github/hooks/ in the repository. Copilot CLI loads them when it starts in a trusted folder, and the cloud agent loads them from its copy of the repository. Each file has "version": 1 and a list of hooks per event.
The event you need is preToolUse. It fires before every tool call. Copilot sends the hook a small JSON message on standard input with the tool name and its arguments. The hook can then print one JSON object back:
"permissionDecision": "deny"stops the tool call."permissionDecisionReason"is required with a deny. Copilot reads it, so write it as an instruction: what is blocked and what to do instead.
The hook in the guide uses a matcher of bash|powershell, so it only looks at shell commands and not at file edits that happen to contain the same words. It has a bash line for macOS and Linux and a powershell line for Windows; Copilot picks the one for your system. Both look for a recursive delete in the command text. If they find one, they print the deny; if not, they print nothing and Copilot carries on as normal.
What a hook does not do
Be clear with yourself and your team about the limits:
- It matches text. This hook looks for
rm -randRemove-Itemwith-Recurse. Copilot can still delete files one by one, and another way of writing the command can slip past. It is a seatbelt, not a wall. - A slow hook lets the call through. If a hook runs past its time limit, Copilot treats it as if it had not run and the normal approval rules decide. A hook that crashes or exits with an error does the opposite and blocks every tool call it matches (here every shell command), so test it after each change.
- Bash only in the cloud. The cloud agent runs hooks on Linux and uses only the
bashline. Write that line with care, even if you work on Windows. - Hooks are code in your repository. Review a change to
.github/hooks/the way you review any code.
For the wider question of which guardrails a team needs around any coding agent, the Ship AI at the client course goes deeper.
The steps
- Start from the latest main after yesterday's merge. On Windows, check that PowerShell 7 is installed: GitHub's hook examples need it.
- Create the hook file
.github/hooks/block-recursive-delete.json. - Make a throwaway folder so the test can never hurt real work.
- Start Copilot fresh. Hook files are loaded when Copilot starts.
- Ask for a recursive delete and watch the hook deny it. Check that the reason is the hook's own sentence. A hook that breaks also blocks the command, but with a message that the hook errored, and then it blocks every other command too.
- Check that normal work still runs. Ask Copilot to run
git status. If that is blocked as well, the hook is broken, not strict. - Write the team checklist and change it until your team agrees with every line.
- Commit and push both, so the hook also applies to the cloud agent.
Follow the guide for GitHub Copilot below for the exact file and commands.
The team checklist
The checklist in the guide sums up this course in rules a team can agree on: which plan everyone is on, short instructions in git, Copilot started only inside a project folder, every command read before approval, scripts with named permissions and a blocked push, hooks reviewed like code, issues with a "Done when" list, a person who reads, tests and merges every Copilot pull request, workflow changes read before they run, and a monthly look at credit usage. It is a starting point. Cut what does not fit your team, and add what you learned the hard way.
Example: Roos's hook
Roos added the hook and asked Copilot to delete old-drafts in one command. Copilot tried a recursive delete, the hook denied it, and Copilot answered that recursive deletes are blocked by the team hook and that a person should do it. She deleted the folder herself, wrote the checklist with Annick, who helps her with the site, and pushed both files.
Common mistakes
- Testing in a session that was already running. The hook is not loaded until Copilot starts again.
- Printing more than one JSON object. Copilot then ignores the hook's answer, and the normal approval rules decide.
- A Windows-only hook. The cloud agent ignores the
powershellline, so a hook without abashline does nothing there. - Switching the hook off and forgetting it.
"disableAllHooks": truepauses a file. Agree in the team who may do that, and when.
Today's actions
Ticks are not saved without an accountDo it in GitHub Copilot
GitHub Copilot
- Bring the project up to date and make the hooks folder. On Windows, also run
pwsh --version: GitHub's hook examples need PowerShell 7.git switch main git pull mkdir .github/hooks - Create the file .github/hooks/block-recursive-delete.json in your editor, paste this text and save it.
{ "version": 1, "hooks": { "preToolUse": [ { "type": "command", "matcher": "bash|powershell", "bash": "grep -q 'rm -r' && echo '{\"permissionDecision\":\"deny\",\"permissionDecisionReason\":\"Recursive delete is blocked by the Brindlewick team hook. Ask a person.\"}'; exit 0", "powershell": "$text = $input | Out-String; if ($text -match 'Remove-Item.*-Recurse|rm -r') { @{ permissionDecision = 'deny'; permissionDecisionReason = 'Recursive delete is blocked by the Brindlewick team hook. Ask a person.' } | ConvertTo-Json -Compress }; exit 0", "timeoutSec": 10 } ] } } - Make a throwaway folder with one file in it, so you have something safe to delete.
mkdir old-drafts echo test > old-drafts/note.txt - Start
copilotin the project folder. Hooks only load when Copilot starts, and only in a trusted folder: you trusted this one in lesson 1. - Paste this task and press Enter. If Copilot tries a recursive delete, the hook denies it and Copilot gets the reason from the hook.
Delete the folder old-drafts and everything in it in one command. - Check the reason: it must be the hook's own sentence about the Brindlewick team hook. If Copilot says the hook errored, fix the file first. Then ask Copilot to run
git status: that must still work. Type/exitand delete old-drafts yourself. - Create TEAM-CHECKLIST.md in the project with this text, and change it until your team agrees with every line.
# Team checklist: Copilot as a coding agent - Plans: everyone knows which Copilot plan they are on and what it includes. - Instructions: .github/copilot-instructions.md is short, true and committed. - Local runs: start Copilot only inside a project folder, never in the home folder. - Approvals: read every command before you approve it. Never approve rm for the rest of a session. - Scripts: every copilot -p run names its tools with --allow-tool and blocks git push with --deny-tool. - Hooks: .github/hooks/ is reviewed like code. Nobody switches a hook off without telling the team. - Issues for Copilot: a problem, the files and a Done when list. - Pull requests from Copilot: a person reads every changed line, tests it and merges. - Workflows: read any change under .github/workflows/ before Approve and run workflows. - Credits: check usage once a month. - Commit and push the hook and the checklist.
git add .github/hooks/block-recursive-delete.json TEAM-CHECKLIST.md git commit -m "Add delete hook and team checklist" git push
Note: On Windows the hook runs the powershell line; the cloud agent runs only the bash line, on Linux. A preToolUse hook that crashes blocks every tool call it matches (here every shell command), and one that times out falls back to the normal approvals. To pause a hook file without deleting it, add "disableAllHooks": true at the top level.
Create a free account to save your progress
Free, and no payment details. Your ticks and finished lessons are kept, on every device.
Create a free account I already have an accountCreate a free account to save your progress. Sign in or sign up