Stop the avalanche first

After an unsuccessful AI edit, do not immediately ask Claude to “fix everything.” First establish the actual scope of the problem through Git: the active branch, changed files, diff, and latest commits.

A minimal rollback is safer than editing again on top of a chaotic diff. Small milestone commits make recovery cheaper.

git status
git diff -- path/to/file
git diff
git log --oneline -3

Recovery depends on where the changes are

StateApproach
Working filesReview the diff and, if needed, cautiously use git restore
Staging areaFirst run git restore --staged, then assess the file
Bad commit in shared historyCreate a new commit with git revert
Bad local commitConsider reset only if the team has not seen the history yet
Confused branchCreate a new branch from a clean base

Minimal Git-first recovery

  1. Stop new AI edits.
  2. Check git status and review suspicious diff fragments.
  3. Separate the needed changes from the unnecessary ones.
  4. Choose the smallest safe rollback method.
  5. Run the relevant tests.
  6. Check the diff again.
  7. Give Claude a new narrow task with explicit boundaries.
Cautious recovery of a working file
git restore path/to/file
git status
git diff

A clean base for another attempt

The old branch can remain a source of individual useful ideas, but it is better to start a new implementation from a clean base when the previous diff is already difficult to explain.

git switch -c refund-refactor-retry main

External effects do not roll back with Git

  • Git restores files and history, but does not undo a database migration.
  • Rolling back code does not reverse an external call or a deployment that has already run.
  • For shared history, git revert is usually safer than rewriting it.
  • Before git restore, make sure the unsaved changes are truly unnecessary.

Canonical lesson source · JavaRush

Open JavaRush material