Using Git Safely with an AI Coding Agent: Branches and Small Commits
Git is the best safety net you have when an agent is editing your code. A clean branch per task, frequent small commits and a protected main branch mean that any mistake, whether yours or the agent's, can be undone in seconds. The agent does not need to be perfect if every step is recoverable. This post sets up a simple workflow that makes that true.
Start every task from a clean state
Before you start a session, make sure there is nothing uncommitted. Otherwise your own half-finished work mixes with the agent's edits and you cannot tell them apart.
git status # expect: nothing to commit, working tree clean
git switch main
git pull
git switch -c feature/discount-validation
Use one branch per task. The branch name is a label that keeps the history understandable and gives you an obvious thing to delete if the experiment fails.
Commit after each working step
Ask the agent to work in small steps, or commit yourself when a step is good. A commit is a checkpoint. If the next step goes wrong, you return to the checkpoint rather than to the start.
# after step 1 (tests written and failing, as intended)
git add -A && git commit -m "Add failing tests for discount validation"
# after step 2 (implementation, tests pass)
git add -A && git commit -m "Validate discount codes before applying"
Many tools can write commits for you if you ask. If you allow that, review the message and the contents. Some teams prefer that the agent never commits and only edits files, leaving commits to the human. Either is fine as long as the rule is explicit. You can state it in your instruction file: "Do not commit or push; leave changes uncommitted."
Never let the agent push to protected branches
Enable branch protection on main in your hosting service so that changes need a pull request and passing checks. Then even an over-eager agent, or a mistaken command, cannot alter main directly. If your tool has permission settings, deny commands such as git push --force outright, and require confirmation for any push.
See what changed since the last checkpoint
git diff # unstaged changes since last commit
git diff --staged # what is about to be committed
git log --oneline -5 # recent checkpoints
Roll back precisely
Match the undo to the problem:
- Discard edits to one file:
git restore path/to/file - Discard all uncommitted edits:
git restore . - Undo the last commit but keep its changes:
git reset --soft HEAD~1 - Return to an earlier commit and drop everything after it:
git reset --hard <commit>(this deletes uncommitted work, so use with care) - Undo a commit that was already shared, safely:
git revert <commit>
A separate post covers undoing agent changes in more detail.
Use a separate worktree for risky experiments
A git worktree is a second checkout of the same repository in another folder. You can let an agent try a risky approach there while your main folder stays untouched.
git worktree add ../shop-experiment -b experiment/new-cache
cd ../shop-experiment
# start the agent here; delete the folder when done
git worktree remove ../shop-experiment
Keep the history readable
- One logical change per commit.
- Messages that say why, not only what.
- Squash noisy "fix typo" commits before opening a pull request if your team prefers a tidy history.
- Do not commit generated files, logs or local settings the agent created. Check with
git status.
A good rhythm is to commit before you hand a task to the agent, so that your own work is safe, and again when the agent finishes and you are happy with the result. Between those two commits, the diff of the working tree is exactly the agent's contribution. That clarity is worth the few seconds a commit takes. If your team squashes commits when merging, the extra checkpoints cost nothing in the final history, so there is little reason to avoid them.
Things to watch for
Agents sometimes create helper files such as scratch scripts, notes or temporary test data. Look for them in git status before git add -A, or prefer adding specific paths. Check also that nothing sensitive, like an .env file, has been staged. If you use a .gitignore, keep it current so such files never show up.
With this workflow, you can run an agent with confidence: you start clean, it works on a branch, you checkpoint good states, and main is safe.
Comments
No comments yet. Have a question or a tip? Be the first to share it.