How to Undo AI-Generated Changes Safely With Git

An AI coding assistant can change more files than expected, mix useful edits with mistakes, or leave behind files you did not ask for. Git gives you a way to inspect those changes and undo only what you intend. This guide shows how to review AI-generated diffs, preserve work you want to keep, and safely restore files or commits.

Practice Git workflows →

Start by inspecting the AI's changes

Before asking an assistant to make a broad change, check git status so you know which edits already belong to you. Afterward, review the changed files and diff rather than assuming every edit came from the assistant or should be discarded:

git status --short
git diff --stat
git diff
git diff --check

git diff shows unstaged tracked changes. If you already staged files, inspect those separately with git diff --cached. Untracked files are not included in either diff, so also check git status for files Git has not started tracking.

Keep the useful changes and undo only selected edits

If a file contains a mix of good and unwanted edits, do not restore the whole file. Interactively choose which unstaged hunks to discard:

git restore -p -- path/to/file

Review each hunk Git presents and discard only the changes you recognize as unwanted. For files you want to keep, stage selected hunks with git add -p, or stage an entire reviewed file with git add path/to/file. Then inspect the staged result before committing:

git diff --cached
git status

Restore a whole file only when you mean to discard all its edits

To discard every unstaged change in one tracked file and restore it to the version currently in the index, use:

git diff -- path/to/file
git restore -- path/to/file

This is destructive for that file's unstaged edits, including your own changes mixed with AI edits. Review the diff first and preserve anything valuable before restoring. If the file has no staged changes, the index normally matches the latest commit. If it does have staged changes, the restore keeps that staged version; inspect it with git diff --cached. To unstage a file while keeping its working-tree edits, use git restore --staged -- path/to/file.

Handle AI-created untracked files carefully

New files do not appear in git diff until they are staged. List untracked files with git status --short, open each one, and decide whether to keep it. If you want to keep a file, add it explicitly after reviewing its contents:

git add -- path/to/new-file

If you decide an untracked file is unwanted, remove only that specific file after verifying its path and contents. Avoid broad git clean commands: they can delete unrelated untracked work, including files that are not recoverable from Git.

Undo an AI change that has already been committed

If an unwanted change is in a commit that has not been shared, you can review the history with git log --oneline and choose a suitable local-history workflow. If the commit has been pushed or shared, prefer git revert, which creates a new commit that reverses the selected commit without rewriting shared history:

git log --oneline
git revert <commit-id>

Replace <commit-id> with the specific commit you reviewed. Reverting can produce conflicts if later changes depend on that commit; resolve them, test the result, and inspect the new commit. Avoid resetting or force-pushing shared history unless your team explicitly agrees on that recovery plan.

A safer Git workflow for AI coding assistants

  1. Check your branch and working tree before prompting the assistant.
  2. Ask for a focused change and review the changed-file list.
  3. Inspect the diff, including staged changes and untracked files.
  4. Keep or discard changes selectively; avoid restoring mixed files blindly.
  5. Run the relevant tests and review the final staged diff.
  6. Commit a coherent, verified change so later rollback is easier.

Undo AI-generated changes with Git FAQ

Can Git recover a change after git restore?

Git may not be able to recover uncommitted changes discarded by git restore. Treat it as destructive. Before using it, review the diff and save any work you may need.

Should I undo AI changes by resetting the whole repository?

Usually not. A broad reset can discard your own unrelated work. Identify the specific files or commits involved and undo only those changes you have reviewed.

Learn Git Faster by Practicing

Reading is just the first step. Real learning happens when you practice.

Start Learning Git →