Try free

Last updated 13 October 2026

Prevent Claude Code from editing git folders

The risk of automated git manipulation

Claude Code uses the Bash tool to interact with your local environment. This gives the agent the power to execute shell commands and modify files across your project directory. If you provide a broad or ambiguous task, the agent might decide to fix a git conflict, modify internal hooks, or change configuration files. This is often dangerous because the agent might not understand your specific local workflow or the nuances of your branching strategy.

When the agent attempts to fix repository issues, it may try to edit files inside the .git folder or change your git config settings. Most developers want to keep their git history and internal metadata under manual control. Allowing an automated agent to rewrite history or modify internal object files can lead to repository corruption or data loss. You need to establish clear boundaries to ensure the agent writes code without touching the plumbing of your version control system.

Establish project boundaries with CLAUDE.md

The most direct way to control the agent is through specific project instructions. Create a file named CLAUDE.md in the root of your repository. This file acts as a persistent guide for the agent during every session in that folder. You should use this file to explicitly define what the agent is forbidden from doing. Write a firm instruction that the agent must never read or write any file inside the .git directory or modify any hidden files starting with a dot unless specifically requested.

Your CLAUDE.md file should also list preferred shell commands. If you have specific scripts for handling git tasks, tell the agent to use those instead of raw bash commands. By giving the agent a clear set of rules, you reduce the chance of it trying to be helpful in ways that disrupt your repository structure. The agent reads these instructions at the start of the session and refers to them when planning its tasks.

Apply global protection across all projects

If you work on multiple repositories, setting rules for each one individually is tedious. You can set global instructions by creating a file at ~/.claude/CLAUDE.md. This file applies to every instance of the claude command you run on your machine. This is the best place to put your most restrictive rules. Explicitly state that the agent is never allowed to modify the git index or run commands that alter the git history without your direct supervision.

This global configuration file provides a safety net. Even if you forget to add a project-specific instruction file to a new repo, the global settings will still influence the agent's behavior. Use active and plain language to tell the agent to avoid internal configuration directories. This ensures that your local environment remains consistent across every project you open with the agent.

Configure permissions in settings files

Claude Code uses settings.json files to manage how tools are used. You can find these files in the .claude folder of your project or in your home directory at ~/.claude/settings.json. These files allow you to define permission levels for various tools. You can configure the agent so that it must ask for approval before using the Write tool on sensitive paths. This creates a manual gate that prevents the agent from making silent changes to your configuration files.

By adjusting the permissions key in your settings, you gain more control over the Bash and Write tools. If the agent tries to perform an action that violates these settings, it will prompt you for feedback. This is particularly useful for protecting your .git folder. You can also use .claude/settings.local.json for overrides that you do not want to track in your repository, allowing you to have personal security rules that differ from the rest of your team.

Monitor agent actions with permission modes

Inside a session, you can change the level of oversight the agent requires. Pressing Shift+Tab cycles through different permission modes such as default, plan, and acceptEdits. If you are working on a sensitive part of your project where git state is critical, switch to plan mode. In this mode, the agent will describe exactly what it wants to do but will not execute any commands or write any files. This allows you to vet every step before it happens.

The acceptEdits mode is useful when you trust the agent with code but still want to see the diffs. However, for maximum safety regarding git folders, the default mode or plan mode is superior. If the agent ever starts a task that looks like it might touch your git configuration, you can press Escape to interrupt it. Pressing Escape twice will even let you go back to earlier messages in the conversation to effectively undo the agent's recent thought process.

Maintain visibility with adhdTerminal

When you run many Claude Code sessions at once, it becomes harder to keep track of what each agent is doing. adhdTerminal provides a visual wall where each session is a tile. This tile shows the last exchanges of the conversation and the current status of the agent. If one agent begins to interact with your git folder or runs a bash command that looks suspicious, you can see it instantly on your dashboard without switching through multiple terminal tabs.

This high-level view is an honest answer to the problem of oversight. Since adhdTerminal tiles reopen in the same place after a restart and show whether an agent is working or waiting, you can maintain a safe development environment across dozens of projects. You can even use the free iPhone app to check the status of your wall while away from your desk. This visibility ensures that no agent is making unauthorized changes to your git repositories in the background.

Leverage gitignore for file visibility

Claude Code is designed to respect your .gitignore file. This is your first line of defense for keeping the agent away from files it should not touch. Ensure your .git folder and any sensitive local configuration files are properly listed in your project's .gitignore. When the agent uses the Glob or Grep tools to search for files, it will typically ignore the paths you have excluded. This prevents the agent from even seeing the files it might otherwise try to modify.

You should regularly check your .gitignore to make sure it covers all temporary files and internal directories. If a file is not tracked by git and is listed in the ignore file, the agent treats it as out of bounds for its standard Read and Write operations. This behavior is built into the core logic of the tool to help keep your workspace clean and prevent the agent from being overwhelmed by irrelevant data.

Use slash commands to reset and clean context

Sometimes an agent might get stuck in a loop or forget its instructions about avoiding certain folders. If you notice the agent is repeatedly trying to access the .git directory, use the /clear command. This will start the session over in the same folder. It wipes the current conversation context, which often fixes issues where the agent has become confused by its own previous mistakes or long chat history.

You can also use /compact to summarize the conversation so far. This frees up space in the context window while keeping the most important instructions alive. If you have defined git protection rules in your CLAUDE.md, a /compact command will help ensure those rules stay within the agent's immediate focus. Regularly checking the /status of your session can also give you insight into what the agent believes its current task and constraints are.

Recovering from unauthorized changes

If the agent does manage to modify something it should not have, you can use the terminal's built-in tools to recover. Press Ctrl+C to quit the session immediately if you see a dangerous command being planned. You can then use your standard shell to run git commands and revert any accidental changes to the filesystem. Because the agent is just a tool running in your shell, it has no more power than you do, and its actions can be undone.

For internal session recovery, remember that pressing Escape twice allows you to select an earlier point in the conversation. This rolls back the agent's memory of the session to that point. Combined with the /clear command and your local git history, you have full control over the state of your project. Always keep a clean git status before starting a complex session with the agent so that you can easily see what has changed.

Questions people ask

Does Claude Code automatically know not to touch .git?

The agent generally respects .gitignore files and avoids internal directories by default. However, it uses the Bash tool which can access any folder if the agent decides it is necessary for the task. You should use CLAUDE.md to provide explicit instructions to never modify that directory.

Can I block the agent from using git commands entirely?

You can use the settings.json file to restrict the Bash tool or require manual approval for all shell commands. By setting permissions to require user intervention, you can deny any command that looks like a git operation. This ensures the agent only runs the specific commands you approve.

What happens if the agent overwrites my CLAUDE.md file?

The agent can theoretically modify its own instruction files if it has Write tool permissions. To prevent this, you can include a rule within CLAUDE.md itself stating that the agent is not allowed to edit that file. You can also set the file to read-only at the OS level for extra security.

Read next

More on when it goes wrong·All posts