published fresh until

My agent ran git reset --hard and wiped my changes: recover and block

Summary When an agent runs git reset --hard and removes uncommitted work, recover from reflog or dangling objects where possible, then enforce guardrails with permission deny rules or PreToolUse hooks; the January 2026 post says CLAUDE.md alone was insufficient.

Not for docker prune guardrails; Codex undo; confirming destructive MCP calls

For your agent
claude mcp add --transport http --scope user whatworks https://whatworks.dev/mcp

Read-only, no account. Sign-in is only for trial reports: /connect.md

What this thread does

The problem

You ask the agent for something small, such as fixing line endings, and it reaches for git reset --hard, discarding uncommitted changes to tracked files in the working tree. Your instructions file already said not to.

What the thread shows

In a r/ClaudeCode post by u/Odd-Outlandishness53 (16 Jan 2026) the author asked Claude to fix CRLF to LF endings. Instead of the safe git add --renormalize . it ran git reset --hard. Their CLAUDE.md contained a capitalised rule against reverting files without asking. The top replies agree on one point: do not rely on the instructions file.

  • Hooks (top comment): add a PreToolUse hook that blocks specific git operations. Commenters pointed to a shared gist, to disler's claude-code-damage-control (no licence file in the repo and none detected by GitHub; PreToolUse hooks plus a patterns file that block commands such as rm -rf and protect paths like ~/.ssh) and to another author's safety-hooks collection.
  • Permission deny list (a commenter; another called it the real answer): add the risky commands to permissions.deny.
  • Why not CLAUDE.md: a commenter claimed it is ignored once context fills (they said around 45%; that number is theirs, not measured here). Anthropic's own docs say permission rules are enforced by Claude Code while CLAUDE.md is context.
  • Recovery: one commenter said a hard reset can sometimes be undone before git garbage-collects orphaned commits; another said it cannot be recovered. Both are partly right (see below).

How to apply it

1. Deny the commands in .claude/settings.json:

{
  "permissions": {
    "deny": [
      "Bash(git reset *)",
      "Bash(git checkout *)",
      "Bash(git restore *)",
      "Bash(git clean *)",
      "Bash(git stash drop *)"
    ]
  }
}

Claude Code's docs say deny rules are checked first, match each part of a compound command, and put the * after the subcommand. They also warn that a rule matches command text, so variants such as git -C dir reset may slip past; test a few, or add a PreToolUse hook that inspects the full command. 2. If you want read-only git (status, diff, log) to stay prompt-free, allow those explicitly, as a commenter wished. 3. Before asking an agent to touch git state, save your work: git stash push -u -m before-agent or a WIP commit. 4. Tell it the safe alternative, since it cannot guess: for line endings, git add --renormalize ..

If it already happened (git behaviour, not from the thread)

  • Committed work: git reflog lists where HEAD was; git reset --hard <old-sha> or a new branch from that sha restores it, until garbage collection.
  • Staged but never committed: the content may still exist as dangling blobs; git fsck --lost-found lists them and writes them under .git/lost-found/ (commits and other objects separately; a blob is saved as a file with its contents).
  • Changes that were never staged or committed are not in git at all; look to your editor's local history or backups.

Mistakes to check

  • A deny rule for git reset but not git checkout -- . or git clean -fd, which also destroy work.
  • Letting the agent write its own guard hook and then edit it later; keep hook files outside what it may edit.

Limits

Not run by me. Scores are modest, and the thread reports no measurement of how well any option works.

Sources

Checked against primary sources:

  • Claude Code permissions docs: permission rules are enforced by Claude Code while CLAUDE.md only shapes what Claude tries; Bash rules match command text, so git -C . push and similar forms are not matched by a git push * rule; the docs point to a PreToolUse hook for inspecting the full command; a PreToolUse hook cannot override a matching deny rule.
  • disler/claude-code-damage-control README and repo metadata: PreToolUse hooks with a patterns.yaml, protected paths such as ~/.ssh/; the README labels it MIT, but there is no licence file and GitHub detects none.
  • git-fsck manual: --lost-found behaviour.

Not checked here: the Reddit post, its scores and comment counts, the 45% context figure, the shared gist and the other safety-hooks collection (Reddit not opened). The reflog and recovery steps are general git behaviour, not tested here.

Started from a post by @Odd-Outlandishness53, packaged by the editor. Original post

Subjects

More in agent workflows

For agents

Copy a short instruction for an agent that has WhatWorks connected, or open this page as Markdown.

# My agent ran git reset --hard and wiped my changes: recover and block
Summary: When an agent runs `git reset --hard` and removes uncommitted work, recover from reflog or dangling objects where possible, then enforce guardrails with permission deny rules or PreToolUse hooks; the January 2026 post says CLAUDE.md alone was insufficient.
Copy for agent
get the whatworks thread agent-ran-git-reset-hard-lost-my-changes-recover-and-block-it and help me try it

Follow this thread

Following tells you when the verdict changes, a new version ships or a new option is added. Your agent mentions it the next time you ask WhatWorks.

Sign in with GitHub to follow. Nothing is posted for you.

Sign in with GitHub

Or ask your agent
follow the whatworks thread agent-ran-git-reset-hard-lost-my-changes-recover-and-block-it
WhatWorks

Connect your agent MCP

Paste this into your agent

Connect me to WhatWorks: add https://whatworks.dev/mcp/login and sign me in so I have an account. Steps: https://whatworks.dev/connect.md.

Or run this in your terminal
  • Claude Codeclaude mcp add --transport http --scope user whatworks https://whatworks.dev/mcp/login && claude mcp login whatworks
  • Codexcodex mcp add whatworks --url https://whatworks.dev/mcp/login && codex mcp login whatworks