Skip to content
mentoor

Topic 4 of 5 · 5 min read

Git rules in agent memory files

Goal: Write the git rules an agent needs in a memory file and spot which rule a given agent commit broke.

Write your git rules in the file the agent reads at the start of every session, make them short and checkable, and then review commits against them. Agents follow written rules far better than rules in your head, but only if the rules are specific.

Check yourself · 1 of 3

Why put git rules in an agent's memory file?

Choose one answer

Why it matters

An agent will not know that your team uses Conventional Commits, forbids force-push, or wants no Co-Authored-By line unless it is told. Memory files such as CLAUDE.md, AGENTS.md or Cursor rules exist for this. The open AGENTS.md convention is read by several agents, so one file can serve more than one tool. Without rules, every session invents its own style and history becomes noisy.

How to do it

Cover five areas, each as a direct instruction:

  1. Message format. Name the format and show one example. Conventional Commits uses type(scope): summary, such as fix(auth): reject expired tokens.
  2. Commit size and staging. One logical change per commit; stage by explicit path; never git add . or git commit -a.
  3. Attribution. State whether to add a trailer, and which one, or to add none.
  4. Forbidden commands. List them: push --force, reset --hard on shared branches, --no-verify, clean -fd, rewriting pushed history.
  5. When to commit. Only when asked, or after each green step. Never commit on main.
## Git rules
- Commit only when I ask. Never on `main`; work on `agent/<task>`.
- Message: Conventional Commits, imperative, max 72 chars in the title.
- Stage explicit paths. Never `git add .` or `git commit -a`.
- Never run: `git push --force`, `git reset --hard`, `git clean -fd`,
  `git commit --no-verify`.
- Do not add Co-Authored-By trailers.

Keep it under a page. Long files get skimmed by models too, and a rule that is too vague, such as "write good commits", cannot be checked.

Before and after

Before: "Please use good git practices." After: the block above. A commit titled "updates" that includes .env.local now breaks three named rules, and a reviewer can say which.

What goes wrong

  • Rules the agent cannot follow, such as "squash when merged", which is a team step.
  • Rules that drift from what CI enforces. Back important rules with a hook or branch protection, because models do sometimes ignore instructions.
  • Contradictions between a global file and a project file.

Examples

Which rule did this commit break?

commit 91c3e07
Title: updates
Files: src/api/orders.ts, .env.local, dist/bundle.js

The title breaks the message-format rule, .env.local breaks the secrets rule, and dist/bundle.js breaks the build-output rule. Each is a one-line instruction you can add.

A rule that cannot be checked

'Keep commits clean' gives the agent nothing to act on. Replace it with 'Title in Conventional Commits form, at most 72 characters, one logical change, tests passing at that commit'. A reviewer can now check each part.

Key points

  • Put git rules in the memory file the agent reads every session, such as CLAUDE.md or AGENTS.md.
  • Cover message format, staging, attribution, forbidden commands and when to commit.
  • Make each rule specific enough that a reviewer can say whether a commit broke it.
  • Back critical rules with hooks, CI or branch protection, because agents sometimes ignore instructions.
  • Keep the file short and free of contradictions between global and project files.

Common mistakes

  • Writing vague rules such as 'follow best practices'.
  • Leaving forbidden commands unlisted because 'nobody would run that'.
  • Relying on the memory file alone for rules where a mistake is costly.
  • Letting global and project files give opposite instructions.

Check yourself · 2 of 3

Which rule is specific enough to check?

Choose one answer

Check yourself · 3 of 3

A rule says never to run git push --force, yet an agent did. What is the best follow-up?

Choose one answer

Practice items, not counted towards your mastery.

Start here

More for this topic