Debugging

Claude Code Deleted My Code: The Vibe Coding Git Workflow That Stops It

8 min read By Red Corner

You asked the AI to fix one error. It fixed the error. It also rewrote three files you never mentioned, deleted a function something else depended on, and broke the tests that were passing ten minutes ago. Now you are typing “Claude Code deleted my code” or “Cursor broke my code” into a search bar, because the only way back you can see is a restart.

It is a common story in vibe coding forums. One builder turned Cursor loose on a batch of errors, watched it change things until the logic was wrong and the tests were red, and reset the whole project. Another put it more painfully: the best version of the app was the first one-shot that never got committed, and dozens of prompts later it never got back to that quality.

This is not an AI problem. It is a version control problem, and that one was solved decades ago.

What this problem looks like

The first is the cascading fix. You ask the model to clear a lint warning or a failing test. It restructures a function, which changes a return value, which breaks a call site elsewhere, which it “fixes” by rewriting that too. Ten minutes in, the change touches a dozen files and nobody can say which edit did the damage.

The second is the vanishing feature. A commenter on a lessons-learned thread warned that when asked to refactor, a model will drop existing logic so quickly and quietly that nothing hints it was ever there, and you do not notice until a widget is simply gone.

The third is the forced reset. In a thread about vibe-coded debugging, someone said their AI had force-reset the repository. The programmers replying pointed out that without protected branches and a remote copy, a force push from an AI is as destructive as one from a careless human, and they had seen production wiped by exactly that.

The fourth is the special-case patch, the same problem in disguise. A procurement manager with no coding background who spent six months on an Excel query tool found that every “fix” was a one-off exception for that exact input. Working general behaviour was quietly replaced by if-statements, and with no history to compare against he could not see it happening.

Why Claude Code deletes working code when you vibe code

A coding agent optimises for the instruction in front of it. “Make the tests pass” is satisfied just as well by deleting the test or carving out an exception as by fixing the root cause. The model does not know which parts of the codebase are load-bearing to you, because you never told it.

Its memory of your project is also limited to its context window, the fixed amount of text it can hold at once. As the codebase grows, more code lives outside that window, and a model that cannot see the call site it is about to break has no reason to preserve it. Several builders noticed the tools get more reckless as projects grow.

Most important: without git, there is no diff, the line-by-line list of what changed between two versions. If the only copy of the working code is the one the AI just overwrote, there is nothing to compare, review or restore. Every fix is a one-way door. A builder who kept decent commits said a bad AI run still cost them hours, but not a rebuild from zero, because they could roll back. That is the whole difference.

This feeds the wider cycle described in why 80% of vibe coding is debugging. This article is about the safety net underneath it.

How to use git when vibe coding so nothing is ever lost

Git records snapshots of your project so you can return to any of them. You need a handful of habits, starting now rather than months in.

Step one: set up the repository before your next prompt

Ask Claude Code to do it: “Initialise a git repository, add a sensible .gitignore for this stack, make an initial commit, then create a private GitHub repository and push to it.” Cursor, Lovable, Replit and Bolt have GitHub integrations that do the equivalent. The GitHub copy is your off-site backup: a good version exists somewhere the agent did not touch.

Step two: commit before every AI task and after every working result

This is the core of a vibe coding git workflow:

  • Before handing the AI any non-trivial task, commit what you have with a message describing the working state, such as “checkout flow working, tests green”.
  • Ask for one change at a time. “Fix the failing test in checkout” is a task. “Fix all the errors” is an invitation to rewrite the app.
  • When the change works and tests still pass, commit again immediately. One feature per commit, or you cannot undo one without undoing all of them.
  • If the result is worse, do not argue with the AI for an hour. Run git checkout . to discard every uncommitted change, and re-prompt from the last good snapshot with tighter instructions.

Put “commit after every successful task” in your CLAUDE.md, the file of standing rules Claude Code reads at the start of each session, and it will do this for you. Cursor rules and their equivalents work the same way.

Step three: give the AI a branch, not your main code

A branch is a parallel copy of the project where changes happen without touching the main version. Before a refactor, ask: “Create a branch called refactor-auth and do the work there.” When it is tested, merge it back; when it is a mess, delete it. On GitHub, turn on branch protection for main so nothing, human or agent, can force push over it.

Step four: write the traps into CLAUDE.md

The builders who stopped losing code wrote down the rules the AI kept breaking:

  • Never delete or rewrite existing functions unless the task asks for it. When refactoring, move code; do not change behaviour.
  • Fix the reported bug first; do not touch linting or formatting in the same change.
  • Never run destructive git commands such as force push, hard reset or branch deletion without asking.
  • After every change, run the test suite and report results before declaring the task done.

Claude Code also supports hooks, small scripts that run automatically around its actions; a common use is blocking dangerous git commands outright. Ask it to set one up.

Step five: tests as the tripwire, reflog as the parachute

Version control tells you what changed; tests tell you whether it matters. The builder with a large test suite said the suite flagged regressions on its own whenever the AI modified code. Ask for a test with every fix; see how to test a vibe coded app.

If something has already gone wrong, ask Claude Code to show you git log (the list of past snapshots) and git reflog (everything the repository has pointed at, including “deleted” branches). Programmers in the forced-reset thread noted that almost anything is recoverable from reflog if a commit ever existed. Have the AI explain each command first.

How a Red Corner CTO would have prevented this problem

A CTO does not treat version control as something you pick up later. It is the first thing set up, before the first feature, because everything else depends on being able to undo.

Before the first prompt, the CTO would have insisted on a repository with a remote on GitHub, a protected main branch, and a CLAUDE.md containing the no-destructive-commands rule and the one-task-per-change rule. That is under an hour in week one, and it is the hour that saves the project.

During the build, the CTO would set the rhythm: small task, tests green, commit, next task. In the first code review they would read the commit history first. A history full of “fix”, “fix again”, “fix for real” tells them the builder is fighting the AI without a checkpoint, and they would repair the workflow before more features went in. They would also read diffs for the pattern the procurement manager missed: a fix that adds a special case rather than changing the general rule. A CTO catches that on sight and asks: is this a general fix or a patch for one input?

Before launch, the CTO would require that every risky change goes through a branch and a pull request, the review step where changes are inspected before joining main. A pull request is the last moment where “the AI deleted the authorisation check” is a comment on a diff rather than a security incident. They would also verify the tests check behaviour rather than merely pass; one engineer who inherited a fully automated client codebase found it full of tests nobody could explain.

After launch, the CTO would make sure deploys come from tagged commits, so a bad release rolls back to a known version in minutes, and that no agent holds credentials to push to production directly. The people who lost the most in these threads had no human in the loop and nothing between the model and main.

Recovering a repository is possible; never needing to is the point.

Frequently asked questions

Can I get my code back if Claude Code or Cursor deleted it?

If you ever committed it, almost certainly: ask the tool to run git reflog and git log and restore the last good commit. If you never used git, check your editor’s local history, any cloud backup, and the chat transcript where the AI printed the earlier version before rebuilding.

Should I let the AI commit and push on its own?

Let it commit, because frequent small commits are the safety net. Be stricter about pushing: keep main behind branch protection, never allow force pushes, and make the agent work on branches. What merges stays a human decision.

Does Lovable or Replit handle version control for me?

They keep a project history and can connect to GitHub, but the discipline is the same: connect the repository, sync often, and make risky changes somewhere you can throw away. If the project is already tangled, untangling a vibe coded codebase shows how to do that with git underneath.

Get a CTO in your corner

Losing working code to an over-eager fix costs a weekend the first time and a launch the second. Red Corner puts real CTOs in your corner to set up this workflow, review the diffs and catch the patch-instead-of-fix before it ships. Get a CTO in your corner at redcorner.io.

Talk to your CTO before you commit to anything.

Every member starts with a call. We make sure we can help you, and that you are ready for the help.

Request a call with your CTO

Every member starts with a call. No card.