Sub topic 14
Using git as a safety net
This chapter assumes you can commit, branch, and read a diff, and that you have started an agent session in a repository. Git is the tool that lets you undo an agent's work. Agents can edit many files in one run, so the safety net has to be in place before you start.
Start from a clean state
Commit or stash your own changes before you run an agent. If the working tree is clean, git diff afterward shows only what the agent changed. If your edits are mixed in, you cannot tell whose change is whose, and reverting the agent's work may also revert yours.
git status --shortAn empty result means the tree is clean.
Give each task its own branch
Create a branch for each task. If the result is poor, you delete the branch and your main line is untouched.
git switch -c agent/fix-date-parsing
# ...run the agent on a narrowly scoped task...
git diff
npm test
git add -p
git commit -m "Fix date parsing for ISO week dates"The sequence has four parts. The agent works, you read the full diff, you run the tests yourself, and you stage only the hunks you accept with git add -p. Running the tests yourself matters because an agent's report that tests passed is a claim, not a result.
Commit in small steps
Commit after each accepted step, not once at the end. Each commit is a restore point. If the agent breaks something on the next step, git restore . returns the tree to the last commit.
| Situation | Command | Effect |
|---|---|---|
| Discard all uncommitted edits | `git restore .` | Reverts tracked files to the last commit |
| Discard one file | `git restore path/to/file` | Reverts only that file |
| Remove new files the agent created | `git clean -n`, then `git clean -f` | Lists, then deletes, untracked files |
| Abandon the whole task | `git switch main` then `git branch -D agent/fix-date-parsing` | Deletes the branch and its commits |
Common errors
- Scope too wide. A prompt such as "refactor the project" produces a diff too large to review. Keep each task small enough that you can read every changed line.
- Ignored files. Git does not track files in
.gitignore, so it cannot restore them. Agent edits to.envor build output are not protected. - Side effects outside the repository. Git covers file contents only. It does not undo database migrations, package installs in global directories, or network calls the agent made.
- Agent-made commits. Some agents can run
git commitorgit push. Denygit pushin your permission settings so that every change stays local until you review it. - Unreviewed staging. Running
git add -Aaccepts everything, including stray files. Prefergit add -p.
Exercise
You asked an agent to fix one bug. git diff shows changes in 14 files, and you only want the changes in src/date.ts. Which git commands keep that file's changes and discard the rest, assuming no new files were created?
Show answer
Stage and commit the wanted file with git add src/date.ts and git commit -m "Fix date parsing". Then run git restore . to discard the remaining uncommitted edits. You can also use git add -p to pick hunks inside the file before committing.