Skip to content

Instantly share code, notes, and snippets.

@Leksat
Last active June 4, 2026 08:21
Show Gist options
  • Select an option

  • Save Leksat/5be530ddb9a31394b740e0291ca1e023 to your computer and use it in GitHub Desktop.

Select an option

Save Leksat/5be530ddb9a31394b740e0291ca1e023 to your computer and use it in GitHub Desktop.
Force Claude Code to load a skill before running certain commands
{
"hooks": {
"PreToolUse": [
{
"matcher": "Bash",
"hooks": [
{
"type": "command",
"command": "~/.claude/hooks/git-gate.sh"
}
]
}
]
}
}
name git-policies
description User's git conventions. Must be loaded before running any git/gh command.

Git policies

Every git command must be prefixed with the sentinel so the git-gate hook lets it through:

echo "git policies applied" && git ...

{Your other git rules}

#!/usr/bin/env bash
set -euo pipefail
cmd=$(jq -r '.tool_input.command // ""')
# Fire only when `git` appears as a command token (start, or after a shell
# separator). Catches `git ...`, `cd /x && git ...`, `git -C /x commit`, etc.
if ! grep -qE '(^|[;&|`(])[[:space:]]*git([[:space:]]|$)' <<<"$cmd"; then
exit 0
fi
sentinel='echo "git policies applied" &&'
case "$cmd" in
"$sentinel"*)
rest="${cmd#"$sentinel"}"
;;
*)
jq -nc '{
hookSpecificOutput: {
hookEventName: "PreToolUse",
permissionDecision: "deny",
permissionDecisionReason: "Load the /git-policies skill, then re-run the command prefixed with a sentinel from the skill"
}
}'
exit 0
;;
esac
if grep -qE '(^|[[:space:]])(push|commit)([[:space:]]|$)' <<<"$rest"; then
jq -nc '{
hookSpecificOutput: {
hookEventName: "PreToolUse",
permissionDecision: "ask"
}
}'
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment