# Reverting Safely — Safe Autonomous Code Fixing

Source: https://www.skillbyai.com/en/safe-autonomous-code-fixing/m-revert

> Undo without rewriting history.

## git revert, monitoring, follow-up

Despite every gate, some merged fixes will be wrong. Monitor error rates and alerts after merges, and keep reverts easy: **git revert** creates a new commit that undoes the change, preserving history (unlike rewriting history on a shared branch). Small, single-purpose fix commits revert cleanly. After a revert, add the case that slipped through to tests or gates, and record it in the bot's metrics.

## Reverting a bot commit, run

I ran this with Python 3 (standard library) and, where it uses git, real git in a throwaway temporary repository. Candidate patches are written by hand to stand in for model output. In a temporary repository, a bot commit changes a limit from 100 to 10; git revert adds a "Revert ..." commit on top, the history keeps all three commits, and the file is back to LIMIT = 100.

```python
import os, subprocess, tempfile
d = tempfile.mkdtemp()
def git(*a): return subprocess.run(["git", *a], cwd=d, capture_output=True, text=True).stdout.strip()
git("init", "-q"); git("config", "user.email", "bot@example.com"); git("config", "user.name", "fix-bot")
open(os.path.join(d, "app.py"), "w").write("LIMIT = 100\n"); git("add", "."); git("commit", "-qm", "initial")
open(os.path.join(d, "app.py"), "w").write("LIMIT = 10\n"); git("add", "."); git("commit", "-qm", "autofix: tighten limit (bot)")
git("revert", "--no-edit", "HEAD")
print(git("log", "--format=%s"))
print("app.py now:", open(os.path.join(d, "app.py")).read().strip())
```

Output:

```
Revert "autofix: tighten limit (bot)"
autofix: tighten limit (bot)
initial
app.py now: LIMIT = 100
```

## One fix per commit

Keeping each automated fix in its own commit makes reverting it a one-command operation.

**Quiz:** Why prefer git revert over rewriting history on main?

- [ ] It skips CI
- [ ] It deletes the bug permanently from all clones
- [ ] It is the only command git has
- [x] It undoes the change with a new commit and keeps shared history intact

*Answer:* It undoes the change with a new commit and keeps shared history intact. Shared history should not be rewritten.
