# Preventing Lovable from Overwriting Manual Code Edits

- Tool: Lovable
- Difficulty: Intermediate
- Fix time: ~10 min
- Compatibility: All Lovable plans
- Last updated: March 2026

## TL;DR

Lovable overwrites files when your prompt does not scope the changes precisely. Prevent this by using @file references to target specific files, adding preservation rules to AGENTS.md, committing your changes to GitHub before prompting, and using Plan Mode to review proposed changes before they execute. These techniques protect your manual edits from being replaced.

## Why Lovable overwrites your manual code changes

Lovable's AI agent works by reading your prompt and deciding which files need modification. When your prompt is broad (like 'fix the styling on this page'), Lovable may rewrite entire files — including sections you carefully edited by hand. The AI does not know which lines you wrote manually versus which lines it generated previously.

This is especially frustrating after you spent time in Dev Mode or GitHub tweaking specific logic, only to have the next AI prompt overwrite your changes. The root cause is that Lovable treats every file in its context as fair game for modification unless you tell it otherwise.

The solution has three layers. First, scope every prompt to specific files using @file references. Second, create an AGENTS.md file that lists files and functions the AI must not modify. Third, use Plan Mode to preview what the AI will change before letting it execute. Together, these techniques give you reliable control over which code the AI touches.

- Broad prompt without @file references — Lovable modifies any file it thinks is relevant
- No AGENTS.md file to define off-limits areas of the codebase
- Agent Mode executed changes without review — Plan Mode was not used to preview modifications
- Manual edits were not committed to GitHub before the next AI prompt
- Multiple files share similar code patterns, and Lovable modifies the wrong one

## Error messages you might see

**Your custom logic in [filename] was replaced by AI-generated code**

Lovable rewrote a file that contained your manual edits. This is not an error message from the system — it is what you notice when comparing the code before and after an AI prompt. Use version history to revert.

**Merge conflict in [filename] after GitHub sync**

Lovable changed a file that you also edited in GitHub. Since both sides modified the same file, Git cannot automatically merge the changes. Resolve the conflict in GitHub and push the resolution.

**Component behavior changed unexpectedly after AI prompt**

Lovable's edit affected logic you added manually that was adjacent to the area it was modifying. The AI did not realize your logic was intentional and rewrote it as part of the broader change.

## Before you start

- A Lovable project with manual code edits you want to protect
- GitHub connected to your project (recommended for version history backup)
- Familiarity with Dev Mode or the Lovable code editor for creating AGENTS.md

## How to fix it

### 1. Use @file references to scope every prompt

*When you name specific files, Lovable limits its changes to those files and leaves everything else untouched*

Every time you send a prompt to Lovable, reference the exact file or files you want modified using the @ syntax. Instead of 'add a loading spinner to the user list', write '@src/components/UserList.tsx add a loading spinner while data is being fetched.' This tells Lovable to only modify UserList.tsx and leave all other files unchanged. You can reference multiple files in one prompt: '@src/components/UserList.tsx and @src/hooks/useUsers.ts add error handling to the data fetch.'

**Expected result:** Lovable modifies only the referenced files. Other files including your manually edited ones remain untouched.

### 2. Create an AGENTS.md file with preservation rules

*AGENTS.md is read at the start of every session and acts as persistent instructions that tell Lovable what not to touch*

Open Dev Mode (click + then Code) and create a file called AGENTS.md in the project root. Add a section listing files and functions that must not be modified. Be specific — list file paths and function names. Lovable reads this file automatically and follows the rules in every prompt.

After:

```
# AGENTS.md

## Protected Files — Do Not Modify

The following files contain manually written logic. Do not overwrite, delete, or restructure them:

- src/lib/calculations.ts — custom pricing logic
- src/hooks/useCustomAuth.ts — manual auth flow with custom token handling
- src/components/CustomChart.tsx — hand-tuned chart configuration

## Protected Patterns

- Never remove or modify functions marked with the comment: // MANUAL — DO NOT OVERWRITE
- Never change the Supabase client initialization in src/integrations/supabase/client.ts
- Always preserve existing imports when adding new ones to a file

## When Modifying Shared Files

- When editing App.tsx, do not remove existing route definitions
- When editing tailwind.config.ts, do not remove custom theme extensions
- When editing index.html, do not remove existing meta tags or link tags
```

**Expected result:** Lovable respects the preservation rules in AGENTS.md and avoids modifying listed files and patterns.

### 3. Commit to GitHub before every AI prompt

*A Git commit creates a safe checkpoint you can always revert to if Lovable overwrites something important*

If your project is connected to GitHub, make sure all your manual edits are committed and pushed before sending a new prompt to Lovable. This way, even if Lovable overwrites your code, you can see exactly what changed in the GitHub diff and revert specific files. If you are not using GitHub, use Lovable's built-in version history: scroll up in the chat panel to see previous versions and revert to any point.

**Expected result:** Your manual edits are safely stored in Git. If Lovable overwrites them, you can restore the previous version from GitHub or Lovable's version history.

### 4. Use Plan Mode to preview changes before execution

*Plan Mode shows you what Lovable intends to change without actually modifying any code — you review and approve first*

Switch to Plan Mode by clicking the mode toggle above the chat input. Send your prompt in Plan Mode. Lovable will analyze the codebase and create a formal plan listing exactly which files it will modify and what changes it will make. Review the plan carefully — if it lists files you do not want changed, modify the plan or reject it. Once the plan looks correct, click 'Implement the plan' to have Agent Mode execute only the approved changes. Plan Mode costs one credit per message but can save many credits by preventing unwanted overwrites. For projects with extensive manual customizations across many files, RapidDev's engineers can help set up a robust AGENTS.md and prompting workflow.

**Expected result:** You see the exact list of files and changes before any code is modified, giving you a chance to protect your manual edits.

## Complete code example

File: `AGENTS.md`

```markdown
# Project Agent Instructions

## Critical: Protected Code

These files contain manually written business logic and must NOT be
modified, deleted, or restructured by the AI agent:

### Off-Limits Files
- src/lib/pricing.ts — custom pricing calculation engine
- src/lib/permissions.ts — role-based access control logic
- src/hooks/useStripeCheckout.ts — Stripe integration with manual error handling
- src/components/reports/RevenueChart.tsx — hand-tuned chart config

### Off-Limits Patterns
- Any function with the comment: // PROTECTED — DO NOT MODIFY
- Any file in src/lib/legacy/ directory
- Database migration files in supabase/migrations/

## When Adding Features

1. Create new files instead of modifying protected files
2. Import from protected files but do not edit them
3. When editing App.tsx, only add new Route entries — do not remove existing ones
4. When editing package.json, only add dependencies — do not remove existing ones

## Technology Preferences

- Use @tanstack/react-query for data fetching
- Use zod + react-hook-form for form validation
- Use date-fns for date operations
- Use Tailwind CSS for all styling — no CSS modules

## Code Style

- TypeScript strict mode
- Named exports (not default exports)
- Use the @/ import alias for all project files
```

## Best practices

- Always use @file references in prompts to limit which files Lovable modifies — broad prompts lead to broad overwrites
- Create AGENTS.md on day one of your project before Lovable generates any code you later customize
- Add the comment '// PROTECTED — DO NOT MODIFY' above critical functions and reference this pattern in AGENTS.md
- Commit to GitHub before every major AI prompt so you always have a safe revert point
- Use Plan Mode for any prompt that affects files you have manually edited — the review step catches unwanted changes
- Keep manual edits in dedicated files (like src/lib/custom.ts) rather than inline in Lovable-generated components — dedicated files are easier to protect
- Review the Lovable chat log after each prompt to see exactly which files were modified, then spot-check them in Dev Mode

## Frequently asked questions

### How do I stop Lovable from overwriting my code?

Use three techniques together: reference specific files with @ syntax in every prompt, create an AGENTS.md file listing protected files and patterns, and use Plan Mode to review changes before they execute. Commit to GitHub before major prompts for an extra safety net.

### What is AGENTS.md and how does it protect my code?

AGENTS.md is a Markdown file in your project root that Lovable reads at the start of every session. Add a list of files and functions the AI must not modify. The agent follows these rules consistently, though it is still wise to review changes in Plan Mode for critical edits.

### Does Lovable have a 'lock file' feature?

There is no built-in file locking feature. However, AGENTS.md serves the same purpose by instructing the AI agent to treat listed files as read-only. Combine this with @file references in prompts to achieve reliable code protection.

### Can I revert if Lovable overwrites my changes?

Yes. Lovable keeps a version history accessible by scrolling up in the chat panel. Click any previous version to revert to that point. If your project is connected to GitHub, you can also revert specific files using Git.

### Should I use Plan Mode for every prompt?

Plan Mode is most valuable when your prompt might affect files you have manually edited. For simple, well-scoped prompts with @file references (like adding a button to a specific component), Agent Mode is fine. Use Plan Mode when the change is broad or touches shared files.

### How do I protect code inside a file that Lovable also needs to edit?

Add the comment '// PROTECTED — DO NOT MODIFY' above the code block and include this pattern in your AGENTS.md. When sending the prompt, explicitly state: 'Do not change any code marked with PROTECTED comments.' This gives the AI a clear signal.

### What if I can't fix this myself?

If you have extensive manual customizations spread across many files and need a reliable workflow to keep them safe during AI-assisted development, RapidDev's engineers have built AGENTS.md configurations and development workflows across 600+ Lovable projects.

---

Source: https://www.rapidevelopers.com/lovable-issues/preventing-lovable-from-overwriting-manual-code-edits
© RapidDev — https://www.rapidevelopers.com/lovable-issues/preventing-lovable-from-overwriting-manual-code-edits
