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
- Grant Least Privilege to AI AgentsConfigure 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.
- Implement Protected BranchesConfigure 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.
- Utilize Git Hooks for Pre-validationImplement 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.
- Introduce Human-in-the-Loop ApprovalsFor 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.
- Monitor and Audit Git ActivityImplement 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'