How to Stop Your AI Agent From Making Unwanted Code Changes

AI agents often behave like brilliant but overly enthusiastic interns — eager to help, but sometimes too eager. Even with clear instructions, they’ll occasionally decide, “Let me fix this other thing too…” and suddenly your codebase contains changes you never asked for.

This happens because modern LLMs are trained to be helpful. But in software projects, “extra helpful” can quickly turn into chaos. I’ve had situations where a tiny, unintended edit was hidden inside seventy modified files. Reviewing that manually through git diff is painful, and asking the agent to revert it is even worse — it doesn’t remember your file state, only the conversation.

The solution isn’t complicated. It’s classic engineering discipline: version control. We already commit early and often, so why not enforce the same rules on our AI agents? Here’s the workflow I use with goose to guarantee clean snapshots and easy rollbacks.

1. Set Up Version Control

Use GitHub.

I prefer the GitHub CLI (gh) because goose interacts with it perfectly.

The GitHub MCP Server is a solid alternative if that fits your workflow better.

2. Always Branch First

Never let an agent work directly on main.

Start on a feature branch so the agent’s changes remain isolated and reversible.

3. Add Rules in a Context File

Here’s the real game-changer.

In a .goosehints or AGENTS.md file, include this line:

“Every time you make a change, make a commit with a clear message.”

This single rule transforms the entire collaboration:

  • It forces automatic checkpointing
  • It creates a perfect chronological trail of the agent’s decisions
  • It turns your git history into an instant undo system

No more manually hunting for hidden edits.

4. Work With Confidence

Once the rule is in place, let the agent build, refactor, or fix freely.

If it goes off track, all you need is:

  • git log to inspect its steps
  • A quick diff to review changes
  • Or simply: “Revert to commit abc123.”

Your workflow becomes transparent and reversible.

The Result

By applying one simple habit — frequent commits — you eliminate anxiety from AI-assisted development. The agent remains helpful, but you stay in control.

  • No more digging through seventy files for a rogue change
  • No more hoping the agent “remembers” what it did
  • No more surprise edits hiding in your repo

Just clean commits, clear history, and total confidence.

Try goose on your next project.

Your future self (and your git history) will thank you.

(Thumbnail credit: https://petesena.medium.com/?source=post_page—byline–124b9288e551—————————————)

Mashraf Aiman
CTO Zuttle
AGS NIRAPAD Alliance

Similar Posts