Skip to content

hookGit and releases

Block Git Reset Hard

A Claude Code hook that blocks git reset --hard and forced git clean in the shell, so uncommitted work is never wiped without you running it.

npx workspacetemplates add block-git-reset-hard --tool claude-code

Writes 1 file to .claude/

.claude/settings.jsonmerge into your file16 lines · 991 BFile
{
  "hooks": {
    "PreToolUse": [
      {
        "matcher": "Bash",
        "hooks": [
          {
            "type": "command",
            "command": "node -e \"let d='';process.stdin.on('data',x=>d+=x).on('end',()=>{let j={};try{j=JSON.parse(d)}catch(e){return}const i=j.tool_input||{};const c=String(i.command||'').replace(/\\x22[^\\x22]*\\x22|\\x27[^\\x27]*\\x27/g,' ')+' ';const t=c.split(/&&|[|][|]|[;|]/).map(s=>s+String.fromCharCode(32));const reset=t.find(s=>/^[ ]*git[ ]+reset[ ].*--hard/.test(s));const clean=t.find(s=>/^[ ]*git[ ]+clean[ ]/.test(s)&&/[ ](-[a-zA-Z]*f[a-zA-Z]*|--force)[ ]/.test(s)&&!/[ ](-[a-zA-Z]*n[a-zA-Z]*|--dry-run)[ ]/.test(s));const h=reset?'git reset --hard':clean?'git clean with force':'';if(h){console.error('Blocked: '+h+' throws away uncommitted work for good. Use git stash or a new branch to keep it, or ask the user to run this command themselves.');process.exitCode=2}})\"",
            "timeout": 10
          }
        ]
      }
    ]
  }
}

What it does

Two git commands erase work that no commit holds: reset with the hard option and clean with force. This PreToolUse hook refuses both when the agent tries them in Bash.

A clean run with the dry-run flag still passes, since it only lists files. The message points the agent to stash or a scratch branch, which keep the work recoverable.

Example

you › Undo everything and get back to a clean tree with git reset --hard.

agent › The command is blocked. The agent offers to stash your changes first and asks whether you want to run the reset yourself.

Questions

Is git reset --soft allowed?
Yes. Only the hard option is refused, because soft and mixed resets keep your file contents intact.
Can I preview what clean would delete?
Yes. git clean -n or --dry-run is allowed and lists the files without removing anything.
  • Block Git No Verify

    HookGit and releases

    Free
  • Detect Merge Markers

    HookGit and releases

    Free
  • Inject Git Branch

    HookGit and releases

    Free
  • Session Git Context

    HookGit and releases

    Free