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 codeLovable 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 syncLovable 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 promptLovable'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
Use @file references to scope every prompt
When you name specific files, Lovable limits its changes to those files and leaves everything else untouched
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.
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
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.
1# AGENTS.md23## Protected Files — Do Not Modify45The following files contain manually written logic. Do not overwrite, delete, or restructure them:67- src/lib/calculations.ts — custom pricing logic8- src/hooks/useCustomAuth.ts — manual auth flow with custom token handling9- src/components/CustomChart.tsx — hand-tuned chart configuration1011## Protected Patterns1213- Never remove or modify functions marked with the comment: // MANUAL — DO NOT OVERWRITE14- Never change the Supabase client initialization in src/integrations/supabase/client.ts15- Always preserve existing imports when adding new ones to a file1617## When Modifying Shared Files1819- When editing App.tsx, do not remove existing route definitions20- When editing tailwind.config.ts, do not remove custom theme extensions21- When editing index.html, do not remove existing meta tags or link tagsExpected result: Lovable respects the preservation rules in AGENTS.md and avoids modifying listed files and patterns.
Commit to GitHub before every AI prompt
A Git commit creates a safe checkpoint you can always revert to if Lovable overwrites something important
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.
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
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
1# Project Agent Instructions23## Critical: Protected Code45These files contain manually written business logic and must NOT be6modified, deleted, or restructured by the AI agent:78### Off-Limits Files9- src/lib/pricing.ts — custom pricing calculation engine10- src/lib/permissions.ts — role-based access control logic11- src/hooks/useStripeCheckout.ts — Stripe integration with manual error handling12- src/components/reports/RevenueChart.tsx — hand-tuned chart config1314### Off-Limits Patterns15- Any function with the comment: // PROTECTED — DO NOT MODIFY16- Any file in src/lib/legacy/ directory17- Database migration files in supabase/migrations/1819## When Adding Features20211. Create new files instead of modifying protected files222. Import from protected files but do not edit them233. When editing App.tsx, only add new Route entries — do not remove existing ones244. When editing package.json, only add dependencies — do not remove existing ones2526## Technology Preferences2728- Use @tanstack/react-query for data fetching29- Use zod + react-hook-form for form validation30- Use date-fns for date operations31- Use Tailwind CSS for all styling — no CSS modules3233## Code Style3435- TypeScript strict mode36- Named exports (not default exports)37- Use the @/ import alias for all project filesBest practices to prevent this
- 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
Still stuck?
Copy one of these prompts to get a personalized, step-by-step explanation.
I am using Lovable.dev and the AI keeps overwriting my manual code changes when I send new prompts. I need a strategy to protect my custom code. Here are the files I have manually edited: - [list your manually edited files] Here is what I need Lovable to change next: - [describe the feature or fix] Please help me: 1. Write an AGENTS.md file that protects my manual edits 2. Rewrite my prompt using @file references to scope the changes 3. Suggest how to restructure my code so manual edits live in dedicated files that are easier to protect
Before making any changes, check @AGENTS.md for the list of protected files and patterns. I need you to @src/components/Dashboard.tsx add a new statistics card showing total revenue. Do NOT modify any other files. Do NOT change existing component logic in Dashboard.tsx — only add the new card below the existing content.
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.
Talk to an Expert
Our team has built 600+ apps. Get personalized help with your issue.
Book a free consultation