Guide · Claude Code
Claude Code keeps changing files I didn't ask it to. How do I stop it?
You ask Claude Code to fix one button. The diff comes back touching the button, a shared utility, the router and a config file. Nothing is obviously wrong, but now you have to review four files instead of one, and any of them could break something that worked yesterday.
Why Claude Code edits files you didn't mention
- It optimises for "make it work", not "change only this". If a fix is easier in a shared helper, it changes the helper.
- It doesn't know which code is finished. To the agent, a login flow you shipped last month looks exactly like code nobody has touched.
- "While I'm here" tidying. Renames, formatting and small refactors creep in because they look like improvements.
- Your instructions are requests, not rules. "Only change Button.tsx" in a prompt or CLAUDE.md is guidance. Nothing checks that it was followed.
1. Name the files, and what must not change
The cheapest fix is a better prompt. Compare:
| Vague | Scoped |
|---|---|
| Fix the button styling. | Fix the button styling in src/components/Button.tsx only. Don't change other components, routes or tests. If you need another file, stop and ask. |
This helps a lot, but on long tasks and long sessions the boundary fades. Treat it as the first layer, not the last.
2. Use Claude Code's own permissions and hooks
Claude Code lets you set permission rules and run hooks before or after its tool calls (configured in .claude/settings.json). You can deny edits to paths that should never change, such as migrations or lockfiles, and run a script after edits. This is useful for paths that are always off-limits. It's harder to use for the per-task question "which files may this task touch?", because that changes every time.
3. Set a scope for each task
A scope is a short, explicit list of what this task may change, agreed before the agent starts. With Stet it's one line:
npx stet-cli init
stet scope set "Fix button styling" --allow "src/components/Button.tsx,src/components/Button.test.tsx"
Stet writes the scope into AGENTS.md and CLAUDE.md, so the agent sees it at the start of the session, and Claude Code can read it over MCP (stet init --mcp).
4. Check the diff against the scope, every time
The part that makes a scope real is a check the agent can't talk its way around:
stet guard
✗ 2 files changed outside the task scope:
src/lib/format.ts
src/router.tsx
stet guard compares everything changed since the scope was set (committed, staged, unstaged and new files) with the allowed globs. Install the git hook and out-of-scope changes are blocked at commit:
stet hook install
For teams, the Stet GitHub Action runs the same check on every pull request.
What to do when the agent genuinely needs another file
Sometimes the extra file is right: the fix really does belong in the shared helper. That's a decision, so make it on purpose: widen the scope, note why, and let the agent continue. The point isn't to forbid change, it's to make every change outside the task visible and deliberate.
FAQ
Can I stop Claude Code from editing certain files completely?
Yes. Claude Code permission rules can deny edits to specific paths, and Stet can mark protected paths that guard always flags. Use these for files that should never change, such as migrations, lockfiles and generated code.
Does CLAUDE.md stop Claude Code from changing other files?
It helps, because Claude Code reads it at the start of a session, but it is an instruction, not a check. Pair it with a check that compares the diff to the agreed scope, such as stet guard.
Does this work with Cursor and Codex too?
Yes. The scope lives in AGENTS.md, which Codex, Cursor and other agents read, and stet guard checks the git diff, so it works whichever agent made the change.
Put guardrails on your AI coding agent
Stet is a free, open-source CLI and MCP server for Claude Code, Cursor, Codex and Copilot. It runs locally: no account, your code never leaves your machine.
npx stet-cli init · Install · Setup guide · GitHub