Sub topic 5
Using branches and commits as checkpoints
This chapter assumes you can read and write code, use basic Git (branches, commits, diffs), and run a test suite from the command line. It covers how to use Git so that any step an agent takes can be kept or thrown away without losing earlier work.
Why checkpoints matter
A coding agent edits many files quickly. If its change goes wrong and your working tree also holds earlier good changes, you may not be able to separate the two. A commit is a saved state you can return to. A branch keeps the whole experiment away from your main line of work.
The rule is simple: never let the agent work on top of uncommitted changes you care about, and commit after each step you have verified.
Start from a clean state
Before you start a session, check that the working tree is clean.
git status --short
git switch -c refactor/extract-calculate-taxAn empty git status --short means there is nothing uncommitted. If there is output, commit or stash it first. Otherwise the agent's edits mix with yours, and the diff you review later will be hard to read.
Create a new branch for each task. If the whole attempt fails, you delete the branch and the main branch is untouched.
The checkpoint cycle
Each small step follows the same cycle: let the agent make one change, verify it, then commit.
In commands, the cycle looks like this:
pytest
# let the agent make one small step, then check it
pytest && git add -A && git commit -m "Extract calculate_tax from format_invoice"
# if a later step goes wrong, discard it and keep the checkpoint
git restore .The && means the commit runs only if pytest succeeds. git restore . resets tracked files to the last commit, so it removes the failed step and keeps everything you committed before.
Commit granularity
A good checkpoint does one thing and leaves the tests passing. Compare these two approaches.
| Approach | Result when step 4 of 6 breaks |
|---|---|
| One commit at the end | You must undo everything or untangle the good parts by hand |
| One commit per verified step | You discard step 4 only and keep steps 1 to 3 |
Small commits also make the history readable. A reviewer can follow "Extract calculate_tax", then "Update callers", then "Remove unused import" as separate decisions.
Choosing how to undo
Which command to use depends on whether the bad change is committed.
- Uncommitted edits to tracked files:
git restore .orgit restore path/to/file. - A commit you want to remove from your own unpushed branch:
git reset --hard HEAD~1. This discards that commit and its changes. - A commit already pushed or shared:
git revert <commit>. It adds a new commit that undoes the old one, so shared history is not rewritten.
Avoid giving the agent permission to run history-rewriting commands such as git reset --hard, git push --force, or git clean -fd without asking you first. These are the commands that destroy checkpoints.
Common mistakes
- Committing with
git add -Awithout looking. The agent may have created scratch files or edited a file outside the agreed scope. Rungit diff --statbefore staging, or stage only the files you expect. - Committing when tests fail "to save progress". A broken checkpoint is not a safe place to return to. Use a separate work-in-progress branch if you must, and mark it clearly.
- Working on the main branch. There is then no clean way to abandon the attempt.
Exercise
The agent has finished step 3 of a refactoring. You committed steps 1 and 2 after verifying them. The tests now fail and the agent's fix attempt has made things worse. Which command returns the tracked files to the state after step 2, and why is this safe for steps 1 and 2?
Show answer
git restore . resets tracked files to the last commit, which is the step 2 checkpoint. Steps 1 and 2 are stored in commits, not in the working tree, so the command does not touch them. Check git status afterwards for new untracked files the agent created, because git restore does not remove those.