HOW-TO GUIDE

How to keep Cursor, Claude Code, and Copilot from reading your .env files

Based on a guide by Mathias Andersen on DEV Community · Nov 29, 2026

You'll end with: A repo where eight ignore and permission files keep .env secrets out of Git and out of each AI coding assistant's reads and context.

Why it's worth having: .gitignore only controls Git, so an assistant inside your editor can still read .env files from disk and sweep keys into cloud prompts.

At a glance

WHO IT'S FOR
IT and OT engineers and developers who keep API keys and database URLs in .env files.
LEVEL
Beginner
COST
Free. Every layer is a plain config file you write yourself.
YOU NEED
A repo with .env files · Your AI coding assistant's settings files

The method, in short

  1. 1Keep .env and .env.* in .gitignore, and cover .env* in .cursorignore, .windsurfignore, and .aiexclude.
  2. 2In .claude/settings.json, add permissions.deny rules for Read(./.env) and Read(./.env.*), and list both in .claudeignore.
  3. 3In .vscode/settings.json, map .env* files to dotenv and turn GitHub Copilot off for dotenv.
  4. 4Tell Copilot agents in .github/copilot-instructions.md never to read, grep, or echo .env files, and keep .env closed on screen shares.

The post lists all eight files, the exact rule for each assistant, and the screen-share risk.