Skip to main content
Article
ai-agentsgitsecurityautomationdata-loss

Preventing Catastrophic Git Operations by AI Agents

Prevent catastrophic data loss from AI agents executing `git reset --hard`. Implement robust Git safeguards and strict permissions to ensure AI only performs safe, controlled operations, protecting your codebase from destructive automation.

intermediate10 min5 steps
The play
  1. Grant Least Privilege to AI Agents
    Configure your AI agent's Git credentials with the minimum necessary permissions. For most analysis or suggestion tasks, grant read-only access. Only provide write access under strict conditions, and preferably to feature branches only.
  2. Implement Protected Branches
    Configure your Git hosting service (GitHub, GitLab, Bitbucket) to protect critical branches (e.g., `main`, `master`). Require pull requests, code reviews, and restrict direct pushes to these branches, especially by automated systems.
  3. Utilize Git Hooks for Pre-validation
    Implement client-side `pre-commit` hooks or server-side `pre-receive` hooks to block or warn against destructive commands like `git reset --hard` or `git push --force` when performed by automated systems or without proper review. This acts as a last line of defense.
  4. Introduce Human-in-the-Loop Approvals
    For any AI-driven action that modifies critical code paths or performs potentially destructive Git operations, require explicit human approval before execution. This ensures oversight on high-impact changes.
  5. Monitor and Audit Git Activity
    Implement comprehensive logging and monitoring of all Git operations, especially those initiated by AI agents. Set up alerts for suspicious or destructive commands (`git reset --hard`, `git push --force`) to detect and respond to issues immediately.
Starter code
git config --global alias.safe-reset-hard '!f() { read -p "Are you ABSOLUTELY sure you want to hard reset the current branch? This is destructive! Type 'yes' to confirm: " confirmation; if [[ "$confirmation" == "yes" ]]; then git reset --hard "$@"; else echo "Operation cancelled."; fi; }; f'
Preventing Catastrophic Git Operations by AI Agents — Action Pack