When three of us started using Claude Code on the same repository, we each configured it differently. One approved every command as it came up, one ran in a permissive mode, and one had a long list of personal rules nobody else knew about. The agent behaved differently for each of us, and a hook one person relied on did not exist for the others.
This guide is how we moved to a shared, reviewed configuration: which settings file does what, how permission rules are evaluated, what hooks can and cannot do, and where CLAUDE.md and subagents fit. Everything here follows the official Claude Code documentation; check it for the current list of keys, because the tool moves quickly.
Settings files and which one wins
Claude Code reads JSON settings from several scopes:
Scope | Location | Shared? | Typical use |
|---|---|---|---|
Managed |
| Deployed by IT | Organisation policy that users cannot override |
User |
| No | Personal defaults across all projects |
Project |
| Yes, committed | Team rules for this repository |
Local project |
| No, git-ignored | Personal overrides for this repository |
Precedence, highest first: managed settings, command-line arguments, local project, shared project, user. So a deny rule in managed settings always holds, and your local file can tighten or loosen what the team file sets, within what managed policy allows.
Our rule of thumb: anything another engineer would need to get the same behaviour goes in .claude/settings.json and gets reviewed like code. Personal preferences stay in the user or local file.
A minimal project file:
{
"$schema": "https://json.schemastore.org/claude-code-settings.json",
"permissions": {
"allow": [
"Bash(pnpm test:*)",
"Bash(pnpm lint:*)",
"Bash(git diff:*)",
"Bash(git log:*)"
],
"ask": ["Bash(git push:*)"],
"deny": [
"Read(./.env)",
"Read(./.env.*)",
"Read(./secrets/**)",
"Bash(curl:*)"
]
},
"env": {
"NODE_ENV": "development"
}
}
The $schema line gives editor completion and catches typos in key names, which otherwise fail silently.
Permission rules
Each rule names a tool and, optionally, a specifier in parentheses:
Bash(npm run test:*)matches Bash commands starting withnpm run test.Read(./secrets/**)andEdit(src/**)use gitignore-style path patterns.WebFetch(domain:docs.example.com)limits fetches by domain.mcp__github__create_issuenames a single tool from an MCP server.
Rules are checked in a fixed order: deny first, then ask, then allow. A matching deny always wins, even if a broader allow also matches. Anything not matched falls back to the current permission mode, which in the default mode means Claude asks you.
Things that tripped us up:
Bash prefix rules are not a sandbox. Claude Code understands shell operators, so
Bash(pnpm test:*)does not approvepnpm test && rm -rf build. But a prefix cannot express intent completely:Bash(git:*)approvesgit push --forcetoo. Prefer narrow prefixes.Read deny rules do not stop a shell command from reading the file. Denying
Read(./.env)blocks the Read tool;cat .envis a Bash command. If a file must not be read, also deny the obvious Bash routes or, better, keep the file out of the working tree. For hard isolation, use the sandboxing options in the docs or a container.Check effective rules with
/permissions. It lists rules from every scope, so you can see where an unexpected allow comes from.
Permission modes
defaultMode sets the starting mode: default (ask on first use), acceptEdits (file edits are accepted for the session), plan (analysis only, no edits or commands) or bypassPermissions (no prompts). Organisations can turn off the last one through managed settings with "disableBypassPermissionsMode": "disable". We do that on shared machines.
Hooks
Hooks are shell commands Claude Code runs on lifecycle events. The ones we use most:
Event | When it runs | Can block? |
|---|---|---|
| Before a tool call | Yes |
| After a tool call succeeds | Can feed back to Claude |
| When you submit a prompt | Yes, and can add context |
| When Claude finishes responding | Can ask Claude to continue |
| New or resumed session | Adds context |
| When Claude needs attention | No |
Hooks receive a JSON object on stdin with fields such as session_id, cwd, hook_event_name, tool_name and tool_input. Exit code 0 means success. Exit code 2 is a blocking error: for PreToolUse the tool call is cancelled and your stderr is shown to Claude, so it can correct itself. Other non-zero codes are reported but do not block.
Block edits to applied migrations
{
"hooks": {
"PreToolUse": [
{
"matcher": "Edit|Write",
"hooks": [
{ "type": "command", "command": "\"$CLAUDE_PROJECT_DIR\"/.claude/hooks/protect-migrations.sh" }
]
}
]
}
}
#!/usr/bin/env bash
set -euo pipefail
file=$(jq -r '.tool_input.file_path // empty')
case "$file" in
*/database/migrations/*)
if git -C "$CLAUDE_PROJECT_DIR" ls-files --error-unmatch "$file" >/dev/null 2>&1; then
echo "Migration $file is already committed. Create a new migration instead of editing it." >&2
exit 2
fi ;;
esac
exit 0
$CLAUDE_PROJECT_DIR points at the project root, so the hook works no matter which directory Claude is in.
Format after every edit
{
"hooks": {
"PostToolUse": [
{
"matcher": "Edit|Write",
"hooks": [
{ "type": "command", "command": "jq -r '.tool_input.file_path' | xargs -I{} npx prettier --write {} >/dev/null 2>&1 || true" }
]
}
]
}
}
The || true matters: a formatter error on a file type Prettier does not know should not interrupt the session.
Hook safety
Hooks run with your user's permissions, automatically, every time. Treat a hook in a shared settings file like any other script in the repository: review it, keep it in version control, quote variables, and never pipe tool input into eval. Claude Code snapshots hook configuration at startup and asks you to review changes made during a session in /hooks, which protects against a hook being rewritten mid-session.
CLAUDE.md: memory, not policy
CLAUDE.md files hold instructions that are loaded into context: the project file (./CLAUDE.md or ./.claude/CLAUDE.md), your user file (~/.claude/CLAUDE.md), and files in subdirectories, loaded when Claude works in them. You can import other files with @path/to/file.md and edit memory quickly with /memory.
The distinction we keep: CLAUDE.md tells Claude how the project works (commands, conventions, where things live). Settings and hooks enforce what it may do. "Never edit applied migrations" is a sentence in CLAUDE.md and a PreToolUse hook, because the sentence alone is a request.
Subagents
Subagents are specialised assistants with their own system prompt, tool list and separate context window. Project subagents live in .claude/agents/ as Markdown files with frontmatter:
---
name: migration-reviewer
description: Reviews new database migrations for locking and rollback risk. Use after a migration file is created.
tools: Read, Grep, Glob, Bash
---
You review SQL and framework migrations. For each new migration, report:
lock level taken, estimated impact on large tables, whether it is reversible,
and whether application code deployed before it still works. Do not edit files.
Because a subagent starts with a fresh context, it is useful for work that produces a lot of output you do not need in the main conversation, such as searching a large codebase or reviewing a diff. Keep the tool list minimal; a reviewer that cannot edit cannot "fix" what it was asked to review. Manage them with /agents.
A rollout checklist
Commit
.claude/settings.jsonwith narrow allows, explicit denies for secrets and one or two hooks.Add
.claude/settings.local.jsonto.gitignore(Claude Code does this when it creates the file, but check).Keep
CLAUDE.mdshort and factual; link out with imports for long docs.Put hook scripts in
.claude/hooks/, executable, reviewed.If you have managed devices, set organisation-wide denies and disable bypass mode in managed settings.
Key takeaways
Managed settings beat everything; then command line, local, project and user files.
Permission rules are evaluated deny, ask, allow; use
/permissionsto see the effective set.Prefix and path rules are guardrails; credentials and sandboxes are the real boundary.
Hooks enforce rules deterministically; exit code 2 in
PreToolUseblocks the call and explains why.CLAUDE.mddescribes the project; settings and hooks enforce it; subagents keep side tasks out of the main context.
References
Claude Code settings: https://code.claude.com/docs/en/settings
Identity and access management (permissions): https://code.claude.com/docs/en/iam
Hooks reference: https://code.claude.com/docs/en/hooks
Memory and CLAUDE.md: https://code.claude.com/docs/en/memory
Subagents: https://code.claude.com/docs/en/sub-agents
Comments
0No comments yet. Be the first to comment!
Sign In to post a comment.