Block Claude Code from editing .env and .git: deny rules or a 29-line hook
A earlier guide's shell hook missed file edits, since the agent can change files with its edit tools without running any command. That leaves .env, .git/config, and Claude's own settings open to change, including switching off the guard itself. The article explains two options in Claude Code: a deny rule in settings.json, which the tool enforces directly, and a PreToolUse hook that inspects each edit's path before it runs. Folders like .git and .claude already prompt for approval in default and acceptEdits modes, though not in bypassPermissions mode, and .env is not on that list. The guide provides a 29-line Node.js hook script to block matching paths, and it was tested on Claude Code 2.1.332 against the reference documentation dated 27 November 2026.
This is an AI-generated summary. ShortSingh links to the original source for the complete article.
Discussion (0)
Log in to join the discussion and vote.
Log in