What this covers
AI coding agents now ship with the ability to run git add, git commit, and sometimes git push. The danger isn’t the tool, it’s the trust boundary: a single misjudged commit can leak a secret, rewrite a teammate’s history, or bury a real change inside a 47-file mega-commit. This guide covers how to set commit message conventions for AI, where to draw commit boundaries, and which actions you should never delegate.
Who this is for
Developers using Cursor agent mode, Claude Code, Codex, or any tool that has its own shell. If you ever say “yes, run it” without reading the staged diff, this guide is for you. It assumes you already use git daily and care about a clean history.
When to reach for it
Read this before flipping the “auto-commit” toggle or onboarding a teammate to agentic coding. Re-read it after the first time an agent commits something embarrassing - because it will, and what you change after that is what actually sticks.
Before you start
- Decide where the trust boundary lives: agent stages, you commit; or agent commits, you push. Both work; pick one explicitly.
- Add a
CLAUDE.mdorAGENTS.mdat repo root with hard rules (no force pushes, no amends on shared branches, no commits tomain). - Make sure
.gitignorealready covers.env*, credential files, and any local cache the agent might wander into. - Configure a pre-commit hook (e.g.
gitleaks,detect-secrets) so a secret can’t ship even if the agent and the human both miss it.
Step by step
- Set rules in
CLAUDE.md/AGENTS.md: never commit secrets, never amend published commits, never commit tomain, nevergit push --force. - Use a “review then commit” flow: have the agent stage files and write the message, then you run
git diff --cachedbefore pressing enter. - Have the agent propose the commit message in conventional-commit form (
feat:,fix:,chore:), but you finalize it - subject lines drift fastest. - Cap commit size in your rules (“one logical change per commit, max ~200 changed lines”). Big AI commits are where review fatigue silently fails.
- After commit, run
git log -1 --statand confirm the file list matches what you expected. Surprise files = roll back withgit reset HEAD~1.
Commit message template for agents
Drop this into AGENTS.md so the agent doesn’t reinvent the format each session:
type(scope): imperative summary under 60 chars
- What changed (1-3 bullets, files/areas)
- Why (link to ticket or short rationale)
- Tests run / verification done
Example output you should expect from the agent: fix(auth): refresh token before retry on 401 followed by 2-3 bullet lines, not a 200-word essay.
What NOT to let AI commit
- Anything matching
**/.env*,**/secrets.*,**/*.pem,**/credentials.json- even when the agent insists “it’s only local”. - Binary files larger than ~1MB without you confirming - lockfile-style binaries (
*.sqlite, big PNGs) bloat history. - Migrations, schema changes, or anything under
infra/andterraform/- human review required. - Generated files that already have a script (
dist/,build/,*.lockrebuilds) - commit the script change, not the output. - Merge commits on shared branches - merges encode intent; let a human decide the strategy.
Recommended workflow
stage -> agent proposes message -> human reads diff -> human commits -> human pushes. The agent never owns the final git push on a branch that has a PR open. For solo branches, you can let it push - but require it to print the remote name and branch name before doing so, so you catch a typo before it lands.
FAQ
- Should I let the agent run
git commit --amend? - Only on commits it created in the current session and that haven’t been pushed. Never on a teammate’s commit. - What about
git rebase? - Allow on local feature branches; forbid on anything that’s been pushed and shared. Put this inAGENTS.md. - Can the agent write the PR body too? - Yes, but treat it like a draft - the agent doesn’t know which reviewer cares about which detail.
- Conventional commits vs. plain English? - Either is fine; pick one in your repo rules so the agent stops switching styles.
- How do I undo an unwanted AI commit? -
git reset --soft HEAD~1keeps the changes staged,git reset --hard HEAD~1discards them. Push hasn’t happened yet if you followed the rules above. - Does this slow things down? - The first week, yes. By week three the agent has learned your style and review takes 10-15 seconds per commit.
Common mistakes
- Letting the agent auto-commit unreviewed because “it’s a small change” - the small changes hide the worst surprises.
- Allowing
git commit -am(auto-stage modified files) - sweeps in unrelated edits. - Letting the agent amend shared commits to “clean up” history - rewrites public history.
- No
CLAUDE.md/AGENTS.md- the agent invents conventions per session. - Trusting verbal “don’t touch X” - rules need to be written, in the repo, or they decay within a session.
- One mega-commit per agent session - kills bisect-ability and makes rollback all-or-nothing.
Related
Tags: #AI coding #Tutorial