Undoing AI-Generated Git Commits Safely: Revert, Reset and Recovery
An AI-generated commit may introduce incorrect code, exposed configuration, broken tests, or an unsafe dependency. The recovery method depends on whether the commit is private or already sharedβnot on how quickly you want it gone.
Start with:
git status --short --branch
git show --stat --oneline HEAD
git diff HEAD~1..HEAD
Preserve any newer uncommitted work before changing history.
Shared branch: create a revert
If the commit was pushed or other people and agents may have based work on it, use:
git revert HEAD
This creates a new commit that reverses the patch while preserving auditable history. Run tests and review the revert; later commits may overlap with the bad change.
For a production incident, code rollback may not reverse database migrations, infrastructure changes, model configuration, or leaked secrets. Handle those as separate recovery tasks and rotate exposed credentials immediately.
Private branch: repair or reset deliberately
If the commit is local and unshared:
git reset --soft HEAD~1
This moves the branch back while keeping changes staged for review. A mixed reset keeps changes but unstages them. Avoid --hard unless the exact discarded content is understood and recoverable.
For a small correction, amend the private commit after editing and testing:
git commit --amend
Do not rewrite a shared branch merely because an agent produced the commit.
Recovery checkpoints for coding agents
Before an agent commits:
- verify repository and branch;
- inspect staged files and secret scans;
- run relevant tests;
- keep the commit scoped to one task;
- require human approval for protected branches;
- record the model/tool workflow where policy requires it.
After rollback, add a regression test that represents the failure. The AI Testing & Evaluation hub explains quality gates, while GitHub Actions covers automation.
If recovery is uncertain
Create a rescue branch before history changes:
git branch rescue/before-rollback
Then inspect reflog and commits without force-pushing. A branch is cheap; lost human work and incident evidence are not.
The Git foundation covers reset, revert, reflog, and worktrees. Connect rollback ownership to Coding Agents and production recovery to AI Operations.
Use revert for shared history, reset or amend only for private history, and test every recovery path before redeployment.