Sub topic 4
Starting from a clean baseline on its own branch
This chapter assumes you know basic Git: branches, commits, staging, and reading git diff. It also assumes you can run your project's tests and linter from the command line. You will prepare the repository so that every change after the agent starts can be attributed to the agent.
Why the baseline matters
Reviewing an agent's multi-file change is a comparison between two states: the code before the run and the code after it. If the "before" state is vague, the comparison is vague too. Suppose you had half-finished edits in the working tree when you started the agent. Then git diff mixes your edits with the agent's, and you cannot tell which lines to scrutinize. You may also approve your own unfinished work by accident, or revert it while discarding the agent's mistakes.
A clean baseline gives you three properties:
- Attribution. Every difference from the starting commit was produced by the agent.
- Reversibility. You can throw the whole run away by switching branches, without touching your other work.
- A known-good reference. If the tests passed before the run and fail after, the agent's change is the cause.
Create a dedicated branch and check the tree
Make the branch before the agent runs, not after. Name it so you can recognize it later. The agent/ prefix is a convention that lets you list and delete these branches in bulk.
git switch -c agent/rename-config-loader
git status --short # no output means the working tree is clean
git log --oneline -1 # note the commit the agent starts fromgit status --short prints one line per changed or untracked file. Empty output means there are no staged changes, no unstaged changes, and no untracked files that are not ignored. The last command shows the commit the agent starts from. Write down that hash, or tag it as shown below.
The git switch -c command creates the branch at your current commit. If you are on an outdated main, the agent will build on stale code. Update your base branch first with git switch main and git pull --ff-only, then create the agent branch.
Handling a dirty working tree
If git status --short prints anything, decide what to do with each line before you continue. You have two reasonable options.
Commit your work on its own branch if it is meaningful:
git switch -c wip/my-feature
git add -A
git commit -m "WIP: my feature"
git switch main
git switch -c agent/rename-config-loaderOr stash it if it is temporary:
git stash push --include-untracked -m "before agent run: rename-config-loader"
git stash listThe --include-untracked flag matters. A plain git stash leaves new, untracked files in place, and those files then look like agent output. Pop the stash only after you finish reviewing and merging, so your edits do not mix into the diff.
Avoid git stash when the work is valuable and long-lived. Stashes are easy to forget and are not attached to any branch. A WIP commit is safer.
Untracked and ignored files
git status --short hides files that .gitignore excludes. Those files are not part of the diff, so the agent can change them without you noticing. Examples are .env files, local config, build output, and virtual environments.
List what is ignored so you know what exists outside Git's view:
git status --short --ignoredTwo practical consequences follow:
- A passing test run may depend on an ignored file, such as a local
.env. The agent might edit it, and you will not see that ingit diff. Keep copies of important ignored files, or compare timestamps withls -l. - Generated artifacts, such as
__pycache__ordist/, can hide behind.gitignore. That is normal, but you should know which directories are ignored before you trust a cleangit statusafter the run.
To preview what git clean would delete, without deleting anything, use git clean -nd. Use this later if the agent leaves stray files.
Run the checks on the baseline first
Before the agent starts, run the same checks you will use to verify its work. The result tells you what "green" means for this repository today.
python -m pytest -q
ruff check .
mypy srcUse your project's own commands here. Record the outcome in one of three ways.
| Baseline result | What it means | What to do |
|---|---|---|
| All checks pass | Any later failure is the agent's | Start the agent |
| Some tests already fail | Failures after the run may not be the agent's | Note the failing test names, or fix them first |
| Checks cannot run at all | You cannot verify the agent's work | Fix the environment before the run |
The middle row is the common trap. If three tests failed before the run, "three tests fail" afterwards is not evidence of a regression, and "zero fail" may mean the agent edited the tests. Write the pre-existing failures in a scratch file, outside the repository, so you can compare later.
Mark the starting point
Even on a branch, you want a fixed name for the commit the agent started from. Agents may create commits of their own. If they do, HEAD moves, and "what changed" becomes "everything since the starting commit". Tag the commit locally:
git tag agent-base/rename-config-loader
git diff agent-base/rename-config-loader --statThe second command compares the working tree to the tagged commit. It lists committed and uncommitted tracked changes together, which is the whole run, however the agent chose to save its work. If you prefer not to create tags, store the hash instead:
BASE=$(git rev-parse HEAD)
echo "$BASE"
git diff "$BASE" --statA shell variable disappears when you close the terminal, so also write the hash down. A tag survives, and you can delete it after the merge with git tag -d agent-base/rename-config-loader.
Remember that git diff against a commit ignores untracked files. To see new files the agent created, pair it with git status --short. The next chapter covers that triage step in detail.
Optional: use a separate worktree
A branch isolates the agent's commits, but the working directory is still shared. If you want to keep editing or running other work while the agent runs, create a second working directory attached to the same repository:
git worktree add ../myproject-agent -b agent/rename-config-loader
cd ../myproject-agent
git status --shortThis creates a new folder with the new branch checked out. Run the agent there. Your original folder stays untouched, so your uncommitted work cannot mix with the agent's. When you finish, remove the worktree:
git worktree remove ../myproject-agentWorktrees have costs. A new directory has no installed dependencies, no virtual environment, and no ignored config files, so you may need to set those up again. Two worktrees cannot have the same branch checked out at once.
Edge cases and failure modes
- The agent switches branches. Some agents run
git checkoutas part of a task. After the run, confirm you are still on the branch you created withgit branch --show-current. - The agent commits for you. Use
git log --oneline agent-base/rename-config-loader..HEADto list its commits. Many small commits are fine. A single large commit is harder to review and revert in parts. - The agent runs
git addorgit stash. Staged state changes howgit diffbehaves: plaingit diffshows only unstaged changes, andgit diff --stagedshows the rest. Diffing against the base commit avoids this confusion. - Submodules. Changes inside a submodule appear in the parent repository only as a changed commit pointer. Check each submodule's own status.
- A lock or hook interferes. Pre-commit hooks may reformat files when the agent commits. Those reformatting changes are part of the run, so expect them in the diff.
A short pre-run checklist
- Your base branch is up to date.
git status --short --ignoredshows nothing you do not recognize.- You created and switched to the agent branch.
- Tests, linter, and type checker ran on the baseline, and you wrote down any existing failures.
- You recorded the starting commit, by tag or by hash.
Exercise
You start an agent without making a branch. The working tree already contains two uncommitted edits of your own. After the run, git diff shows changes in six files. What two problems does this cause for your review, and what should you have done before the run?
Show answer
First, you cannot tell which of the six files' changes came from the agent and which came from you, so you may approve or revert the wrong lines. Second, there is no named starting commit or separate branch, so discarding the agent's run would also discard your own edits. Before the run you should have committed or stashed your edits (with --include-untracked for new files), created a branch such as agent/<task-name> with git switch -c, confirmed git status --short was empty, run the baseline tests, and recorded the starting commit.
Exercise
The baseline test run shows that testcacheexpiry already fails. After the agent's run, the suite reports all tests passing. Why should this make you look closer rather than relax?
Show answer
A test that failed before the run and passes now may have been fixed by the agent legitimately, or the agent may have weakened, skipped, or deleted it. Because you recorded the pre-existing failure, you know to check git diff for that test and for the code it covers instead of trusting the green result.