setup-git-guardrails
Skill3 starsChanged 4 months agoMITView on GitHub
Set up a hook that blocks dangerous git commands before they execute, in Claude Code or Codex CLI.
- Deletes or force-pushes
- Commits and pushes
In this repository26 other files it gives its agents.
- AGENTS.mdAGENTS.md
- mod/CLAUDE.mdCLAUDE.md
- sync-upstreamSkill
- afkSkill
- codebase-designSkill
- commitSkill
- compound-refreshSkill
- compoundSkill
- deputySkill
- diagnosing-bugsSkill
- grillingSkill
- handoffSkill
- implementSkill
- improve-codebase-architectureSkill
- prototypeSkill
- questionnaireSkill
- researchSkill
- review-gateSkill
- setup-cantrips-loopSkill
- simplifySkill
- specSkill
- tddSkill
- teachSkill
- ticketsSkill
- wait-whatSkill
- writing-for-agentsSkill
The file
We split this skill into the rules it gives an agent
9 rules. About 0 are common, 6 are distinctive, and 3 are very specific to this project.
≈0
Common · estimate
Many files like this have a rule of this kind.
≈6
Distinctive · estimate
This author's own choices. Worth a look.
3
Very specific
Tied to their project's names, files or tools. Probably skip.
- Common
- Distinctive
- Very specific
- Distinctive: line 22
When blocked, the agent sees a message telling it that it does not have authority to access these commands.
- Distinctive: line 28
Ask the user for whatever the arguments did not already supply: which harness — Claude Code, Codex CLI, or both — and install for this …
- Distinctive: line 48
Read HOOK-CONFIG.md and work through the section for each harness being installed.
- Very specific: line 50
If the config file already exists, merge the hook into the existing hooks.PreToolUse array.
- Distinctive: line 56
Ask if user wants to add or remove any patterns from the blocked list.
- Distinctive: line 62
Run a quick test:
- Very specific: line 68
Should exit with code 2 and print a BLOCKED message to stderr.
- Distinctive: line 72
Test any pattern the user added in step 4 the same way, splitting its literal too — a hand-added pattern is the likeliest to be a malformed …
- Very specific: line 76
If it runs, read the config file back and check the hook sits inside hooks.PreToolUse, and on Codex that the entry shows as trusted under …
≈ “Common” is an estimate: at least 1 in 5 of the 350,495 skill files we read have a rule of the same kind, not the same words. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.
Files
Setup Git Guardrails
Sets up a PreToolUse hook that intercepts and blocks dangerous git commands before the agent executes them.
What Gets Blocked
git push(all variants including--force)git reset --hardgit clean -f/git clean -fdgit branch -Dgit checkout ./git restore .
When blocked, the agent sees a message telling it that it does not have authority to access these commands.
Steps
1. Ask harness and scope
Ask the user for whatever the arguments did not already supply: which harness — Claude Code, Codex CLI, or both — and install for this project only or all projects?
2. Copy the hook script
The bundled script is at: scripts/block-dangerous-git.sh
Copy it to the target location based on harness and scope:
- Claude Code, project:
.claude/hooks/block-dangerous-git.sh - Claude Code, global:
~/.claude/hooks/block-dangerous-git.sh - Codex CLI, project:
.codex/hooks/block-dangerous-git.sh - Codex CLI, global:
~/.codex/hooks/block-dangerous-git.sh
Make it executable with chmod +x.
The script needs bash, jq and grep on PATH — state that to the user, since jq ships with none of the three platforms by default.
Without it the script reads an empty command, matches nothing, and exits 0, which is the allow verdict: the guardrail installs and passes everything.
3. Add hook to config
Read HOOK-CONFIG.md and work through the section for each harness being installed.
If the config file already exists, merge the hook into the existing hooks.PreToolUse array. Don't overwrite other settings.
On Codex, finish by having the user trust the entry under /hooks; the hook does not run until they do.
4. Ask about customization
Ask if user wants to add or remove any patterns from the blocked list. Edit every copy step 2 wrote, so a both-harness install does not end up guarding two different lists. The entries are extended regexes matched against the whole command, not literal text.
5. Verify
Run a quick test:
echo '{"tool_input":{"command":"git '"push"' origin main"}}' | bash SCRIPT_PATHShould exit with code 2 and print a BLOCKED message to stderr.
On Windows, run it through Git Bash by its full path — bash on the Windows PATH is the WSL stub, which reaches no script and reports no verdict.
The split quoting is what makes this runnable once the hook is live: the command's own text never spells the blocked pattern, so the hook lets it through, while the script still receives git push origin main and answers for itself.
Test any pattern the user added in step 4 the same way, splitting its literal too — a hand-added pattern is the likeliest to be a malformed regex, and a plain payload cannot reach it.
That proves the script, not the wiring.
Prove the wiring live: run git push --dry-run and confirm the harness denies it — a denial is the hook firing, and the command running, harmless as this one is, means it did not.
If it runs, read the config file back and check the hook sits inside hooks.PreToolUse, and on Codex that the entry shows as trusted under /hooks.
Does it work?
Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.
No reviews yet
People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.
- It worked
- Partly
- It didn't
Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.
Your trail
Files you open will collect here, so you can find them again.
Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.
Now do this for your own.
Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.

