Sub topic 14
Using git as a safety net
This chapter assumes you can commit, branch, and read a diff, and that you have already used one of the agents in an interactive session. It builds on the plan, implement, verify workflow. Git is what makes that workflow reversible. An agent can edit many files and run shell commands in a few seconds. Git gives you a record of what existed before it did, and a way to go back.
Why git matters more with an agent
A person who edits code by hand usually changes a few lines at a time. An agent can change dozens of files in one turn. It may also rename files, regenerate output, or run formatters across the tree. Reviewing that by memory is not realistic. You need a precise comparison against a known good state.
Git provides three things the agent does not:
- A baseline. A commit is a snapshot you trust.
- A diff. It shows exactly what changed, including edits you did not ask for.
- An undo. You can discard or revert changes without asking the agent to "put it back".
Do not rely on the agent's own summary of what it did. Summaries can omit a change or describe intent instead of result. The diff is the source of truth.
Start from a clean working tree
Before you begin a task, run git status. If you have uncommitted work, commit it or stash it first. Otherwise your changes and the agent's changes mix in the same diff, and you cannot tell them apart.
git status --short
git stash push -u -m "wip before agent task"The -u flag also stashes untracked files. Restore your work later with git stash pop.
Work on a branch
Give each agent task its own branch. This keeps your main branch untouched and makes the whole task easy to discard.
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 a clear purpose at each step:
git switch -ccreates the branch, so the experiment is isolated.- The agent runs on a task small enough to review.
git diffshows every change in the working tree.npm testis your own check, run by you and not reported by the agent.git add -pstages changes hunk by hunk, so you accept only what you understand.git commitrecords the accepted result.
The review loop
The loop below shows how work moves between you, the agent, and git.
Review the diff, not just the result
Passing tests do not prove the change is right. Read the diff for these problems:
- Files you did not expect, such as lock files, generated output, or config.
- Edits to tests that make them pass by weakening an assertion.
- Deleted code that looked unused but is called dynamically.
- Hard-coded values, debug output, or commented-out blocks.
- Changes under paths your instruction file says not to touch, such as
dist/orgenerated/.
Useful commands for a closer look:
git diff --stat # which files changed and how much
git diff --name-only # file names only
git diff -- src/ # limit the diff to one directory
git diff --check # whitespace errors and leftover conflict markers
git status --short # includes untracked files, which git diff does not showNote that git diff does not show new untracked files. Always run git status as well, or the agent can add a file you never see.
Commit in small steps
A good habit is to commit after each accepted step instead of at the end of a long session. Then each commit is a restore point. If the third step goes wrong, you return to the second, not to the start.
Some agents can commit for you if you ask. This is convenient, but decide how much to allow:
- If you let the agent commit, review each commit afterward with
git showorgit log -p. - If you prefer to commit yourself, say so in your instruction file, for example "Do not run
git commitorgit push." - Treat
git pushas a separate decision. Pushing publishes work, and it is hard to undo once others have pulled it.
You can also enforce this with permissions. In Claude Code, a deny rule can block a command pattern:
{
"permissions": {
"deny": [
"Bash(git push:*)"
]
}
}This goes in .claude/settings.json. The other tools have their own approval settings, so check how each one treats git commands before you rely on them.
Undoing agent changes
Choose the undo that matches how far the change went.
| Situation | Command | Effect |
|---|---|---|
| One file edited badly, not committed | `git restore path/to/file` | Resets that file to the last commit |
| Several files edited, not committed | `git restore .` | Discards all tracked changes in the working tree |
| New untracked files created | `git clean -nd`, then `git clean -fd` | Previews, then deletes untracked files and directories |
| A commit you made is wrong, not pushed | `git reset --soft HEAD~1` | Removes the commit, keeps changes staged |
| A commit is already pushed or shared | `git revert <commit>` | Adds a new commit that reverses it |
| The whole branch is a dead end | `git switch main`, then `git branch -D agent/name` | Abandons the branch |
Edge cases and limits
Git protects tracked files. It does not protect everything an agent can touch.
- Ignored files. Files in
.gitignore, such as.env, build output, or local databases, are not tracked. An agent can change or delete them and git will not show it. Back up anything important that is ignored. - Outside the repository. A shell command can write to your home directory or to system paths. Git cannot undo that. Sandboxing and approval settings are the control here.
- Side effects. Database migrations, installed packages, API calls, and deployed resources are not undone by
git restore. - Hooks. Repository git hooks run when the agent commits. A failing pre-commit hook can block a commit, and the agent may try to bypass it. Check that it did not use
--no-verify. - Dirty trees and merge conflicts. If you switch branches mid-task with uncommitted changes, git may refuse or carry changes across. Commit or stash first.
Parallel work with worktrees
If you want two agents, or an agent and yourself, to work at the same time, a single working directory is a problem. Edits collide. A git worktree gives each task its own directory and branch while sharing one repository.
git worktree add ../my-project-agent -b agent/add-pagination
cd ../my-project-agent
claudeWhen the task is done, merge or discard the branch and remove the worktree:
cd ../my-project
git worktree remove ../my-project-agent
git branch -D agent/add-paginationInstall dependencies in each worktree. node_modules is not shared between directories.
A practical checklist
- Run
git status. Commit or stash your own work. - Create a branch with a clear name.
- Give the agent one narrowly scoped task.
- Read
git diffandgit status. - Run the tests and linter yourself.
- Stage only what you accept with
git add -p. - Commit, then repeat for the next small step.
- Merge when the branch is reviewed, or delete it.
Exercise
An agent finished a task on your branch. git diff shows only edits to src/date.ts, and npm test passes. A later run of git status --short shows ?? scripts/migrate.sh. What happened, why did git diff not show it, and what should you do before committing?
Show answer
The agent created a new file, scripts/migrate.sh. git diff compares tracked files only, so untracked files do not appear in it. Open the file and read it to decide whether the task needed it. If it did not, delete it, using git clean -nd to preview first. If you keep it, stage it deliberately with git add scripts/migrate.sh and mention it in the commit message. Also check that the agent did not run the script.
Exercise
You committed an agent's change and pushed the branch. A teammate has pulled it. You now find the change is wrong. Should you use git reset --hard HEAD~1, or git revert? Explain.
Show answer
Use git revert <commit>. It adds a new commit that reverses the change, so history stays consistent for your teammate. git reset --hard rewrites history, which would force you to push with --force and would leave your teammate's copy out of sync with the shared branch.