Sub topic 4
Starting from a clean baseline on its own branch
This chapter assumes you know basic Git (branches, commits, staging, diffs) and can run your project's tests from the command line. You should also have used a coding agent at least once.
An agent can change many files in one run. If your working tree already holds your own unfinished edits, you can't tell which changes are yours and which are the agent's. A clean baseline on its own branch fixes this. Everything that differs from the starting commit is the agent's work, and you can discard it with one command.
Why the baseline matters
Review depends on a trustworthy comparison. git diff shows the difference between two states. If one state is already messy, the diff mixes your edits with the agent's, and a mistake in either can slip through.
A dedicated branch gives you three things:
- Isolation. Your main branch stays untouched while the agent works.
- A fixed reference point. You know exactly which commit the agent started from.
- Cheap rollback. You can delete the branch and lose nothing you wanted.
Set up the baseline
First, deal with your own work. Commit it, or stash it with git stash. Then create the branch and confirm it is clean.
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 fromWrite down the commit hash from the last line. It is the anchor for every later comparison, such as git diff <hash>.
Name the branch after the task. A prefix such as agent/ makes agent branches easy to list and clean up later.
What "clean" means
git status --short printing nothing means there are no modified, staged, or untracked files. Check each case, because each one can hide trouble:
- Modified files. Your edits would be mixed into the agent's diff.
- Untracked files. These do not appear in
git diff, so you can't separate them from new files the agent creates. - Ignored files. Build output and local config are hidden by
.gitignore. An agent that edits an ignored file leaves no trace in the diff.
Run the baseline checks first
Before the agent starts, run your tests and linter on the clean branch. If a test already fails, you need to know that now. Otherwise you may blame the agent for a failure that was there before it touched anything.
Record the result in a line or two, for example "42 passed, 0 failed, lint clean". After the run you can compare against it. This also gives the agent a known-good state to work toward.
Common mistakes
| Mistake | Consequence | Fix |
|---|---|---|
| Starting with uncommitted edits | The diff mixes your work with the agent's | Commit or stash first |
| Running on `main` directly | Rollback needs manual cleanup | Always create a branch |
| Skipping baseline tests | Existing failures look like agent bugs | Run tests before the run |
| Forgetting the start commit | You can't compare against a fixed point | Record `git log --oneline -1` |
Discarding a failed run
If the run goes badly, you can abandon it without risk to your own work. Switch back and delete the branch:
git switch main
git branch -D agent/rename-config-loaderUse -D only when you are sure you don't want any of the agent's work. Chapter content later in this tutorial covers keeping part of a change instead.
Exercise
You start an agent run with two of your own uncommitted edits in the working tree. After the run, git diff shows five changed files. Why is this a problem, and what should you have done first?
Show answer
Two of the five files may be your own edits, so you can't tell which changes the agent made. You should have committed or stashed your work, created a dedicated branch such as agent/<task>, and confirmed with git status --short that the tree was clean before starting the agent.