Stop Cursor from overwriting your finished code by committing to Git before every AI operation, using focused single-task Composer sessions, closing the Agent Review Tab before making manual edits, and adding protected files to .cursorignore. Code overwriting is the most reported critical issue with Cursor, and Git-based safety workflows are the primary defense.
Why Cursor Overwrites Code and How to Prevent It
Code overwriting is the single most reported critical issue with Cursor. The agent can modify unrelated files, remove comments despite instructions, and replace working code with incomplete implementations. The Cursor team identified three root causes: Agent Review Tab file locking conflicts, cloud sync racing with local saves, and Format On Save triggering after AI edits. This tutorial covers every practical defense against unwanted code modifications.
Prerequisites
- Cursor installed (Free or Pro)
- Git initialized in your project
- Understanding of basic Git commands (commit, diff, reset)
- An active project where you need to protect existing code
Step-by-step guide
Commit to Git before every AI operation
Commit to Git before every AI operation
Make 'git commit before AI prompt' a habit. This creates a checkpoint you can always return to if Cursor modifies code unexpectedly. Use a quick checkpoint commit before starting any Composer or Agent session.
1# Before every Composer session:2git add -A && git commit -m "checkpoint: before AI edit"34# If Cursor breaks something:5git diff # See what changed6git checkout -- path/to/file.ts # Revert specific file7# Or revert everything:8git reset --hard HEAD # Nuclear option: revert all changesPro tip: Create a Git alias for quick checkpoints: git config --global alias.cp '!git add -A && git commit -m "checkpoint"'. Then just type 'git cp' before each AI session.
Expected result: Every AI operation has a Git checkpoint you can revert to if anything goes wrong.
Use single-task Composer sessions
Use single-task Composer sessions
The primary cause of unwanted overwrites is scope creep in Composer sessions. When a session exceeds about 20 messages, the agent loses track of which files it should and should not modify. Keep sessions focused on one task and start a new session (Cmd+N then Cmd+I) for each distinct task.
1// BAD: Long multi-task session2// "Fix the login bug, then add the dashboard feature,3// then refactor the API layer"45// GOOD: Separate sessions, one task each6// Session 1: "Fix the login validation error in LoginPage.tsx"7// Session 2: "Add a dashboard page following the pattern in HomePage.tsx"8// Session 3: "Refactor the API layer to use service classes"Pro tip: If debugging in a Composer session exceeds 20 messages, start a new session with a summary of what has been tried. Fresh sessions prevent context pollution.
Expected result: Each session modifies only the files relevant to its single task.
Close the Agent Review Tab before manual editing
Close the Agent Review Tab before manual editing
A confirmed bug causes the Agent Review Tab to conflict with manual edits. If you have the Review Tab open and make manual changes, Cursor may revert your manual edits when you interact with the agent again. Always close the Agent Review Tab before editing files manually.
1// Workflow to prevent Review Tab conflicts:2// 1. Finish your Composer/Agent session3// 2. Accept or reject all suggested changes4// 3. CLOSE the Agent Review Tab (X button)5// 4. Now make your manual edits safely6// 5. Commit to Git before starting next AI sessionPro tip: The Cursor team addressed the worst 'Zombie Revert' bugs in Cursor 2.3 (The Stability Release). Make sure you are on the latest version.
Expected result: Manual edits are preserved without being reverted by the agent.
Disable cloud sync for project folders
Disable cloud sync for project folders
If your project folder is in a cloud-synced directory (OneDrive, iCloud, Dropbox), file save conflicts can cause Cursor to overwrite recent changes. Move your project to a non-synced directory or exclude it from cloud sync.
1// Check if your project is in a synced folder:2// macOS: ~/Library/Mobile Documents/ (iCloud)3// ~/OneDrive/4// ~/Dropbox/5//6// Move project to a non-synced location:7// mv ~/OneDrive/Projects/myapp ~/Projects/myappExpected result: No file save conflicts between cloud sync and Cursor's AI edits.
Use .cursorignore to protect finished files
Use .cursorignore to protect finished files
For files that are completely finished and should never be modified by AI, add them to .cursorignore. This prevents Agent mode from reading or modifying them even when exploring the codebase autonomously.
1# .cursorignore — protect finished files from AI modification23# Completed and tested modules4src/auth/encryption.ts5src/core/billing-engine.ts67# Generated files that should not be AI-modified8src/generated/9prisma/migrations/1011# Configuration that should remain stable12tsconfig.jsonPro tip: Only add truly finished files to .cursorignore. If you later need to modify them with AI assistance, remove them from .cursorignore first.
Expected result: Protected files are invisible to Cursor's AI and cannot be modified by the agent.
Complete working example
1# ===========================================2# Protected Files — Cursor cannot read or modify these3# ===========================================45# Completed and locked modules6src/auth/encryption.ts7src/auth/token-signing.ts8src/core/billing-engine.ts9src/core/payment-processor.ts1011# Database migrations (immutable once applied)12prisma/migrations/13src/db/migrations/1415# Generated code (managed by code generators)16src/generated/17__generated__/1819# Configuration files (manual changes only)20tsconfig.json21.eslintrc.js22prettier.config.js2324# Environment and secrets25.env26.env.*27!.env.example28*.pem29*.key3031# Lock files32package-lock.json33pnpm-lock.yaml34yarn.lockCommon mistakes when stopping Cursor from Overwriting Code
Why it's a problem: Not committing to Git before AI operations
How to avoid: Make git add -A && git commit -m 'checkpoint' a reflex before every Composer or Agent session.
Why it's a problem: Running multi-task Composer sessions longer than 20 messages
How to avoid: One task per session. Start fresh with Cmd+N for each new task.
Why it's a problem: Making manual edits with the Agent Review Tab still open
How to avoid: Always close the Agent Review Tab (click X) and accept/reject all changes before editing files manually.
Best practices
- Commit to Git before every AI operation as a mandatory workflow step
- Keep Composer sessions focused on a single task with fewer than 20 messages
- Close the Agent Review Tab before making any manual edits
- Move projects out of cloud-synced directories (iCloud, OneDrive, Dropbox)
- Use .cursorignore to protect files that should never be AI-modified
- Use Git branches for experimental Agent tasks and discard branches if sessions go wrong
- Disable Format On Save if it conflicts with AI edits in Cursor Settings
Still stuck?
Copy one of these prompts to get a personalized, step-by-step explanation.
I need a workflow to prevent an AI code editor from overwriting my manual code changes. Generate a checklist of safety steps including Git practices, file protection, and session management for safe AI-assisted coding.
Before making any changes, commit all current work to Git as a checkpoint. Then modify ONLY the files I explicitly mention. Do NOT touch any other files. If you need to change a file I did not mention, ask me first.
Frequently asked questions
Why does Cursor overwrite code I already finished?
Cursor's agent explores and modifies files autonomously in Agent mode. In long sessions, it can lose track of which files are relevant. The confirmed root causes include Agent Review Tab conflicts, cloud sync racing, and Format On Save interference.
Can I protect specific files from Cursor modification?
Yes. Add files to .cursorignore to make them completely invisible to all AI features. Cursor cannot read, index, or modify files in .cursorignore.
How do I recover code that Cursor overwrote?
If you committed to Git before the AI session, run git diff to see changes, then git checkout -- path/to/file.ts to revert specific files. Cursor also creates automatic checkpoints in Agent mode that you can restore from the session history.
Does Cursor have an undo feature for AI changes?
Yes. Cursor creates checkpoints in Agent mode sessions. You can restore to any previous checkpoint. Standard editor undo (Cmd+Z) works for recent changes. Git remains the most reliable recovery method.
Should I disable Format On Save when using Cursor?
If you experience formatting conflicts where Cursor's changes are reformatted incorrectly, disable Format On Save during AI sessions. This was identified by the Cursor team as one root cause of code overwriting.
Talk to an Expert
Our team has built 600+ apps. Get personalized help with your project.
Book a free consultation