# Claude Code 核心速查

Source: https://codewiki.com/zh/cheatsheets/claude-code/

## 启动与诊断

- `claude` — 在当前工作目录启动交互式会话
- `claude 'explain this repository'` — 带初始提示词启动交互式会话
- `claude -p 'summarize src/auth/'` — 输出一次结果后退出
- `cat test.log | claude -p 'explain the failure'` — 把标准输入连同提示词交给非交互模式
- `claude --version` — 输出已安装的 Claude Code 版本
- `claude doctor` — 检查安装状态并报告配置错误

## 限定任务

- `Read src/auth/; do not edit yet; map the request flow.` — 先读取指定区域，不执行写入
- `Plan this change; list files, risks, and verification commands.` — 要求方案列出文件、风险和验证命令
- `Implement issue #123; change only src/auth/ and tests/auth/.` — 说明交付目标和可写路径
- `Preserve the public API; ask before changing the contract.` — 把接口变更放到明确的审批关口之后
- `Stop if credentials, network access, or a migration is required.` — 需要扩大能力时先暂停并说明
- `Finish only after tests pass; report commands and exit codes.` — 用可复现证据定义完成条件

## 项目指令

- `/init` — 生成或完善项目级起始指令
- `CLAUDE.md` — 在项目根目录保存共享指令
- `~/.claude/CLAUDE.md` — 保存适用于所有项目的个人指令
- `CLAUDE.local.md` — 保存不提交到 Git 的本地项目指令
- `.claude/rules/testing.md` — 保存按路径或主题拆分的项目规则
- `@docs/testing.md` — 在 `CLAUDE.md` 中导入测试说明
- `@AGENTS.md` — 在 `CLAUDE.md` 中复用跨智能体指令

## 上下文

- `Explain @src/auth/session.ts` — 把一个文件的完整内容加入提示词上下文
- `Map @src/auth/` — 只加入目录清单，不加载目录中所有文件
- `/add-dir ../shared` — 为当前会话增加可访问目录
- `/context` — 查看上下文窗口的占用来源
- `/compact keep the failing test output and current plan` — 按指定重点压缩会话并释放上下文
- `/clear completed-task` — 开始空上下文的新会话，并命名上一会话
- `/rewind` — 从检查点恢复代码或会话内容

## 权限

- `/permissions` — 查看并编辑 `allow`、`ask` 和 `deny` 规则
- `/plan fix the auth bug` — 进入计划模式并立即处理所给任务
- `claude --permission-mode plan` — 以只读的计划模式启动会话
- `claude --permission-mode dontAsk --allowedTools 'Read,Grep,Glob'` — 仅自动批准列出的读取工具，其余请求直接拒绝
- `claude --disallowedTools 'Read(./.env)' 'Read(./secrets/**)'` — 禁止读取两个敏感路径
- `claude --restricted -p 'inspect this repository'` — 以受限工具和配置启动一次非交互检查

## 会话与隔离

- `/rename auth-refactor` — 重命名当前会话，便于后续查找
- `claude -c` — 继续当前目录最近一次会话
- `claude -r auth-refactor 'finish the tests'` — 按名称恢复会话并继续任务
- `claude -r auth-refactor --fork-session` — 复制已恢复会话，并生成新的会话 ID
- `/branch alternate-fix` — 分支当前对话，不创建 Git 分支
- `claude -w auth-refactor` — 为新会话创建隔离的 Git worktree

## 模型与推理强度

- `/model` — 打开模型选择器
- `/model sonnet` — 切换模型，并保存为新会话的默认值
- `/effort high` — 设置当前会话的推理强度
- `/effort status` — 输出当前推理强度
- `claude --model sonnet --effort high` — 仅为本次启动选择模型和推理强度

## 审查与验证

- `/diff` — 在交互界面查看未提交和逐轮差异
- `/code-review high` — 以 `high` 强度审查当前差异的正确性
- `/code-review high 123` — 以 `high` 强度审查 PR 123
- `/security-review` — 对当前分支相对 `origin` 默认分支的差异做安全审查
- `/verify` — 构建并运行应用，以行为证据验证修改
- `Compare the diff with the acceptance criteria; cite each gap by file and line.` — 逐项对照验收标准，并用文件与行号指出缺口

## 规格与测试

- `Restate the acceptance criteria as observable behavior before editing.` — 编辑前把验收标准改写为可观察行为
- `Before refactoring, add characterization tests for current behavior.` — 重构前用特征测试固定现有行为
- `Reproduce the bug with a failing test before changing production code.` — 修改生产代码前先用失败测试复现问题
- `Keep existing tests; add boundary, error, and cancellation cases.` — 保留现有测试，并补充边界、错误与取消场景
- `Run the narrowest relevant test first, then the full required suite.` — 先运行最小相关测试，再运行完整必需套件
- `Do not weaken assertions or remove tests solely to make the suite pass.` — 不要只为让套件通过而削弱断言或删除测试
- `If behavior changes, update the API contract and tests together.` — 行为变化时同步更新 API 契约与测试

## 非交互自动化

- `claude -p 'review the current diff' --output-format text` — 输出纯文本结果
- `claude -p 'summarize the diff' --output-format json` — 输出包含最终结果的单个 JSON 对象
- `claude -p 'review the diff' --output-format stream-json --verbose` — 输出流式 JSON 事件
- `claude -p 'return a summary' --json-schema '{"type":"object","properties":{"summary":{"type":"string"}},"required":["summary"]}'` — 按 JSON Schema 验证最终对象
- `claude -p 'review the diff' --max-budget-usd 1` — 把 API 调用预算上限设为 1 美元
- `claude -p 'review the diff' --max-turns 8` — 未完成时在 8 个智能体轮次后报错停止
- `claude -p 'one-shot check' --no-session-persistence` — 不保存可恢复的会话

## MCP 工具

- `claude mcp add --transport http sentry https://mcp.sentry.dev/mcp` — 以本地作用域添加 HTTP 服务器配置
- `claude mcp list` — 列出 MCP 服务器及其连接状态
- `claude mcp get sentry` — 查看指定 MCP 服务器的配置与状态
- `claude mcp login sentry` — 为指定 HTTP 服务器启动 OAuth 登录
- `/mcp` — 在会话中查看服务器状态和认证
- `claude mcp remove sentry` — 删除本地作用域中的服务器配置

## 子智能体与技能

- `.claude/agents/code-reviewer.md` — 保存项目级子智能体定义
- `claude --agent code-reviewer` — 让整个会话使用已配置的子智能体
- `/subtask inspect the parser for race conditions` — 分出继承当前对话的后台子智能体
- `/tasks` — 查看并管理当前会话的后台任务
- `.claude/skills/fix-issue/SKILL.md` — 保存项目级可复用技能
- `/skills` — 列出已发现的技能并管理其可见性
- `/fix-issue 123` — 调用技能，并把 `123` 作为参数传入
