Skip to main content
plugin

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

Install into Claude Code · Plugin marketplace All methods and platforms ↓
/plugin marketplace add karanb192/claude-code-hooks
/plugin install git-safety@claude-code-hooks
Open on GitHub

What 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-hooks

Step 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

Similar plugins

All plugins for Git and GitHub →

From the same set28

The whole set →
subagent-spawn-cap subagent-spawn-cap counts subagent spawns per session: asks at 20, asks again every 10, refuses at 60. Puts back the cap Claude dropped in 2.1.224. ★ 586repo stars config-guard config-guard — a PreToolUse hook that stops the agent from rewriting the files holding its own guardrails: settings, hook scripts, plugin manifests. Reads pass. ★ 586repo stars case-insensitive-guard case-insensitive-guard is a Claude Code plugin that blocks destructive Bash commands aimed at a path with a different letter case. It checks commands such as rm -rf before they run. ★ 586repo stars protect-tests protect-tests blocks the "fake green" trap: stops the agent from deleting, renaming, or skipping tests instead of fixing the code. Works with pytest, jest, Go, JUnit, RSpec, and more. ★ 586repo stars protect-secrets protect-secrets — a PreToolUse hook that checks every Read, Edit, Write, Bash and Grep call and blocks .env, SSH keys or cloud credentials — or asks you first. ★ 586repo stars bounty-board bounty-board turns TODO and FIXME comments into bounties that grow in XP value as they age. Scans your repo, prices each debt marker by git-blame age, and feeds the top 3 into your session as side quests. ★ 586repo stars dead-end-registry dead-end-registry stores reverted code changes and alerts you before retrying failed ideas. Keeps track of discarded approaches and detour token costs per repository. ★ 586repo stars standup-autopilot The standup-autopilot plugin turns agent session transcripts into a daily standup card with tasks, tests, PRs, and blockers. Open blockers carry into the next session. ★ 586repo stars

Project sponsors

Companies that keep this analytics open