Skip to content

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)
  1. Why commit before a big change
  2. Give the agent its own branch
  3. Review the diff and run the tests
  4. Go back safely with git restore and git reset
  5. Small commits with clear messages
  6. Common mistakes
  7. Frequently asked questions
  8. Checklist

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
  • --short prints 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(+)
  1. git add -A stages the whole tree: new, modified and deleted files (git add).
  2. git commit -m records the commit with that message (git commit).
  3. The output shows the branch, the short hash and a summary. 59ba9a5 is 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).
  1. git switch main goes back to the main branch.
  2. git merge --ff-only only merges if it can move main up to the branch's last commit without a merge commit. If main moved on in the meantime, it refuses and exits with an error, and you decide what to do.
  3. git branch -d deletes 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).
safe points in the git historymainb1624a6safe point59ba9a5main moves up01999c6agent/bulk-discountcommit01999c6attempt 2merge --ff-onlygit reset --hardcommit before askinguncommitted work: safe to throw awayif it is committed, you can go backwhat is not committed, git reset --hard deletes for good

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;
 }
  1. Lines starting with + are new, lines with - were removed, and the rest is context.
  2. The first hunk (@@ -1,6 +1,7 @@) adds a console.log to addLine. Nobody asked for it: it is leftover debugging.
  3. 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.

ask, review, then commit or go backask for iton the agent's branchreviewgit status · git diffnpm testis it right?only what you askedand tests are greenyesnodiscard (agent's branch)git restore <file>git reset --hard · git clean -fkeep itgit add -p · git commit -mone commit per changeask againbefore asking: a clean git status and a safe-point commitreview before the commit, not aftergit diff skips new files: look for ?? in git status

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.

SituationCommandWhat you lose
One file came out wronggit restore <file>That file's uncommitted changes
You staged something by mistakegit restore --staged <file>Nothing: the change stays in the file
The whole attempt went wronggit reset --hardEvery uncommitted change to tracked files
New files were left behindgit clean -n, then git clean -fThe untracked files
The agent's commit has to gogit reset --hard <commit>Later commits, recoverable with git reflog
You already shared itgit 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
  1. 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.
  2. git reset --hard with no commit uses HEAD: files and index go back to the last commit.
  3. The new file survived the reset: git reset --hard does not deal with files git does not track.
  4. git clean -n shows what it would delete without deleting anything. -f deletes. Without -f, git refuses by default. If the agent created folders, add -d: git clean -nd and git 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 with git reflog. Use it only on the agent's working branch, after a look at git 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
  1. git reset --hard 59ba9a5 moves the branch to the safe point and puts the files back the way they were there.
  2. git reflog -3 shows the last three moves. HEAD@{0} is where you are; HEAD@{1} is the move before.
  3. 'HEAD@{1}' is quoted because in PowerShell the unquoted form fails. In bash and zsh it works either way.
  4. 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
  1. git restore --staged unstages the file; the change stays in the file.
  2. git add -p shows each hunk and asks. We answered n to the first (the console.log) and y to the second (the discount). The other answers worth knowing: q to quit and s to split a hunk, when it can be split.
  3. The index now reads 4 +++- instead of 5 ++++-: the console.log stayed out.
  4. git restore src/cart.js throws away what did not make it into the commit. Since it restores from the index, which now matches the commit, the discount stays and the console.log goes. git status --short prints 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
git cheat sheet: which command for each casesituationcommandsee what changedgit status --short · git diffsafe point before askinggit add -A · git commit -m "..."a branch for the agentgit switch -c agent/<task>discard one filegit restore <file>unstage one filegit restore --staged <file>discard everything uncommittedgit reset --harddelete new untracked filesgit clean -n · git clean -fgo back to a safe pointgit reset --hard <commit>undo on a shared branchgit revert <commit>recover after a resetgit reflogin cyan: deletes uncommitted workuse it only on the agent's branch, after checking git status

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 --hard later, 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 ?? in git status.
  • Running git reset --hard without looking first. A one-second git status saves 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 ??) and git 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 --hard and git clean -f only on the agent's branch.
  • You merged with git merge --ff-only and 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

Luis Morales

Luis Morales

Frontend and Shopify developer, EcomLabs

@luismoralesdev on Instagram

Published October 30, 2026 · updated November 1, 2026

← Blog