git-safety — Claude Code plugin: what it does, how to install
git-safety — a PreToolUse Bash hook for Claude Code that intercepts destructive git and gh CLI actions before execution.
MIT ★ 586 updated 2026-10-26
/plugin marketplace add karanb192/claude-code-hooks
/plugin install git-safety@claude-code-hooksWhat git-safety does
The plugin evaluates every Bash tool invocation against safety patterns before the shell runs it. Protected branches are fixed to main and master. Branch-dependent rules check the active branch locally via git branch --show-current, while other patterns scan the command string directly. Whenever an action matches a rule, the hook returns permissionDecision: "deny" alongside the rule identifier and explanation.
Rules respond to the HOOK_SAFETY_LEVEL environment variable. The default setting is high, blocking commits, merges, rebases, resets, and pushes when sitting on main or master, as well as deletion of those branches. It equally denies destructive GitHub CLI actions like gh pr merge, gh pr close, gh issue close, gh release delete, and gh repo delete. The strict profile adds a blanket ban on force-pushes with -f or --force, while leaving --force-with-lease operational. Setting it to critical disables all checks here to let block-dangerous-commands handle filtering.
This hook operates purely by denying: it provides no interactive prompt to confirm commands, logging every incident to ~/.claude/hooks-logs/<date>.jsonl.
What it helps with
- stop direct commits, merges, and rebases on main and master
- prevent accidental deletion of default project branches
- intercept destructive GitHub CLI operations before closing tickets or PRs
- restrict uncoordinated force-pushing across branches
Example prompts for the agent
- Commit these changes straight to main
- Run git push --delete origin master
- Close the open PR using gh pr close 4
How to install git-safety
Plugin marketplace
/plugin marketplace add karanb192/claude-code-hooks/plugin install git-safety@claude-code-hooksStep by step, with commands for each platform: Claude Code plugins: marketplaces, installing, and building your own
Questions about git-safety
How to install git-safety in Claude Code?
Run /plugin marketplace add karanb192/claude-code-hooks followed by /plugin install git-safety@claude-code-hooks inside Claude Code, then restart the application.
What does git-safety do?
It intercepts shell commands to prevent destructive Git and GitHub CLI actions, including editing protected branches, branch deletion, and closing issues or pull requests.
How do you configure the security level?
Set the HOOK_SAFETY_LEVEL environment variable to strict, high, or critical. Avoid editing script constants directly because subsequent plugin updates overwrite installed files.
Source text
git-safety · MIT
# git-safety
> Branch-aware git guardrails plus destructive gh CLI protection: denies the operations that rewrite shared history or close things you didn't mean to close.
A `PreToolUse` hook fires before every `Bash` call and checks the command against a tiered pattern list. On a match it returns `permissionDecision: "deny"` with the pattern id and reason, so Claude sees exactly why the command was refused. Branch-aware rules (`git commit`, `merge`, `rebase`, `reset`, `push` while on a protected branch) resolve the current branch via `git branch --show-current` at check time; everything else matches on the command text alone. Protected branches are `main` and `master`.
## Install
```
/plugin marketplace add karanb192/claude-code-hooks # once per machine
/plugin install git-safety@claude-code-hooks
```
Restart Claude Code, done. (Or from a shell: `claude plugin install git-safety@claude-code-hooks`.)
## What it does
| Event | Runs | What happens |
|-------|------|--------------|
| PreToolUse (`Bash`) | sync | Checks the command against the active safety level's patterns; on a match, denies with the pattern id and reason, and appends a log row. Non-matching commands pass through untouched (`{}`). |
Blocked at the default `high` level:
- `git push` naming `main` or `master` (any flags)
- `git branch -d` / `-D` / `--delete` on `main` or `master`
- `git commit`, `git merge`, `git rebase`, `git reset`, `git push` while the current branch **is** `main` or `master`
- `gh pr merge`, `gh pr close`, `gh issue close`, `gh release delete`, `gh repo delete`
`strict` adds any force-push (`--force` or `-f`; `--force-with-lease` stays allowed). `critical` applies no git-safety rules at all, deferring entirely to `block-dangerous-commands`.
**Composition with `block-dangerous-commands`:** that hook already blocks force-push (any, and to main/master) and `git reset --hard` on any branch, so at `high` the two don't overlap. Run git-safety at `strict` if you use it on its own.
## Configuration
Optional, set via environment variable:
| Variable | Default | Purpose |
|----------|---------|---------|
| `HOOK_SAFETY_LEVEL` | `high` | Safety level: `critical`, `high`, or `strict`. Invalid values fall back to `high`. |
| Level | What's blocked |
|-------|----------------|
| `critical` | Nothing from this hook (defer entirely to `block-dangerous-commands`) |
| `high` | Branch-aware guardrails, protected-branch deletion, pushes to main/master by name, destructive gh CLI operations |
| `strict` | All of `high` plus any force-push, so the hook is self-sufficient standalone |
The `SAFETY_LEVEL` constant in the script still exists, but prefer the env var: plugin updates overwrite installed files, so edits don't survive. This hook always denies; it has no ask mode.
## Data & privacy
Blocked commands are logged (with timestamp, pattern id, session id, cwd, and permission mode) to `~/.claude/hooks-logs/<date>.jsonl`. The only command it runs is `git branch --show-current`, locally; it makes no network calls.
## Uninstall
```
/plugin uninstall git-safety@claude-code-hooks
```
Part of [claude-code-hooks](https://github.com/karanb192/claude-code-hooks).
© Karan Bansal · MIT · karanb192/claude-code-hooks