---
name: git-safe
version: "1.0.0"
category: tools
description: "当需要任何 git 操作时必须使用此技能：commit、branch、merge、rebase、stash。防止危险错误。Use when ANY git operation is needed - commit, branch, merge, rebase, stash. This skill MUST be used for ALL git operations to prevent dangerous mistakes."
triggers:
  zh: ["git", "commit", "branch", "merge", "rebase", "stash", "提交", "分支", "合并", "冲突解决", "cherry-pick"]
  en: ["git", "commit", "branch", "merge", "rebase", "stash", "cherry-pick"]
license: MIT
compatibility: Node.js >= 14
config: 
  - config/git.yaml
dependencies: 
  - None
metadata: 
author: "sunhongda20204834@example.com"
created: "2026-06-16"
updated: "2026-06-16"
status: "stable"
---

# Git安全操作 / Git Safe

## Changelog / 版本履历
<!-- Track all changes to this skill. Update this table whenever you modify the skill. -->
| 日期 | 版本 | 变更摘要 |
|------|------|---------|
| 2026-06-29 | 2.0.0 | 集成 .opop/ 多平台凭证管理 + git_remote_list/add/push_all + git_credential_set 工具 |
| 2026-06-16 | 1.0.0 | 规范化调整 |

***

## Core Concept / 核心概念

### 🇨🇳
一句话说清：输入什么 → 做什么 → 输出什么。明确 **不做什么**。

**输入**: Git 操作请求（commit, branch, merge, rebase, stash 等） | **输出**: 安全执行结果（JSON 格式，包含操作状态、结果和错误信息） | **不负责**: 直接执行 git 命令或绕过安全检查

### 🇺🇸
One line: Input → Process → Output. Explicitly state what is **NOT done**.

**Input**: Git operation requests (commit, branch, merge, rebase, stash, etc.) | **Output**: Safe execution results (JSON format with operation status, results, and error messages) | **NOT responsible for**: Directly executing git commands or bypassing safety checks

***

## Position / 定位

```
上游 Skills → tools 路由器 → git-safe → 安全 JS 脚本 → git 命令
    (easy/auto)     (输入来源)        (输出流向)      (最终执行)
```

***

## Iron Law / 核心铁律

### 🇨🇳
1. **铁律1**: 绝不直接执行 git 命令。所有 git 操作必须通过安全的 JS 脚本进行。违规示例：❌ 直接使用 bash 工具执行 `git commit`。合规示例：✅ 通过 git-safe.js 脚本执行 commit 操作。
2. **铁律2**: 危险操作必须要求用户确认。任何可能造成数据丢失或历史重写的操作都需要显式用户批准。违规示例：❌ 自动执行 force push 而不询问用户。合规示例：✅ 检测到 force push 尝试时暂停并请求用户确认。
3. **铁律3**: 严禁强制推送或硬重置。这些操作默认被禁止，除非在特殊情况下经过多重确认。违规示例：❌ 执行 `git push --force` 或 `git reset --hard`。合规示例：✅ 使用安全的替代方案或完全阻止此类操作。

### 🇺🇸
1. **Law 1**: NEVER execute git commands directly. All git operations MUST go through safe JS scripts. Violation: ❌ Using bash tool to directly execute `git commit`. Compliance: ✅ Executing commit operations through git-safe.js script.
2. **Law 2**: Dangerous operations REQUIRE explicit user confirmation. Any operation that could cause data loss or history rewriting must have explicit user approval. Violation: ❌ Automatically executing force push without asking the user. Compliance: ✅ Pausing and requesting user confirmation when force push is detected.
3. **Law 3**: NEVER allow force push or hard reset by default. These operations are prohibited unless explicitly confirmed through multiple safety checks in special circumstances. Violation: ❌ Executing `git push --force` or `git reset --hard`. Compliance: ✅ Using safe alternatives or completely blocking such operations.

***

## Multi-Platform Git & .opop Integration / 多平台 Git 与 .opop 集成 (NEW v2.0)

### .opop/ Workspace / 工作空间

项目根目录下的 `.opop/` 是插件自管工作空间，用于存储凭证、状态和缓存：

```
.opop/
├── .gitignore           # 自动忽略凭证文件
├── config.json           # 插件工作空间配置
├── credentials/
│   └── tokens.json       # 加密存储的 git/npm 凭证
├── state/                # Agent/Hook 持久化状态
└── workspace/            # 临时文件和缓存
```

### Credential Management / 凭证管理

一次存储，到处使用。支持的平台和凭证类型：

| 平台 | 凭证类型 | 工具 |
|------|---------|------|
| GitHub / Gitee / CodeUp(云效) / GitLab | token / password / SSH | `git_credential_set` |
| npm | token | `git_credential_set` (type=npm) |

```bash
# 存储凭证
git_credential_set platform="github" type="token" label="my-github" value="ghp_xxx"
git_credential_set platform="codeup" type="token" label="codeup" value="xxx"
git_credential_set platform="other" type="npm" label="npm" value="npm_xxx"

# 查看 / 删除
git_credential_list
git_credential_delete credential_id="cred_xxx"
```

### Multi-Remote Operations / 多远程操作

```bash
git_remote_list                                           # 查看所有远程
git_remote_add name="gitee" url="https://gitee.com/..."   # 添加远程
git_push_all                                              # 一键推送到所有远程
```

### Iron Law 4 / 铁律 4 (NEW)
4. **铁律4**: 凭证存储在 `.opop/credentials/`，绝不硬编码在代码或配置文件中。使用 `git_credential_set` 工具存储，`git_credential_list` 查看。

## Rationalization Table / 合理化防御表
<!-- REQUIRED for quality/workflow/tools domains. Prevents model rationalization. -->

| # | Trap / 陷阱 | Question / 请问自己 | Action / 应该怎么做 |
|---|-------------|------------------|------------------|
| 1 | "这只是一个小提交，我可以直接执行 git 命令" | 这个操作是否真的安全？是否有潜在的破坏性后果？ | 必须通过 git-safe 脚本执行，即使是小提交也可能有意外后果 |
| 2 | "我知道我在做什么，可以跳过安全检查" | 安全检查的存在是为了防止什么类型的错误？我是否100%确定不会犯错？ | 永远不要跳过安全检查，即使是有经验的开发者也会犯错 |
| 3 | "用户很着急，直接执行会更快" | 速度重要还是数据安全重要？如果出错需要多少时间来修复？ | 安全永远优先于速度，花几秒钟确认比花几小时修复错误更值得 |

***

## Red Flags / 三层防御

### Layer 1: Input / 输入
- **INPUT-01**: 未检测到 git 仓库 → 🔴 CRITICAL → 中断操作并提示用户初始化仓库
- **INPUT-02**: 工作树有未提交的更改 → 🟡 WARN → 继续但标记状态，提醒用户注意潜在冲突

### Layer 2: Execution / 执行
- **EXEC-01**: 尝试强制推送 → 🔴 CRITICAL → 中断操作并要求用户确认
- **EXEC-02**: 尝试硬重置 → 🔴 CRITICAL → 中断操作并要求用户确认  
- **EXEC-03**: diff 中包含敏感数据（API 密钥、密码等）→ 🔴 CRITICAL → 中断操作并警告用户

### Layer 3: Output / 输出
- **OUTPUT-01**: 提交信息不符合 Conventional Commits 格式 → 🟡 WARN → 继续但建议用户使用标准格式

**级别标识**: 🔴 CRITICAL → 中断 | 🟡 WARN → 继续+标记 | 🔵 INFO → 记录

***

## Description / 描述
<!-- One paragraph describing what this skill does, when to use it, and what makes it unique. Keep it concise and bilingual. -->
Use when ANY git operation is needed - commit, branch, merge, rebase, stash. This skill MUST be used for ALL git operations to prevent dangerous mistakes. When a user requests any git-related action (including but not limited to: commit, branch creation/deletion, merging, rebasing, stashing, cherry-picking, status checking, log viewing), this skill intercepts the request and routes it through safe JavaScript wrapper scripts that perform pre-validation and post-verification. This ensures that potentially destructive operations like force pushes, hard resets, or history rewriting are either prevented entirely or require explicit user confirmation. / 当需要任何 git 操作时必须使用此技能。当用户请求任何 git 相关操作时（包括但不限于：提交、分支创建/删除、合并、变基、暂存、cherry-pick、状态检查、日志查看），此技能会拦截请求并将其路由到安全的 JavaScript 包装脚本，这些脚本执行预验证和后验证。这确保了潜在的破坏性操作（如强制推送、硬重置或历史重写）要么被完全阻止，要么需要用户明确确认。

## Triggers / 触发词
<!-- List keywords that should trigger this skill. These are used by routing systems like 'easy' to auto-select skills. -->
- English: 'git', 'commit', 'branch', 'merge', 'rebase', 'stash', 'cherry-pick'
- 中文: 'git', '提交', '分支', '合并', '冲突解决', 'cherry-pick'

## Capabilities / 能力
<!-- List specific capabilities this skill provides. Each item should be a concrete ability with brief explanation. -->
- Safe git operations - Routes all git commands through secure JS scripts with validation
- Conventional Commits enforcement - Validates commit messages against standard format
- Dangerous operation confirmation - Requires explicit user approval for destructive commands
- Working tree status checks - Verifies repository state before operations
- **Multi-platform credentials** (NEW) - Stores GitHub/Gitee/CodeUp/GitLab/npm tokens in `.opop/credentials/`
- **Multi-remote push** (NEW) - `git_push_all` pushes to all remotes simultaneously
- JSON result output - Returns structured results for LLM consumption

## Dependencies / 依赖
<!-- List other skills, config files, or resources this skill depends on. Explain why each dependency is needed. -->
- None - This is a standalone tool skill with no external dependencies

## Usage / 使用方式

### Invocation / 调用方式
<!-- Explain how this skill is typically invoked - via easy auto-routing, auto orchestration, expert direct call, or domain router. -->
This skill is typically invoked via the tools domain router when git-related keywords are detected in user requests. It can also be called directly using the expert entry point.

### Parameters / 参数
<!-- Document all parameters this skill accepts. Use the table format below. -->
| 参数 | 类型 | 必填 | 说明 |
|------|------|------|------|
| command | string | ✅ | Git operation to perform (status, commit, branch, merge, etc.) |
| args | array | ❌ | Additional arguments specific to the command |

### Example / 示例
<!-- Show concrete examples of how to invoke this skill. Use code blocks for commands. -->
```
/tools git status
/tools git commit -m "feat(auth): add login timeout handling" -a
```

## Output Format / 输出格式
<!-- Describe what this skill produces - JSON, Markdown report, code files, etc. Be specific about structure and content. -->
The skill produces JSON-formatted output containing operation status, results, and any error messages. For status operations, it returns detailed repository state information. For commit operations, it returns commit hash and verification results.

## Configuration / 配置
<!-- If this skill reads any YAML config files, document them here with their structure and purpose. -->
This skill can optionally read from config/git.yaml for repository-specific settings, but operates independently without configuration.

## Workflow / 工作流程
<!-- Detail the step-by-step workflow this skill follows. Number each step clearly. -->
1. Detect git operation request - Identify the specific git command needed
2. Validate operation safety - Check if the operation requires special confirmation
3. Execute via safe script - Run the operation through the git-safe.js wrapper
4. Verify and return results - Ensure operation completed successfully and return structured output

## Auto-Review / 自检清单
<!-- Checklist that should be automatically verified after skill execution. This helps ensure quality and completeness. -->
| # | 检查项 |
|---|--------|
| 1 | LLM did not execute git commands directly |
| 2 | All operations went through git-safe.js script |
| 3 | Dangerous operations required user confirmation |
| 4 | Commit messages followed Conventional Commits format |
| 5 | Working tree status was checked before operations |
| 6 | Results were returned in proper JSON format |
