Claude Code edits files you did not ask it to touch
Tell Claude Code the scope, and enforce it where it matters: permission deny rules block edits to paths you name, plan mode shows the change before any file is touched, and Esc Esc or /rewind undoes edits it already made. In Cursor use checkpoints; in Codex use a read-only sandbox while you plan.
In Claude Code
- Name the files in the request: "change only src/billing/invoice.ts". Anthropic's guide also suggests "Only make changes that are directly requested or clearly necessary" in CLAUDE.md.
- Block protected paths with deny rules. Rules are checked deny, then ask, then allow, and Edit rules also cover Write.
- Use plan mode for anything larger than a one-line change: Shift+Tab, or claude --permission-mode plan. It proposes without editing your source.
- Undo with Esc Esc or /rewind. Checkpoints cover file edits only, not commands that ran, and are separate from git, so commit before big tasks.
- For files that must never change, add a PreToolUse hook that exits with code 2. The docs' example blocks .env, package-lock.json and .git/.
{
"permissions": {
"deny": ["Edit(/.env)", "Edit(/docs/**)"]
}
}Both rule strings are the docs' own examples: Edit(/.env) protects the .env at the project root, and Edit(/docs/**) everything under docs/. Put your own folder in place of docs.
In Cursor and Codex
- Cursor saves checkpoints during an Agent session. Click one in the chat timeline or use Restore Checkpoint; it reverts files only. Cursor's docs say to use git for permanent history.
- Cursor's .cursorignore blocks Agent access to listed files, but the docs say terminal and MCP tools can still reach them.
- Codex has read-only, workspace-write and danger-full-access sandbox modes; switch with /permissions.
When a skill or vault helps, and when it does not
Helps
- A task skill that lists exactly which files a change type touches keeps the agent inside them.
- A vault that maps your codebase helps the agent find the right file instead of editing a look-alike.
Does not help
- Instructions alone are not a fence. Use deny rules or hooks for files that must not change.
- A skill cannot undo an edit; checkpoints and git can.