---
title: "Git Rollback After AI Edits: 5 Commands to Recover Your Code"
description: "AI broke your code? Commit before it starts, then use diff, restore and stash to roll back from lightest to heaviest, with a full recovery example."
url: https://cdnping.org/en/blog/git-basics-for-ai-coding
language: en
published: 2026-11-30
category: "Developer tools"
tags: ["Git","AI coding","Beginners","Rollback","Claude Code"]
source: cdnping.org
license: Free to quote and summarize with a link to the url above.
---
# Git Rollback After AI Edits: 5 Commands to Recover Your Code

Before you let an AI tool edit your code, save the current state as a Git commit. After that, no matter what the AI does, you can get back with `git restore`. Beginners only need five commands: init, commit, diff, restore and stash. This article covers when to use each one and how to recover when an AI edit goes wrong.

An AI tool may change a dozen files at once, and it does so fast. Without version history, it is hard to say exactly what it touched, and impossible to go back to the way things were with one command. Git solves that.

## Why do you need Git before letting AI edit code?

AI coding tools write directly to the files on your disk. There is no "undo" button that covers the whole project. Common situations:

- You ask it to fix one bug, and it refactors three unrelated files along the way.
- It stops halfway, leaving the project in a broken, half-edited state.
- You want to compare "before" and "after," but there is no "before."

Git records "commits," which are save points. As long as you committed before the AI started, you always have a clean state to return to.

## What does each of the five commands do?

| Command | What it does | When to use it |
| --- | --- | --- |
| `git init` | Turns the current directory into a Git repository | The first time a project uses Git |
| `git add -A` + `git commit -m "message"` | Saves the current state as a save point | Before every AI task |
| `git diff` | Shows changes that are not yet committed | After the AI finishes, to decide whether to keep them |
| `git restore` | Discards changes and returns to the last commit | The AI broke something |
| `git stash` | Sets changes aside temporarily | You want to see the clean version first, or you are not sure yet |

Strictly speaking, you must `add` before you commit; here the two steps are counted as one action.

## How do you set it up the first time?

Run this in your project directory:

```bash
git init
git add -A
git commit -m "before ai: initial state"
```

If Git asks you to set a name and email, follow the prompt and use `git config --global user.name` and `git config --global user.email`.

It is also a good idea to create a `.gitignore` first and exclude `node_modules/`, `.env`, virtual environment directories and the like. Files such as `.env` that hold API keys should never be committed, and certainly never pushed to a public repository.

## What is the workflow each time AI edits your code?

1. Run `git status` to confirm the working tree is clean. If there is uncommitted work, commit it first.
2. Give the AI a small, clearly scoped task. Don't say "optimize the whole project" in one go.
3. When the AI finishes, run `git diff` and read through it. If many files changed, start with `git diff --stat` to see which ones.
4. Run your tests, or start the program.
5. If you are happy with it, `git commit`. If not, roll back (see the next section).

The smaller the task, the smaller the difference between save points, and the cheaper a rollback is.

## How do you recover when AI breaks something?

There are a few cases. Work from lightest to heaviest.

### Undo changes to just one file

```bash
git restore path/to/file.py
```

The file returns to its content at the last commit.

### Undo all changes

```bash
git restore .
```

Note that `git restore` discards modifications that have not been committed, and once they are gone you cannot get them back from Git. Run `git diff` to check before you do it, or set the changes aside with stash (below) first.

### What if the AI created a pile of new files?

`git restore` does not handle newly created, untracked files. Preview first:

```bash
git clean -n
```

Once you have confirmed the list is correct, use `git clean -f` to delete them. This deletion also cannot be recovered from Git, so always look at the preview first.

### You already committed, and only then noticed a problem

Use `git log --oneline` to find the commit you want, then use `git revert <commit-hash>` to create a reverse commit, which keeps the history most complete. `git reset --hard`, for a full rollback, discards commits and changes, so beginners should not reach for it first; if you need it, check the official documentation at git-scm.com first.

## How does git stash help here?

Stash means "put it aside, don't delete it." A typical case: the AI changed a lot, you can't yet tell whether it's good or bad, and you want to see what the code looked like before.

```bash
git stash push -u -m "ai attempt 1"   # set aside all changes, including new files
# the working tree is now clean, so you can run and compare
git stash list                         # list what has been set aside
git stash pop                          # bring back the most recent stash
```

`-u` means untracked new files are stashed too. If you want to keep the changes but don't want them occupying your working tree, `git stash` is much safer than a straight restore.

A more careful approach is to give the AI its own branch: `git switch -c ai-try`. If the result is bad, switch back to the main branch and throw that branch away entirely. The main branch is unaffected.

## A worked example of recovering from a bad edit

Suppose you ask the AI to turn a function into an async function. It changes 5 files at the same time, and running the code throws an error:

1. `git diff --stat` shows which 5 files changed.
2. You find that 4 of them look right and 1 should not have been touched. Use `git restore` on that one file.
3. If it still errors, `git restore .` takes everything back to the starting point. Split the task into smaller pieces and give it to the AI again.

The whole process takes under a minute, provided you committed once before starting.

## What should you watch for with different AI tools?

Whether it is a command-line tool or an editor extension, the principle is the same: they all change your local files. If you haven't picked a tool yet, start with [how to choose an AI coding tool](/en/blog/ai-coding-tools-for-beginners). If you plan to use a command-line tool, see [how to use Claude Code](/en/blog/claude-code-beginner-guide) or [how to use Codex CLI](/en/blog/codex-cli-beginner-guide). If you work in VS Code, read [using AI to write code in VS Code](/en/blog/vscode-ai-extension-beginner). For the overall checklist, see [the 6 things to prepare before you first code with AI](/en/blog/first-ai-coding-project-checklist).

Some tools have their own checkpoints or undo features. Their exact behavior is described in each tool's official documentation, but don't rely on them alone. Git is the universal layer, and you control it yourself.

If an AI tool suddenly stops responding, you can first check cdnping's [/en/down/claude-code](/en/down/claude-code) or [/en/status](/en/status) to see whether the service itself has a problem, and then decide whether it is your own environment.

## No overseas credit card? How do you access these tools?

Many beginners get stuck at "the tool is installed, but the account and payment are hard to sort out." AGI Plan is a monthly-subscription, multi-provider LLM gateway that you pay for in RMB, with no overseas credit card required. For tools such as Claude Code and Codex, you change one endpoint address and use the same quota. Some models are marked as available only outside China; see [agiplan.dev/models](https://agiplan.dev/models) for specifics. The service also states plainly that it is not an officially authorized channel. AGI Plan is a commercial partner of cdnping. To learn the setup steps, see [agiplan.dev/startup](https://agiplan.dev/startup).

## What should you do next?

1. Run `git init` and make your first commit in your project now.
2. Add a `.gitignore` and confirm `.env` is not in the commits.
3. Before the next time you ask AI to edit code, run `git status`, and after it finishes, run `git diff` first.
4. Deliberately have the AI change one small thing, then practice `git restore` and `git stash` once more. After that, a real problem won't throw you.
