A git workflow for AI coding agents: small commits before every change
Guide/ 12 min read
Commit before you ask, give the agent its own branch, review the diff and run the tests, and know which git command takes you back safely.
- Git
- AI
- Claude Code
In this article (8)
A safe git workflow for AI coding agents comes down to four habits: commit before you ask for a big change, let the agent work on its own branch, review the diff and run the tests before you commit its work. If something goes wrong, git restore and git reset take you back to the last safe point.
We ran every command in this article with git 2.54 in a throwaway repository: a store with a cart and its Node tests, where the "agent" adds a bulk discount (we simulated its edits by copying files, so the run can be repeated). The output you see is the real output. If what the agent broke is a specific error, first report the bug with the error, the steps and where to look.
Why commit before a big change
An agent can touch ten files in a minute. If you had uncommitted work when you asked, your changes and the agent's end up mixed together, and pulling them apart becomes manual work. A commit before you ask is a safe point: whatever happens next, you can diff against it and go back to it.
Start by checking the state with git status:
$ git status --short
M README.md
--shortprints one line per file.M(a space, then M): modified, not staged yet.??: a new file git does not track. You will see it when the agent creates files.- No output: nothing pending.
Commit that work before you ask for anything:
$ git add -A
$ git commit -m "Document how to run the tests"
[main 59ba9a5] Document how to run the tests
1 file changed, 2 insertions(+)
git add -Astages the whole tree: new, modified and deleted files (git add).git commit -mrecords the commit with that message (git commit).- The output shows the branch, the short hash and a summary.
59ba9a5is your safe point.
What about editor checkpoints? Claude Code keeps checkpoints and has /rewind to roll back to an earlier prompt, but its best practices warn that they only cover files Claude edited with its own file tools, not files a Bash command touched, and that they work alongside git rather than replacing it.
Give the agent its own branch
With the safe point in place, create a branch for the agent's task:
$ git switch -c agent/bulk-discount
Switched to a new branch 'agent/bulk-discount'
git switch -c creates the branch where you are and switches to it in one step; if the switch fails, the branch is not created. The agent/ prefix is our own convention, so the agent's work stands out in the branch list.
The branch is what buys you freedom. main stays as it was, and on the agent's branch you can throw work away, redo it and use git reset --hard without touching main or anyone on the team. When the result is good and committed, bring it in:
$ git switch main
Switched to branch 'main'
$ git merge --ff-only agent/bulk-discount
Updating 59ba9a5..01999c6
Fast-forward
src/cart.js | 4 +++-
test/discount.test.js | 15 +++++++++++++++
2 files changed, 18 insertions(+), 1 deletion(-)
create mode 100644 test/discount.test.js
$ git branch -d agent/bulk-discount
Deleted branch agent/bulk-discount (was 01999c6).
git switch maingoes back to the main branch.- git merge
--ff-onlyonly merges if it can movemainup to the branch's last commit without a merge commit. Ifmainmoved on in the meantime, it refuses and exits with an error, and you decide what to do. - git branch
-ddeletes the branch, but only once it is merged.
If an attempt is no good, drop the whole branch. -d refuses because the branch has commits main does not, and -D (short for --delete --force) deletes it anyway:
$ git branch -d agent/attempt-2
error: the branch 'agent/attempt-2' is not fully merged
hint: If you are sure you want to delete it, run 'git branch -D agent/attempt-2'
$ git branch -D agent/attempt-2
Deleted branch agent/attempt-2 (was d88c657).
Scroll the figure sideways to see all of it
Review the diff and run the tests
When the agent is done, do not commit yet. Look at what it touched first:
$ git status --short
M src/cart.js
?? test/discount.test.js
$ git diff --stat
src/cart.js | 5 ++++-
1 file changed, 4 insertions(+), 1 deletion(-)
Notice the gap: git status lists the new test with ??, but git diff --stat leaves it out. git diff with no arguments compares your files with the index, and a file git does not track yet is not in the index. If you only read the diff, you miss what the agent created.
Then read the full diff:
$ git diff
diff --git a/src/cart.js b/src/cart.js
index 57589c5..9009c7d 100644
--- a/src/cart.js
+++ b/src/cart.js
@@ -1,6 +1,7 @@
// Demo store cart. Prices in cents.
export function addLine(cart, line) {
+ console.log("adding", line.sku);
cart.lines.push(line);
}
@@ -11,8 +12,10 @@ export function removeLine(cart, sku) {
export function cartTotal(cart) {
let total = 0;
+ let items = 0;
for (const line of cart.lines) {
total += line.price * line.quantity;
+ items += line.quantity;
}
- return total;
+ return items >= 3 ? Math.round(total * 0.9) : total;
}
- Lines starting with
+are new, lines with-were removed, and the rest is context. - The first hunk (
@@ -1,6 +1,7 @@) adds aconsole.logtoaddLine. Nobody asked for it: it is leftover debugging. - The second hunk is the discount: it counts items and takes 10% off from 3. That is what you asked for.
To see new files too, stage everything and compare the index with the last commit (--staged is a synonym for --cached):
$ git add -A
$ git diff --staged --stat
src/cart.js | 5 ++++-
test/discount.test.js | 15 +++++++++++++++
2 files changed, 19 insertions(+), 1 deletion(-)
Then run the tests. In the demo, npm test passed 4 of 4: the two that already existed and the two new discount tests. If an existing test was changed so that it passes, ask why before you accept it.
Scroll the figure sideways to see all of it
Go back safely with git restore and git reset
git has three commands with similar names, and its documentation tells them apart like this: git revert makes a new commit that undoes other commits; git restore restores files without moving your branch; git reset moves the tip of the branch and changes history.
| Situation | Command | What you lose |
|---|---|---|
| One file came out wrong | git restore <file> | That file's uncommitted changes |
| You staged something by mistake | git restore --staged <file> | Nothing: the change stays in the file |
| The whole attempt went wrong | git reset --hard | Every uncommitted change to tracked files |
| New files were left behind | git clean -n, then git clean -f | The untracked files |
| The agent's commit has to go | git reset --hard <commit> | Later commits, recoverable with git reflog |
| You already shared it | git revert <commit> | Nothing: it adds a new commit |
In the demo, the agent's second attempt left a mess: two modified files and a new debug file.
$ git status --short
M src/cart.js
M test/cart.test.js
?? src/tmp-debug.js
$ git restore test/cart.test.js
$ git status --short
M src/cart.js
?? src/tmp-debug.js
$ git reset --hard
HEAD is now at 01999c6 Add 10% discount from 3 items
$ git status --short
?? src/tmp-debug.js
$ git clean -n
Would remove src/tmp-debug.js
$ git clean -f
Removing src/tmp-debug.js
- git restore
<file>puts that file back the way it is in the index; if you had not staged it, that is the last commit. Other files are left alone. - git reset
--hardwith no commit usesHEAD: files and index go back to the last commit. - The new file survived the reset:
git reset --harddoes not deal with files git does not track. - git clean
-nshows what it would delete without deleting anything.-fdeletes. Without-f, git refuses by default. If the agent created folders, add-d:git clean -ndandgit clean -fd.
Careful with
git reset --hard. It deletes every uncommitted change to tracked files, and work that was never committed cannot be brought back, not even withgit reflog. Use it only on the agent's working branch, after a look atgit status, and never with your own work pending.
To undo the agent's commit, move the branch back to the safe point. If you change your mind, the commit is not lost: git reflog records where HEAD pointed after every move.
$ git reset --hard 59ba9a5
HEAD is now at 59ba9a5 Document how to run the tests
$ git reflog -3
59ba9a5 HEAD@{0}: reset: moving to 59ba9a5
01999c6 HEAD@{1}: reset: moving to HEAD
01999c6 HEAD@{2}: commit: Add 10% discount from 3 items
$ git reset --hard 'HEAD@{1}'
HEAD is now at 01999c6 Add 10% discount from 3 items
git reset --hard 59ba9a5moves the branch to the safe point and puts the files back the way they were there.git reflog -3shows the last three moves.HEAD@{0}is where you are;HEAD@{1}is the move before.'HEAD@{1}'is quoted because in PowerShell the unquoted form fails. In bash and zsh it works either way.- The reflog does not last forever: by default entries are kept for 90 days, and 30 for commits no branch points to any more.
If the change is already on main or pushed, do not rewrite history: the git reset docs say not to do it with commits you have already given to somebody else. Use git revert, which records a new commit with the opposite change (it needs a clean working tree):
$ git revert --no-edit HEAD
[main 191bacc] Revert "Add 10% discount from 3 items"
Date: Fri Oct 30 10:00:00 2026 -0600
2 files changed, 1 insertion(+), 18 deletions(-)
delete mode 100644 test/discount.test.js
--no-edit takes the default message without opening the editor.
Small commits with clear messages
One commit per logical change. If the agent did two things, make two commits, or leave out what you did not ask for. In the demo, the diff held the discount (asked for) and a console.log (not asked for). Unstage the file first, then pick hunk by hunk with git add -p:
$ git restore --staged src/cart.js
$ git add -p src/cart.js
(1/2) Stage this hunk [y,n,q,a,d,k,K,j,J,g,/,e,p,P,?]?
(2/2) Stage this hunk [y,n,q,a,d,K,J,g,/,s,e,p,P,?]?
$ git diff --staged --stat
src/cart.js | 4 +++-
test/discount.test.js | 15 +++++++++++++++
2 files changed, 18 insertions(+), 1 deletion(-)
$ git commit -m "Add 10% discount from 3 items"
[agent/bulk-discount 01999c6] Add 10% discount from 3 items
2 files changed, 18 insertions(+), 1 deletion(-)
create mode 100644 test/discount.test.js
$ git restore src/cart.js
$ git status --short
git restore --stagedunstages the file; the change stays in the file.git add -pshows each hunk and asks. We answerednto the first (theconsole.log) andyto the second (the discount). The other answers worth knowing:qto quit andsto split a hunk, when it can be split.- The index now reads
4 +++-instead of5 ++++-: theconsole.logstayed out. git restore src/cart.jsthrows away what did not make it into the commit. Since it restores from the index, which now matches the commit, the discount stays and theconsole.loggoes.git status --shortprints nothing: clean.
For the message, the git commit docs suggest a short first line, no more than 50 characters, that sums up the change, then a blank line and a longer description if needed. "Add 10% discount from 3 items" is 29. With messages like that, the log becomes your list of safe points:
$ git log --oneline
01999c6 Add 10% discount from 3 items
59ba9a5 Document how to run the tests
b1624a6 Cart with tests
Scroll the figure sideways to see all of it
Common mistakes
- Asking for a change with your own uncommitted work. If you reach for
git reset --hardlater, it takes your work along with the agent's. - Letting the agent work on
main. A branch per task costs one command and lets you throw everything away with no fallout. - Reviewing only
git diff. It does not show new files; look for??ingit status. - Running
git reset --hardwithout looking first. A one-secondgit statussaves you from deleting what you meant to keep. - Resetting a shared branch. Once you have pushed, use
git revert. - One giant "agent changes" commit. When something breaks later, you will not know which part to revert.
- Relying only on editor checkpoints. They do not cover what a terminal command changed.
Frequently asked questions
Can the agent make the commits for me?
Yes. Claude Code can run git if you allow it, but review the diff before it commits. For git push, it is worth always being asked: Claude Code's best practices show exactly that rule, "ask": ["Bash(git push *)"], in .claude/settings.json.
Can I undo a git reset --hard?
Commits, yes: find them in git reflog and go back with git reset --hard 'HEAD@{1}' or the hash, while the entry has not expired. Changes that were never committed, no. That is why the commit comes before the request.
Branch or worktree?
For one task at a time, a branch is enough. For several agent sessions in parallel, Claude Code has claude --worktree <name>, which creates a git worktree on its own branch; the repository needs at least one commit first.
How often should I commit?
Every time the tests pass and the change makes sense on its own. With an agent, that is usually after each reviewed request. That is our rule of thumb, not a git rule.
Checklist
- A clean
git status, or your work committed, before you ask. - A new branch with
git switch -c agent/<task>. - You checked
git status --short(including??) andgit diff. - The tests pass and no existing test changed without a reason.
- Each commit does one thing and its first line sums it up in 50 characters or fewer.
git reset --hardandgit clean -fonly on the agent's branch.- You merged with
git merge --ff-onlyand deleted the branch.
With git in order, the agent can get things wrong without costing you anything. If what went wrong is an error, how to ask AI to fix a bug covers what to paste into the request, and what the agent should know about your project before it starts is in what context an AI coding agent needs. To review the plan before it touches any file, use Claude Code plan mode, and the whole method is in my 4-step Claude Code workflow.
Saw this on Instagram? Comment GIT on the Reel and I'll send you the guide.
Written by

Published October 30, 2026 · updated November 1, 2026