DEV Community

Nic Flemmer
Nic Flemmer

Posted on

Keep undo points in hidden git refs, not commits

I wanted an agent's turns to be undoable one at a time, without touching the branch history. Committing after every turn would do it, but then your branch is full of commits you didn't write, and they'd go out with your next push.

What I do instead: at the start and end of every turn, the app records the state of the task's files as a checkpoint and keeps each one as a hidden git ref under refs/styx/checkpoints/. A ref outside refs/heads/ isn't a branch, so it doesn't show in git branch, plain git log doesn't list it, and a normal git push doesn't send it. It doesn't touch your index or your commits either.

You can see them with plain git:

git for-each-ref refs/styx/checkpoints/
Enter fullscreen mode Exit fullscreen mode

Because they're real refs, git's garbage collection keeps the objects they point to, so nothing needs a separate store.

Undo then works from those two snapshots: take what a turn changed and reverse just that, leaving later turns and your own edits alone. If a later change touched the same lines, it refuses rather than guess.

The trade-offs: the refs live in your clone only, so teammates don't get them, and they're not a backup. They're cleaned up when a task is archived. And anything that rewrites history under them (a rebase of the branch, say) would orphan them, which is why the app merges main into a task instead of rebasing.

Have you tried hidden refs, git notes or stash for this kind of thing? What bit you?

Top comments (0)