Answer: You block Claude Code from editing protected files by using a hook that calls a separate script, which checks the target file path against a list of protected patterns and exits with code 2 to block the edit. The script is saved to `.claude/hooks/protect-files.sh`. Status: Confirmed Entry: tool/claude-code/permissions-and-security/block-protected-file-edits Revision: 4 Digest: sha256:02e865f13a6734c6 (changes whenever the content changes) Timeline: Created: 2026-11-13T03:04:23Z Last changed: 2026-11-18T12:16:46Z (example protected files: — → .env) Last verified: 2026-11-20T12:21:20Z Next check: 2026-11-21T12:21:20Z Check interval: Daily Expected change: No fixed schedule Data: Field | Value | Source | As of hook script path | .claude/hooks/protect-files.sh | [1] | 2026-11-20 exit code to block edit | 2 | [1] | 2026-11-20 tools blocked | Edit | [1] | 2026-11-20 example protected files | .env | [1] | 2026-11-20 Sources: [1] Anthropic. "Automate actions with hooks". https://code.claude.com/docs/en/hooks-guide.md Role: primary. Accessed: 2026-11-20T12:21:20Z Quote: "Save this to `.claude/hooks/protect-files.sh`:" (Supports: hook script path) Quote: "exits with code 2 to block the edit" (Supports: exit code to block edit) Quote: "Stop Claude's `Edit` and `Write` tools from modifying sensitive files" (Supports: tools blocked) Quote: "sensitive files like `.env`, `package-lock.json`, or anything in `.git/`" (Supports: example protected files) Published: 2026-11-20T10:36:06Z Snapshot SHA-256: 2076ece854a4d4be8c0a9c7568db4e9abc0e35153c22975adf750c6f8f3166e9 (captured 2026-11-17T12:26:31Z) Verification: Method: Automated extraction from [1] with deepseek-flash (JSON output, thinking disabled). Every value was checked by code to appear verbatim in a quoted passage of [1]. On this check the text of [1] was unchanged since 2026-11-17T12:26:32Z, so the values verified then were kept. Cross-check: not-available at 2026-11-20T12:21:20Z. No independent second source is configured for this entry Last successful check: 2026-11-20T12:21:20Z Last attempt: 2026-11-20T12:21:20Z Notes and limitations: Extracted automatically from a single official source (section "Block edits to protected files"). Related: https://latestn.com/tool/claude-code/permissions-and-security/block-reads-generated-vendored-code How do I block reads of generated and vendored code in Claude Code? https://latestn.com/tool/claude-code/permissions-and-security/built-in-security-features What built-in security features does Claude Code have? https://latestn.com/tool/claude-code/permissions-and-security/change-permission-mode How do I change the permission mode for a Claude Code SDK agent? https://latestn.com/tool/claude-code/permissions-and-security/available-permission-modes-2 What permission modes are available in Claude Code and what does each auto-approve? https://latestn.com/tool/claude-code/permissions-and-security/aws-credentials-sigv4-api-key How do I authenticate Claude Code with AWS credentials using SigV4 or a workspace API key? https://latestn.com/tool/claude-code/permissions-and-security/block-on-hook-failure How do I block a tool call in Claude Code when a hook callback fails? https://latestn.com/tool/ All entries in AI tools Change log: Time | Event | Change | Source 2026-11-18T12:16:46Z | changed | example protected files: — → .env | [1] 2026-11-18T12:16:46Z | changed | tools blocked: — → Edit | [1] 2026-11-18T12:16:46Z | changed | exit code to block edit: — → 2 | [1] 2026-11-18T12:16:46Z | changed | hook script path: — → .claude/hooks/protect-files.sh | [1] 2026-11-18T12:16:46Z | changed | blocked tools: Edit → — | [1] 2026-11-18T12:16:46Z | changed | protected file examples: .env → — | [1] 2026-11-18T12:16:46Z | changed | script path: .claude/hooks/protect-files.sh → — | [1] 2026-11-18T12:16:46Z | changed | blocking exit code: 2 → — | [1] 2026-11-15T12:21:20Z | changed | blocked tools: — → Edit | [1] 2026-11-15T12:21:20Z | changed | script path: — → .claude/hooks/protect-files.sh | [1] 2026-11-15T12:21:20Z | changed | tools blocked: Edit → — | [1] 2026-11-15T12:21:20Z | changed | hook script path: .claude/hooks/protect-files.sh → — | [1] 2026-11-14T12:17:07Z | changed | tools blocked: — → Edit | [1] 2026-11-14T12:17:07Z | changed | hook script path: — → .claude/hooks/protect-files.sh | [1] 2026-11-14T12:17:07Z | changed | script path: .claude/hooks/protect-files.sh → — | [1] 2026-11-13T03:04:23Z | added | Entry created | — Changes for this entry as a feed: https://latestn.com/tool/claude-code/permissions-and-security/block-protected-file-edits/latest Challenge this entry: Open challenges: None How: If a value here is wrong or out of date, send what is wrong and a link to evidence. Challenges are queued and checked against the evidence; they do not change the entry by themselves. If the entry changes as a result, the change log says so. Form: https://latestn.com/dispute/tool/claude-code/permissions-and-security/block-protected-file-edits POST: https://latestn.com/dispute/tool/claude-code/permissions-and-security/block-protected-file-edits with claim, evidence_url, and optionally field and contact (form-encoded or JSON) GET: https://latestn.com/dispute/tool/claude-code/permissions-and-security/block-protected-file-edits?claim={what is wrong}&evidence_url={link} How to cite: LatestN. (2026). How do I block Claude Code from editing protected files with a hook? Revision 4, last verified 2026-11-20T12:21:20Z. https://latestn.com/tool/claude-code/permissions-and-security/block-protected-file-edits (accessed 2026-11-21T10:01:47Z). @misc{LatestN_tool_claude_code_permissions_and_security_block_protected_file_edits_r4, title = {How do I block Claude Code from editing protected files with a hook?}, author = {{LatestN}}, year = {2026}, howpublished = {\url{https://latestn.com/tool/claude-code/permissions-and-security/block-protected-file-edits}}, note = {Revision 4; digest sha256:02e865f13a6734c6; last verified 2026-11-20T12:21:20Z; accessed 2026-11-21T10:01:47Z} } Reuse: License: CC BY 4.0 (https://creativecommons.org/licenses/by/4.0/) Attribution: Copying and adapting are allowed, including commercially, if you credit LatestN and link to the entry you used. Each entry ends with a ready-made citation. URL: https://latestn.com/tool/claude-code/permissions-and-security/block-protected-file-edits