AI Broke My Code: How to Fix It Without Losing Work
An AI coding tool can make a change that looks plausible but breaks your build, a feature, or the whole app. Don't start by deleting files or asking the AI to rewrite everything. First identify what changed, preserve any work worth keeping, and use Git to return to a known-good state. This guide walks through that recovery process step by step.
Practice Git workflows →1. Pause and capture the current state
Avoid making more edits until you know what is broken. In your project's terminal, check the working tree and the latest commit:
git status --short
git log -5 --onelineIf you have valuable changes that are not committed, copy the project or create a backup branch before attempting a broad rollback. Git cannot recover unsaved editor text, so save your files first. Avoid commands such as git reset --hard until you understand exactly what they will discard.
2. Find the change that caused the problem
Reproduce the issue if possible, then inspect the files changed since the last known-good commit. For uncommitted edits, review the diff:
git diff --stat
git diff
git diff --checkAlso run the project's existing test or build command and note the first meaningful error. Compare that output with the changed files instead of guessing. If the AI has already committed its edits, inspect that commit with git show --stat --oneline HEAD and git show HEAD. Untracked files do not appear in git diff, so include git status in your review.
3. Choose the smallest safe recovery
Undo one uncommitted file
If a tracked file contains only unwanted, unstaged changes, restore it from the index:
git diff -- path/to/file
git restore -- path/to/fileThis discards unstaged edits in that file. If you have both useful and unwanted changes in it, use git restore -p to review changes interactively, or keep the edits and repair the specific bug instead.
Undo a commit that has already been shared
If the bad change is in a commit that teammates may have pulled, prefer a new reversing commit rather than rewriting shared history. Read how to undo AI-generated changes safely for a closer look at choosing between restoring files, reverting a commit, and preserving selected edits.
4. Verify the app before moving on
A clean Git status does not prove that the app works. Rerun the failing test, build, or development server, then check the affected feature manually. Review the final changes before committing:
git status
git diff --check
git diffMake the next AI change easier to recover
- Commit or back up a working state before a large AI task.
- Ask for a focused change and review the diff before accepting it.
- Run the same tests or build after each meaningful change.
- Keep credentials out of prompts and source files; see what to do if an API key reaches GitHub.