Try free

Last updated 13 October 2026

How to keep Claude Code from editing files

The risk of unintended file modifications

A coding agent needs a level of autonomy to be useful but that same autonomy can lead to mistakes. You might ask Claude Code to explain a logic flow and find that it has decided to refactor your configuration files or rewrite your environment templates. This happens because the agent is designed to be proactive and fix issues it notices while performing its primary task. If you do not set boundaries early the agent might use the Edit tool or the Write tool on files that are critical to your infrastructure or security.

The panic of seeing a terminal window scroll through hundreds of lines of unintended diffs is a common experience for developers moving from simple chat interfaces to terminal agents. You need a way to tell the agent exactly where the walls are. This is not just about preventing bugs but also about protecting sensitive data like API keys or personal notes that might be sitting in your project directory. Establishing these boundaries ensures that you remain the final authority on what changes are committed to your disk.

Relying on standard git ignore rules

Claude Code respects your .gitignore file by default. This is the most effective first line of defense for any developer. If you have files that should never be touched by an agent you should ensure they are properly listed in your .gitignore. This includes node modules, build artifacts, local environment files, and IDE specific configuration folders. When the agent uses the Glob tool or the Grep tool it will skip these paths entirely. This prevents the agent from even seeing the files which is the safest way to keep them from being edited.

If you have files that you want to keep in your repository but protect from the agent you can still use this mechanism. You might temporarily add a specific file path to your .gitignore while you are working with the agent. This is a quick manual way to toggle protection for a sensitive file without setting up complex configuration files. Once you finish your session you can remove the path from .gitignore to return to your normal development flow. The agent will not try to bypass these rules as it is built to follow the standard conventions of your workspace.

Defining project boundaries in CLAUDE.md

The most powerful tool for controlling behavior is the CLAUDE.md file. You should place this file in the root of your repository to provide high level instructions that the agent reads at the start of every session. Use this file to list specific directories or files that the agent is allowed to read but never write to. You can be very direct in your language. Tell the agent that the Edit tool and the Write tool are strictly forbidden for any file ending in a specific extension or located in a protected folder like production-configs.

You can also use CLAUDE.md files in subdirectories to create local rules. A CLAUDE.md file in a subfolder applies specifically to that part of the file tree. This is useful for large monorepos where different teams might have different rules about agent autonomy. If you have a legacy directory that is very fragile you can place a CLAUDE.md file there with a single instruction to never modify any content. The agent will respect these local instructions over the more general rules found in the root of the project. This hierarchical approach allows for fine grained control across complex codebases.

Using permission modes for manual oversight

You can control the level of autonomy the agent has by switching permission modes within a session. By default the agent might ask for permission before using certain tools but you can make this more restrictive. Pressing Shift+Tab allows you to cycle through different modes. The plan mode is particularly helpful for protecting your files. In this mode the agent will think through the problem and describe the changes it wants to make but it will not execute the tools to actually change the files. You get to see the intended outcome before a single character is modified.

If you want even more safety you can use the bypassPermissions mode carefully or stick to the default mode which prompts you for significant actions. When you see a prompt asking to use the Edit tool you have the opportunity to deny it. If the agent is consistently trying to edit files it should not touch you should use the /config command to check your current settings. You can also use the /permissions command to see a summary of what the agent is allowed to do. This manual oversight is your final gatekeeper against unwanted modifications during an interactive session.

Configuring tool restrictions in settings.json

For a more permanent solution you can modify the .claude/settings.json file in your project. This file allows you to define specific permissions for each tool the agent uses. You can find the permissions section and explicitly set the Edit tool or the Write tool to ask for permission every time or even deny them for the entire project. This is a hard technical limit that the agent cannot talk its way out of. If you set a tool to deny the agent will simply tell you it does not have the necessary permissions to perform that action.

You can also use the ~/.claude/settings.json file for global restrictions that apply to every project on your computer. This is the place to put rules that should never be broken regardless of the specific repository you are in. For example you might want to prevent the agent from ever using the Bash tool to run commands that delete files or modify system settings. By layering these settings files you create a defense in depth strategy. The global settings provide a safety net while the project specific settings allow for the flexibility needed to get work done.

Visualizing agent activity with adhdTerminal

Managing these boundaries becomes much harder when you are running multiple tasks at once. If you have several sessions open it is easy to lose track of which one has permission to edit files. This is where adhdTerminal becomes a useful tool for a developer. It runs your Claude Code sessions as tiles on a wall. Each tile shows the last exchanges of the conversation and a status indicator. You can quickly see if an agent in one tile is currently using the Edit tool while you are focused on a different task in another tile.

The visual feedback from the tiles helps you maintain situational awareness. If you notice a tile representing a sensitive project is suddenly showing a long list of file edits you can jump in and interrupt the process by clicking into the tile and pressing Escape. This is much faster than hunting through multiple terminal tabs to find the specific session that is making changes. The adhdTerminal app keeps your various agent sessions organized and visible so you can enforce your file protection rules without being overwhelmed by the complexity of your workflow.

Using local overrides for personal safety

There are times when you need to protect files that are specific to your local machine such as custom build scripts or personal notes that are not part of the git repository. You should use the .claude/settings.local.json file for this. This file is intended for your own overrides and should not be tracked by your version control system. You can define very strict rules here that only apply to you. This is perfect for a scenario where the rest of your team allows the agent to edit a certain config file but you have local modifications you want to keep safe.

The agent will prioritize the settings in your local file over the project wide settings.json. This allows you to be more restrictive than your teammates without bothering them with your personal workflow requirements. You can also use this file to experiment with different permission levels before suggesting them as a project wide standard. If you find a specific tool restriction that works well for protecting your code you can then move that setting into the main CLAUDE.md or settings.json file for everyone to use.

Limiting agent scope with specific commands

The way you start a session can also help protect your files. If you only need a quick answer or a code review without any edits you can use the claude -p command. This runs a single prompt and prints the answer without opening the full interactive interface. Since the agent is not in a continuous loop it is much less likely to start making unauthorized changes to your files. It is a great way to get context or help with a specific line of code while keeping the rest of your project completely safe.

Inside a session you can use the /add-dir command to selectively add only the directories you want the agent to focus on. By being selective about the context you provide you naturally limit the agent's ability to edit files outside of those paths. If you find the agent is looking at too many files and you are worried about its next move you can use the /clear command. This wipes the current session context within that folder and lets you start over with a narrower focus. Keeping the context window small is a practical way to prevent the agent from wandering into sensitive areas of your codebase.

Managing global instructions for total control

Your home directory contains a hidden .claude folder where you can place a global CLAUDE.md file. This file is the ultimate authority for your personal machine. You can use it to define a set of core principles that every agent session must follow. For example you can state that the agent should never attempt to modify any file in your home directory outside of the current project path. This protects your SSH keys, shell configurations, and other personal data from an agent that might get too aggressive with its Bash tool or Write tool.

This global protection is especially important if you use adhdTerminal to manage many concurrent projects. When you have a wall of tiles running different tasks the global CLAUDE.md ensures that every single one of those sessions respects your primary safety rules. You do not have to worry about forgetting to set up protection for a new project because the global instructions are always in effect. This gives you the confidence to let the agent work on multiple parts of your system at the same time knowing that your most sensitive files are behind a permanent shield.

Questions people ask

Can I prevent the agent from running specific shell commands?

You can control this in your settings.json file under the permissions section for the Bash tool. While you cannot easily block individual strings you can set the Bash tool to always ask for permission. This ensures you see every command before it runs and can deny anything that looks like it might modify protected files.

How do I stop an agent that has already started making unwanted edits?

Press the Escape key immediately to interrupt the current action. If you need to revert changes you can press Escape twice to see a list of earlier messages and return to a state before the edits began. You can also use your own git tools to discard the changes if the agent has already finished writing to the disk.

Does the agent follow rules inside custom subagents?

Yes, any subagent defined in the .claude/agents directory will still respect the global and project-specific instructions in CLAUDE.md and settings.json. You should include explicit boundary instructions in your subagent markdown files to ensure they remain focused on their specific tasks without straying into protected files.

Read next

More on claude code basics·All posts