Chuyển đến nội dung
Skillpedia
Triển khai và đám mây (DevOps)310k · #120

git-guardrails-claude-code

Skill này làm gì

Skill thêm một kiểm tra an toàn vào Claude Code để ngăn một số lệnh git chạy. Dành cho người muốn tránh thiệt hại ngoài ý muốn cho kho mã của mình.

Khi nào nên dùng

  • ✓Khi bạn muốn ngăn agent push mã vô tình.
  • ✓Khi bạn muốn chặn các lệnh phá hủy như git reset --hard.
  • ✓Khi bạn muốn các quy tắc an toàn này áp dụng cho một dự án hoặc cho tất cả dự án của bạn.

Liên kết skill

https://github.com/mattpocock/skills/blob/main/skills/misc/git-guardrails-claude-code/SKILL.md

Sao chép rồi dán vào AI của bạn (Claude, ChatGPT hoặc Cursor) và nhờ nó làm theo hướng dẫn.

Cách dùng

Cách dễ nhất: sao chép tin nhắn và dán vào AI của bạn. AI sẽ đọc skill và làm theo hướng dẫn.

Tin nhắn có sẵn để dán

Hãy dùng skill này để giúp tôi: https://github.com/mattpocock/skills/blob/main/skills/misc/git-guardrails-claude-code/SKILL.md Đọc tệp SKILL.md tại liên kết đó và làm theo hướng dẫn cho việc của tôi.
Bạn muốn AI tự tìm những skill như thế này?Cài MCP Skillpedia miễn phí: chỉ một dòng, và AI sẽ tìm trong 15.000+ skill trước mỗi việc.Cài đặt trong 1 phút →

Ví dụ cách nhờ AI

“Thiết lập các hook an toàn để Claude Code không thể push hoặc reset repo git của tôi một cách vô tình.”

5 skill mới mỗi tuần. Đọc chỉ 1 phút.

Cách hoạt động

Agent sao chép một script hook vào dự án hoặc thư mục cài đặt home của bạn, rồi liên kết nó trong tệp cài đặt. Khi có lệnh bị chặn được thử, hook dừng nó và báo cho agent biết nó không có thẩm quyền chạy lệnh đó.

Nội dung kỹ thuật (SKILL.md gốc)

Lấy nguyên văn từ GitHub, do tác giả viết bằng tiếng Anh.

Setup Git Guardrails

Sets up a PreToolUse hook that intercepts and blocks dangerous git commands before Claude executes them.

What Gets Blocked

  • git push (all variants including --force)
  • git reset --hard
  • git clean -f / git clean -fd
  • git branch -D
  • git checkout . / git restore .

When blocked, Claude sees a message telling it that it does not have authority to access these commands.

Steps

1. Ask scope

Ask the user: install for this project only (.claude/settings.json) or all projects (~/.claude/settings.json)?

2. Copy the hook script

The bundled script is at: scripts/block-dangerous-git.sh

Copy it to the target location based on scope:

  • Project: .claude/hooks/block-dangerous-git.sh
  • Global: ~/.claude/hooks/block-dangerous-git.sh

Make it executable with chmod +x.

3. Add hook to settings

Add to the appropriate settings file:

Project (.claude/settings.json):

{
  "hooks": {
    "PreToolUse": [
      {
        "matcher": "Bash",
        "hooks": [
          {
            "type": "command",
            "command": "\"$CLAUDE_PROJECT_DIR\"/.claude/hooks/block-dangerous-git.sh"
          }
        ]
      }
    ]
  }
}

Global (~/.claude/settings.json):

{
  "hooks": {
    "PreToolUse": [
      {
        "matcher": "Bash",
        "hooks": [
          {
            "type": "command",
            "command": "~/.claude/hooks/block-dangerous-git.sh"
          }
        ]
      }
    ]
  }
}

If the settings file already exists, merge the hook into the existing hooks.PreToolUse array. Don't overwrite other settings.

4. Ask about customization

Ask if user wants to add or remove any patterns from the blocked list. Edit the copied script accordingly.

5. Verify

Run a quick test:

echo '{"tool_input":{"command":"git push origin main"}}' | <path-to-script>

Should exit with code 2 and print a BLOCKED message to stderr.

Câu hỏi thường gặp

Những lệnh git nào bị chặn?

Mọi kiểu push, git reset --hard, git clean -f, git branch -D, và checkout hoặc restore áp dụng cho mọi tệp.

Tôi có chọn được nơi các quy tắc áp dụng không?

Có. Skill hỏi bạn muốn cài đặt chúng cho riêng dự án này hay cho tất cả dự án.

Điều gì xảy ra khi một lệnh bị chặn?

Claude thấy một thông báo nói rằng nó không có thẩm quyền truy cập lệnh đó.

Chi tiết

Mô tả gốc của tác giả

Set up Claude Code hooks to block dangerous git commands (push, reset --hard, clean, branch -D, etc.) before they execute. Use when user wants to prevent destructive git operations, add git safety hooks, or block git push/reset in Claude Code.

repo: mattpocock/skills

Độ phổ biến

Sao GitHub
310k
Vị trí xếp hạng
#120
Giấy phép
MIT
Cập nhật
1/11/2026

Skill này có hữu ích không?

0

Đăng nhập để bình chọn.

Bình luận

  • …

Thêm skill từ kho mã này

Skill tương tự