Sub topic 9
Reviewing, accepting, and undoing changes
This chapter assumes you have used agent mode to edit files and run commands. It also assumes you know basic Git: git diff, git add, git commit, git restore, and branches. The examples use VS Code with GitHub Copilot. Other editors use different button names, but the working model is the same.
Why review is a separate step
The agent loop ends when the agent decides it is done. It does not end when the work is correct. The agent can pass its own tests and still change a file you did not want touched, loosen an assertion to make a test pass, or add a dependency you did not ask for. Review is the step where you decide whether the result is acceptable.
Agent edits are also usually applied to your files right away, as pending changes. Keeping or undoing them is a decision you make afterward. This is different from ask mode, where you copy code out of the chat yourself.
The working model: pending, kept, or undone
Each file the agent edits is in one of three states.
| State | What it means | What you can do |
|---|---|---|
| Pending | The edit is on disk and shown as a diff in the editor. You have not decided yet. | Keep or undo it, per hunk, per file, or all at once. |
| Kept | You accepted the edit. It is now an ordinary unsaved or saved change in your working tree. | Commit it, or revert it with Git. |
| Undone | You rejected the edit. The file returns to its content before the agent touched it. | Ask the agent to try again. |
A hunk is one contiguous block of changed lines in a diff. You can accept some hunks in a file and reject others.
Reviewing the diff
VS Code shows pending edits in two places. The chat panel lists the changed files, and each file opens as an inline diff in the editor. Removed lines are marked in red and added lines in green. Controls appear for each hunk and for the whole file. The exact labels differ between versions, but the options are "Keep" and "Undo", with navigation arrows to move between hunks.
Review in this order:
- Read the list of changed files first. A file you did not expect is the quickest warning sign.
- Open each diff and read every hunk. Do not rely on the agent's summary, because it describes intent and the diff shows what actually changed.
- Check the tests. Look for deleted tests, weakened assertions, skipped tests, and changed expected values.
- Check configuration and dependency files, such as
package.jsonand CI settings. These changes are easy to overlook and have wide effects. - Run the checks yourself. The agent may have run tests, but your own run confirms the result in your environment.
What to look for
- Scope creep: renamed variables, reformatted files, or changed imports in code unrelated to the task.
- Fake fixes: a
try/catchthat swallows an error, a hard-coded value that makes a test pass, or a test markedskip. - Invented APIs: calls to functions or options that do not exist in your library version. These show up as type errors or runtime failures, so run the type checker and the build.
- Leftover debugging:
console.logcalls, temporary files, or commented-out blocks. - Secrets: any token, key, or password written into a file, including example configs.
If a diff is too large to review, that is a signal about the prompt. Undo it and ask for a smaller step.
Keeping and undoing at different granularities
You can act at four levels.
- Hunk: keep or undo one block of changes inside a file. Use this when most of an edit is right but one part is wrong.
- File: keep or undo everything in one file.
- All pending changes: keep or undo every file in the current set. Use "Keep all" only after you have read every diff.
- Checkpoint: restore the workspace to its state at an earlier point in the conversation.
Hunk-level control matters because rejecting a whole file throws away good work. A better pattern is to undo the bad hunk, keep the rest, and tell the agent what was wrong so that it does not reintroduce the problem.
Checkpoints
VS Code records a checkpoint for each chat request in an agent session. If you hover over an earlier request in the chat, you can restore the files to the state before that request ran. The edits made by that request and by every later request are removed.
Checkpoints are convenient but limited:
- They cover file edits that the agent made through its editing tools.
- They do not reverse terminal side effects. A command such as
npm install, a database migration,git push, or a file deleted by a shell command is not undone. - They live with the chat session. If you clear the history or lose the session, you lose them.
- They do not replace version control. They have no names, no history you can share, and no merge support.
Using Git as the real safety net
Treat Git as the primary undo mechanism and the editor controls as a convenience. A reliable routine takes about a minute of setup.
# Before a risky task: start from a clean tree on a new branch
git status --short
git switch -c agent/add-user-validation
# If the tree has your own uncommitted work, save it first
git add -A
git commit -m "wip: before agent task"
# After the agent finishes and you have kept its edits:
git diff --stat # which files changed, and how much
git diff # the full change
npm test # your own verification
# Commit in a small, reviewable unit
git add -p # choose hunks interactively
git commit -m "Validate name and email in createUser"Committing your own work first matters. If your uncommitted edits are mixed with the agent's edits, you cannot tell them apart and cannot revert only the agent's work.
To reverse changes after you have kept them:
# Discard unstaged changes in one file
git restore src/users.ts
# Discard all unstaged changes in tracked files
git restore .
# Remove untracked files the agent created (preview first)
git clean -n
git clean -f
# Undo the last commit but keep the changes in your working tree
git reset --soft HEAD~1Check git clean -n before running git clean -f. The -n flag only lists what would be removed, and -f deletes the files permanently.
Common errors and how to avoid them
| Problem | Cause | Fix |
|---|---|---|
| You cannot tell the agent's edits from your own | You had uncommitted work when you started | Commit or stash before each agent task |
| "Keep all" accepted a bad edit | You accepted without reading each diff | Review per file, and use `git add -p` before committing |
| A restored checkpoint left the project broken | A terminal command changed state outside the files | Reverse the command, for example reinstall dependencies from the lockfile |
| The agent's edit disappeared | You edited the same file while the agent was working, or undid the wrong hunk | Avoid editing files the agent is changing, and check `git diff` |
| Tests pass but the behavior is wrong | The agent changed the tests | Compare the test diff separately from the source diff |
| New files remain after "undo" | They are untracked, and Git restore does not touch them | Use `git status` and `git clean -n` |
A review checklist for each task
- Confirm that the changed file list matches the scope you gave in the prompt.
- Read every hunk, with the test files first.
- Run the type checker, the linter, and the tests yourself.
- Undo any hunk that is out of scope, and keep the rest.
- Check
git statusfor untracked files and for changes outside the source folders. - Commit with a message that says what changed and why.
If the same problem appears twice, put the rule in your instructions file. For example: "Do not modify existing tests unless the task says so." The agent then sees that rule at the start of every session, so you do not have to repeat it in each prompt.
Exercise
The agent changed five files. In one file, 4 of 5 hunks are correct and the fifth removes an existing error check. You also see that it ran npm install some-package in the terminal. What do you do, and what does a checkpoint restore not cover here?
Show answer
Undo only the fifth hunk and keep the other four, then read the remaining diffs and run the tests yourself. Ask why some-package was needed, and remove it with npm uninstall some-package or restore package.json and the lockfile from Git if you do not want it. A checkpoint restore would revert the file edits but would not reverse the terminal command's effects, such as the installed package in node_modules.
Exercise
Why should you commit or stash your own uncommitted work before starting an agent task?
Show answer
Without a clean starting point, your edits and the agent's edits are mixed in the working tree. You then cannot use git diff or git restore to inspect or revert only the agent's work, and a hunk-level undo may discard changes you wanted to keep.