Topic 1 of 5 · 5 min read
Small focused agent commits
Goal: Split an agent's mixed working tree into small commits that each hold one logical change and pass the tests.
Make every commit hold one logical change that builds and passes its tests, and stage files by explicit path so nothing rides along by accident. An agent can touch twenty files in ten minutes. Git only helps you if that work lands as steps you can read, test and undo one by one.
Check yourself · 1 of 3
An agent finished a task and the tree has a bug fix, a rename and a formatter run. What is the best way to commit?
Why it matters with agents
Agents such as Claude Code, Codex, Cursor and Copilot agent mode happily mix a bug fix, a rename, a formatter run and a new dependency in one working tree. If that becomes one commit, a reviewer cannot judge it, git bisect cannot narrow a regression, and reverting the bug fix also reverts the rename. Small commits are the cheapest safety net you have.
How to do it
- Commit after each green step. Ask the agent to work in steps and to stop when the tests pass. Commit then, before the next step starts. A commit is a save point you can return to.
- One reason to exist. If the message needs the word "and", split it. A refactor and a behaviour change never share a commit.
- Stage by path, not by sweep. Use
git add src/billing/tax.ts tests/tax.test.ts. Avoidgit add .andgit commit -a: they pick up everything the agent or another process left in the tree, including scratch files and other people's edits. - Split a mixed tree.
git add -plets you stage hunk by hunk. Check what is staged withgit diff --stagedbefore every commit. - Each commit passes alone. Run the tests on the staged state. A commit that only works together with the next one breaks bisect.
git status --short
git add src/billing/tax.ts tests/tax.test.ts
git diff --staged --stat
git commit -m "fix(billing): round VAT per line item"
Before and after
Before: one commit, "update billing", with 14 files: the VAT fix, a renamed helper, a reformatted folder and a lockfile change. After: three commits. The rename comes first, the fix second with its test, the formatter run last on its own. Each can be reviewed in minutes and reverted alone.
What goes wrong
- The sweep commit.
git add .stages a.env.localor a debug log the agent created. - The red commit. Work is committed mid-step with failing tests, so the history has no trustworthy points.
- Mixed noise. Whitespace or import-order changes hide the three lines that matter.
Examples
Splitting a mixed tree
git status shows 9 changed files after one agent session. Three belong to a pagination fix, four to a renamed config helper and two are formatter output. Stage the rename first and commit it, then the fix with its test, then the formatter run. If the fix turns out wrong, git revert removes only the fix.
The stray file
$ git add .
$ git status --short
A src/export/csv.ts
A tmp/debug-output.json
M package-lock.json
The task was a CSV export. tmp/debug-output.json is the agent's scratch file and the lockfile change is unrelated. Unstage both with git restore --staged tmp/debug-output.json package-lock.json and commit only the export.
Key points
- A commit should hold one logical change and pass the tests on its own.
- Commit after each green step so every step of agent work is a save point.
- Stage by explicit path or with
git add -p, never withgit add .orgit commit -aon a tree an agent has touched. - Run
git diff --stagedbefore each commit to see exactly what will be recorded. - Refactors, formatting and behaviour changes belong in separate commits.
Common mistakes
- Letting the agent finish a long task and committing everything at the end.
- Using
git add .because the tree 'only has the agent's changes'. - Committing red tests with the plan to fix them in the next commit.
- Mixing a rename or reformat with a behaviour change so the diff hides the real edit.
Check yourself · 2 of 3
Why is git add . risky after an agent session?
Check yourself · 3 of 3
Which command lets you stage only some changes inside one file?
Practice items, not counted towards your mastery.
Start here
- Pro Git book (opens in a new tab)BookFree
The full reference for staging, branching, reset and revert, written by Git's maintainers and community.
- Conventional Commits (opens in a new tab)DocsFree
A short, widely used commit message format you can paste into an agent's rules.
- Push protection (GitHub Docs) (opens in a new tab)DocsFree
How GitHub blocks pushes that contain secrets, and what to do when a push is blocked.