# 通用工作流

> ## 文档 索引
> 获取 the 完成 文档 索引 at: https://代码.claude.com/docs/llms.txt
> 使用 this 文件 to disc结束 所有 可用 页s 之前探索 further.

# 通用工作流

>步骤-by-步骤 指南s for探索 代码基础s,固定 缺陷s, re事实oring, 测试, and other 日常 任务s with Claude 代码.

This 页 c结束s 实践 工作流s for 日常 开发:探索 unfamiliar 代码, 调试ging, re事实oring,写作 测试s,创建 PRs, and管理 会话s. 每个 节 includes 示例 及时s you can adapt to your own 项目s. For 高er-级别 模式s and 提示,看见 [最佳实践](/en/最佳-实践).

## Understand 新 代码基础s

### Get a 快 代码基础 概述

S上pose you've 公正 连接 a 新 项目 and 需要 to understand its 结构 快ly.

  
    ```bash  主题={空}
    cd /路径/to/项目 
    ```
  

  
    ```bash  主题={空}
    claude 
    ```
  

  
    ```文本  主题={空}
    give me an 概述 of this 代码基础
    ```
  

  
    ```文本  主题={空}
    ex简单 the 主 架构 模式s 使用d here
    ```

    ```文本  主题={空}
    what are the 键 数据 模型s?
    ```

    ```文本  主题={空}
    how is 认证 已处理?
    ```
  

  提示:

  * 启动 with broad 问题s, then nar行 下 to 特定 面积s
  * Ask about coding 惯例 and 模式s 使用d in the 项目
  * 请求 a 词汇表 of 项目-特定 条款

### 查找 相关 代码

S上pose you 需要 to locate 代码 r兴高采烈 to a 特定 feature or 函数式ity.

  
    ```文本  主题={空}
    查找 the 文件 that handle 用户 认证
    ```
  

  
    ```文本  主题={空}
    how do these 认证 文件 工作 together?
    ```
  

  
    ```文本  主题={空}
    跟踪 the 日志in 流程 from front-结束 to 数据库
    ```
  

  提示:

  * Be 特定 about what you're 看 for
  * 使用 do主 language from the 项目
  * 安装 a [代码 intelligence 插件](/en/disc结束-插件s#代码-intelligence) for your language to give Claude 精确 "前往 to 定义" and "查找 参考文献" navigation

***

## 修复 缺陷s 高效ly

S上pose you've encountered an 错误 消息 and 需要 to 查找 and 修复 its 来源.

  
    ```文本  主题={空}
    I'm看见 an 错误 when I 运行 npm 测试
    ```
  

  
    ```文本  主题={空}
    suggest a 几个 方式s to 修复 the @ts-ignore in 用户.ts
    ```
  

  
    ```文本  主题={空}
    更新 用户.ts to 添加 the 空 检查 you 建议
    ```
  

  提示:

  * Tell Claude the 命令 to reproduce the 问题 and get a 栈 跟踪
  * Mention 任何步骤s to reproduce the 错误
  * Let Claude know if the 错误 is intermittent or 一致

***

## Re事实or 代码

S上pose you 需要 to 更新 旧 代码 to 使用 现代 模式s and 实践.

  
    ```文本  主题={空}
    查找 deprecated API 使用方法 in our 代码基础
    ```
  

  
    ```文本  主题={空}
    suggest how to re事实or utils.js to 使用 现代 Java脚本 features
    ```
  

  
    ```文本  主题={空}
    re事实or utils.js to 使用 ES2024 features while维护 the 相同 behavior
    ```
  

  
    ```文本  主题={空}
    运行 测试s for the re事实ored 代码
    ```
  

  提示:

  * Ask Claude to ex简单 the 益处s of the 现代 方法
  * 请求 that 更改s 主tain 落后 兼容性 when 需要ed
  * Do re事实oring in 小, 可测试 increments

***

## 使用 专业 子代理

S上pose you 想要 to 使用 专业 AI 子代理 to handle 特定 任务s 更多 有效ly.

  
    ```文本  主题={空}
    /代理s
    ```

    This 显示s 所有 可用 子代理 and lets you 创建 新 ones.
  

  
    Claude 代码 自动所有y delegates 恰当 任务s to 专业 子代理:

    ```文本  主题={空}
    re视图 my 最近 代码 更改s for 安全 问题s
    ```

    ```文本  主题={空}
    运行 所有 测试s and 修复 任何 失败s
    ```
  

  
    ```文本  主题={空}
    使用 the 代码-审查员 sub代理 to 检查 the auth 模块
    ```

    ```文本  主题={空}
    have the 调试ger sub代理调查 why 用户s can't 日志 in
    ```
  

  
    ```文本  主题={空}
    /代理s
    ```

    Then 选择 "创建 新 sub代理" and fol低 the 及时s to de好:

    * A 唯一 标识符 that describes the sub代理's 目的 (for 示例, `代码-审查员`, `api-设计师`).
    * When Claude should 使用 this 代理
    * Which 工具 it can access
    * A 系统 及时描述 the 代理's 角色 and behavior
  

  提示:

  * 创建 项目-特定 子代理 in `.claude/代理s/` for 团队分享
  * 使用 de脚本ive `描述` 字段s to 启用 自动 delegation
  * 限制 工具 access to what 每个 sub代理 实际ly 需要s
  * 检查 the [子代理 文档](/en/sub-代理s) for 详情ed 示例

***

## 使用 计划 模式 for 安全 代码 分析

计划 模式 instructs Claude to 创建 a 计划 by分析 the 代码基础 with 读取-only 运营, 完美 for探索 代码基础s, 规划 复杂 更改s, or回顾 代码 安全ly. In 计划 模式, Claude 使用s [`Ask用户问题`](/en/工具-参考) to gather 要求 and clarify your 目标s 之前建议 a 计划.

### When to 使用 计划 模式

* **Multi-步骤 实施**: When your feature requ怒s制造 编辑s to 许多 文件
* **代码 探索**: When you 想要 to研究 the 代码基础 tho粗糙ly 之前改变 任何薄g
* **交互 开发**: When you 想要 to ite速率 on the 指导 with Claude

### How to 使用 计划 模式

**Turn on 计划 模式 期间 a 会话**

You can switch into 计划 模式 期间 a 会话 using **Shift+Tab** to cycle th粗糙 许可 模式s.

If you are in 正常 模式, **Shift+Tab** 第一个 switches into Auto-接受 模式, indicated by `⏵⏵ 接受 编辑s on` at the 底部 of the 终端. A subsequent **Shift+Tab** will switch into 计划 模式, indicated by `⏸ 计划 模式 on`.

**启动 a 新 会话 in 计划 模式**

To 启动 a 新 会话 in 计划 模式, 使用 the `--许可-模式 计划` flag:

```bash  主题={空}
claude --许可-模式 计划
```

**运行 "负责人较少" queries in 计划 模式**

You can also 运行 a 查询 in 计划 模式 直接ly with `-p` (that is, in ["负责人较少 模式"](/en/负责人较少)):

```bash  主题={空}
claude --许可-模式 计划 -p "分析 the 认证 系统 and suggest 改善s"
```

### 示例: 规划 a 复杂 re事实or

```bash  主题={空}
claude --许可-模式 计划
```

```文本  主题={空}
I 需要 to re事实or our 认证 系统 to 使用 OAuth2. 创建 a 详情ed mig比率n 计划.
```

Claude分析s the 当前 实施 and 创建 a comprehensive 计划. Re好 with fol低-上s:

```文本  主题={空}
What about 落后 兼容性?
```

```文本  主题={空}
How should we handle 数据库 mig比率n?
```

Press `Ctrl+G` to 打开 the 计划 in your 默认 文本 编辑or, where you can 编辑 it 直接ly 之前 Claude proceeds.

When you 接受 a 计划, Claude 自动所有y 名称s the 会话 from the 计划 满意. The 名称 应用ears on the 及时 bar and in the 会话 picker. If you've al就绪 设置 a 名称 with `--名称` or `/重命名`, 接受ing a 计划 获胜't 结束写入 it.

### Con图 计划 模式 as 默认

```json  主题={空}
// .claude/设置.json
{
  "权限": {
    "默认模式": "计划"
  }
}
```

看见 [设置 文档](/en/设置#可用-设置) for 更多 配置 选项.

***

## 工作 with 测试s

S上pose you 需要 to 添加 测试s for 揭开 代码.

  
    ```文本  主题={空}
    查找 功能s in 通知s服务.快速 that are not c结束ed by 测试s
    ```
  

  
    ```文本  主题={空}
    添加 测试s for the 通知 服务
    ```
  

  
    ```文本  主题={空}
    添加 测试 案例s for 边 条件 in the 通知 服务
    ```
  

  
    ```文本  主题={空}
    运行 the 新 测试s and 修复 任何 失败s
    ```
  

Claude can gene速率 测试s that fol低 your 项目's 现有 模式s and 惯例. When询问 for 测试s, be 特定 about what behavior you 想要 to 验证. Claude检查s your 现有 测试 文件 to 匹配 the style, 框架s, and 断言ion 模式s al就绪 in 使用.

For comprehensive cove狂怒, ask Claude to identify 边 案例s you might have missed. Claude can分析 your 代码 路径s and suggest 测试s for 错误 条件, boundary 值s, and 意外 输入s that are 容易 to 结束看.

***

## 创建 拉取请求s

You can 创建 拉取请求s by询问 Claude 直接ly ("创建 a pr for my 更改s"), or 指南 Claude th粗糙 it步骤-by-步骤:

  
    ```文本  主题={空}
    总和marize the 更改s I've made to the 认证 模块
    ```
  

  
    ```文本  主题={空}
    创建 a pr
    ```
  

  
    ```文本  主题={空}
    enhance the PR 描述 with 更多 上下文 about the 安全 改善s
    ```
  

When you 创建 a PR using `gh pr 创建`, the 会话 is 自动所有y 链接 to that PR. You can 恢复 it 更晚 with `claude --from-pr `.

  Re视图 Claude's gene速率d PR 之前提交 and ask Claude to 高轻 潜力 风险s or conside比率ns.

## Handle 文档

S上pose you 需要 to 添加 or 更新 文档 for your 代码.

  
    ```文本  主题={空}
    查找 功能s without 适当 JSDoc comments in the auth 模块
    ```
  

  
    ```文本  主题={空}
    添加 JSDoc comments to the 撤销cumented 功能s in auth.js
    ```
  

  
    ```文本  主题={空}
    improve the gene速率d 文档 with 更多 上下文 and 示例
    ```
  

  
    ```文本  主题={空}
    检查 if the 文档 fol低s our 项目 标准
    ```
  

  提示:

  * 规格ify the 文档 style you 想要 (JSDoc, doc字符串s, etc.)
  * Ask for 示例 in the 文档
  * 请求 文档 for 公共 APIs, 接口s, and 复杂 日志ic

***

## 工作 with 图像s

S上pose you 需要 to 工作 with 图像s in your 代码基础, and you 想要 Claude's 帮助分析 图像 满意.

  
    You can 使用 任何 of these 方法:

    1. Drag and 下降 an 图像 into the Claude 代码 风ow
    2. 复制 an 图像 and 粘贴 it into the CLI with ctrl+v (Do not 使用 cmd+v)
    3. Provide an 图像 路径 to Claude. E.g., "分析 this 图像: /路径/to/your/图像.png"
  

  
    ```文本  主题={空}
    What does this 图像 显示?
    ```

    ```文本  主题={空}
    Describe the UI 元素s in this 截图
    ```

    ```文本  主题={空}
    Are there 任何 问题atic 元素s in this 图表?
    ```
  

  
    ```文本  主题={空}
    Here's a 截图 of the 错误. What's causing it?
    ```

    ```文本  主题={空}
    This is our 当前 数据库 模式. How should we 修改 it for the 新 feature?
    ```
  

  
    ```文本  主题={空}
    Gene速率 CSS to 匹配 this 设计 模型
    ```

    ```文本  主题={空}
    What HTML 结构 would re创建 this 组件?
    ```
  

  提示:

  * 使用 图像s when 文本 描述s would be 不清楚 or 笨重
  * Include 截图s of 错误, UI 设计s, or 图表s for 更好 上下文
  * You can 工作 with mul提示le 图像s in a 对话
  * 图像 分析 工作s with 图表s, 截图s, 模型s, and 更多
  * When Claude 参考文献 图像s (for 示例, `[图像 #1]`), `Cmd+Click` (Mac) or `Ctrl+Click` (风ows/Linux) the 链接 to 打开 the 图像 in your 默认视图er

***

## 参考 文件 and 总监ies

使用 @ to 快ly include 文件 or 总监ies without 等待ing for Claude to 读取 them.

  
    ```文本  主题={空}
    Ex简单 the 日志ic in @src/utils/auth.js
    ```

    This includes the 满 满意 of the 文件 in the 对话.
  

  
    ```文本  主题={空}
    What's the 结构 of @src/组件s?
    ```

    This provides a 目录列表 with 文件 信息.
  

  
    ```文本  主题={空}
    显示 me the 数据 from @GitHub:repos/所有者/repo/问题s
    ```

    This 获取es 数据 from 连接 MCP 服务器 using the 格式 @服务器:资源.看见 [MCP 资源](/en/mcp#使用-mcp-资源) for 详情s.
  

  提示:

  * 文件 路径s can be relative or absolute
  * @ 文件 参考文献 添加 `CLAUDE.md` in the 文件's 目录 and parent 总监ies to 上下文
  * 目录 参考文献 显示 文件列表s, not 满意s
  * You can 参考 mul提示le 文件 in a single 消息 (for 示例, "@文件1.js and @文件2.js")

***

## 使用 延长思考 (薄king 模式)

[延长思考](https://平台.claude.com/docs/en/构建-with-claude/延长-薄king) is 启用 by 默认,给 Claude s步伐 to 原因 th粗糙 复杂 问题s步骤-by-步骤 之前 responding. This推理 is 可见 in 详细 模式, which you can toggle on with `Ctrl+O`.

添加ition所有y, Opus 4.6 and Sonnet 4.6 支持 适应推理: instead of a 固定思考 令牌 预算, the 模型 动态所有y 所有ocates思考 基础d on your [effort 级别](/en/模型-config#ad公正-effort-级别)设置. 延长思考 and 适应推理 工作 together to give you 控制 结束 how 深ly Claude 原因s 之前 responding.

延长思考 is 特定ly 有价值 for 复杂 架构师ural 决定s, 挑战 缺陷s, multi-步骤 实施 规划, and评估 贸易offs between 不同 方法es.

  pH值rases 像 "薄k", "薄k 硬", and "薄k 更多" are 解释ed as 常规 及时 说明 and don't 所有ocate思考 令牌s.

### Con图思考 模式

薄king is 启用 by 默认, but you can ad公正 or 禁用 it.

| 范围                    | How to con图                                                                     | 详情s                                                                                                                                                                                   |
| ------------------------ | ------------------------------------------------------------------------------------ | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| **Effort 级别**         | 运行 `/effort`, ad公正 in `/模型`, or 设置 [`CLAUDE_代码_EFFORT_级别`](/en/env-vars) | 控制思考 深度 for Opus 4.6 and Sonnet 4.6.看见 [Ad公正 effort 级别](/en/模型-config#ad公正-effort-级别)                                                                       |
| **`ultra薄k` 键词** | Include "ultra薄k" 任何where in your 及时                                         | 设置s effort to 高 for that turn on Opus 4.6 and Sonnet 4.6. 有用 for one-off 任务s要求 深推理 without 永久ly改变 your effort设置                          |
| **Toggle 短剪切**      | Press `选项+T` (macOS) or `Alt+T` (风ows/Linux)                                  | Toggle思考 on/off for the 当前 会话 (所有 模型s). May requ怒 [终端 配置](/en/终端-config) to 启用 选项 键 短剪切s                                     |
| **全球 默认**       | 使用 `/config` to toggle思考 模式                                                | 设置s your 默认 across 所有 项目s (所有 模型s).保存 as `al方式s薄king启用` in `~/.claude/设置.json`                                                                    |
| **限制 令牌 预算**   | 设置 [`MAX_薄KING_令牌S`](/en/env-vars) 环境 可变                       | 限制 the思考 预算 to a 特定 数字 of 令牌s. On Opus 4.6 and Sonnet 4.6, only `0` 应用lies un较少 适应推理 is 禁用. 示例: `出口 MAX_薄KING_令牌S=10000` |

To视图 Claude's思考 流程, press `Ctrl+O` to toggle 详细 模式 and看见 the 内部推理 显示ed as gray italic 文本.

### How 延长思考 工作s

延长思考 控制s how 很多 内部推理 Claude per形式s 之前 responding. 更多思考 provides 更多 s步伐 to探索 解决方案,分析 边 案例s, and self-正确 薄雾akes.

**With Opus 4.6 and Sonnet 4.6**,思考 使用s 适应推理: the 模型 动态所有y 所有ocates思考 令牌s 基础d on the [effort 级别](/en/模型-config#ad公正-effort-级别) you 选择. This is the 推荐 方式 to tune the 贸易off between 速度 and推理 深度.

**With 更旧 模型s**,思考 使用s a 固定 令牌 预算 drawn from your 输出 所有ocation. The 预算 varies by 模型;看见 [`MAX_薄KING_令牌S`](/en/env-vars) for per-模型 ceilings. You can 限制 the 预算 with that 环境 可变, or 禁用思考 整个ly via `/config` or the `选项+T`/`Alt+T` toggle.

On Opus 4.6 and Sonnet 4.6, [适应推理](/en/模型-config#ad公正-effort-级别) 控制s思考 深度, so `MAX_薄KING_令牌S` only 应用lies when 设置 to `0` to 禁用思考, or when `CLAUDE_代码_禁用_适应_薄KING=1` 撤销s these 模型s to the 固定 预算.看见 [环境变量](/en/env-vars).

  You're 收费 for 所有思考 令牌s 使用d 偶数 when思考 总和maries are redacted. In 交互 模式,思考 应用ears as a collapsed 桩 by 默认. 设置 `显示薄king总和maries: 真实` in `设置.json` to 显示 满 总和maries.

***

## 恢复 之前 对话s

When 启动ing Claude 代码, you can 恢复 a 之前 会话:

* `claude --继续` 继续s the 最多 最近 对话 in the 当前 目录
* `claude --恢复` 打开s a 对话 picker or 恢复s by 名称
* `claude --from-pr 123` 恢复s 会话s 链接 to a 特定 拉取请求

From 内部 an 活跃 会话, 使用 `/恢复` to switch to a 不同 对话.

会话s are 存储 per 项目 目录. The `/恢复` picker 显示s 交互 会话s from the 相同 Git 仓库,包括 工作树. 会话s 创建d by `claude -p` or SDK invocations do not 应用ear in the picker, but you can 静止 恢复 one by通过 its 会话 ID 直接ly to `claude --恢复 `.

### 名称 your 会话s

Give 会话s de脚本ive 名称s to 查找 them 更晚. This is a 最佳 实践 when工作 on mul提示le 任务s or features.

  
    名称 a 会话 at 启动上 with `-n`:

    ```bash  主题={空}
    claude -n auth-re事实or
    ```

    Or 使用 `/重命名` 期间 a 会话, which also 显示s the 名称 on the 及时 bar:

    ```文本  主题={空}
    /重命名 auth-re事实or
    ```

    You can also 重命名 任何 会话 from the picker: 运行 `/恢复`, 导航 to a 会话, and press `R`.
  

  
    From the 命令 行:

    ```bash  主题={空}
    claude --恢复 auth-re事实or
    ```

    Or from 内部 an 活跃 会话:

    ```文本  主题={空}
    /恢复 auth-re事实or
    ```
  

### 使用 the 会话 picker

The `/恢复` 命令 (or `claude --恢复` without 参数) 打开s an 交互 会话 picker with these features:

**键董事会 短剪切s in the picker:**

| 短剪切  | 行动                                            |
| :-------- | :------------------------------------------------ |
| `↑` / `↓` | 导航 between 会话s                         |
| `→` / `←` | Expand or collapse 分组 会话s               |
| `进入`   | 选择 and 恢复 the 高轻ed 会话         |
| `P`       | Pre视图 the 会话 满意                       |
| `R`       | 重命名 the 高轻ed 会话                    |
| `/`       | 搜索 to 过滤 会话s                         |
| `A`       | Toggle between 当前 目录 and 所有 项目s |
| `B`       | 过滤 to 会话s from your 当前 Git 分支   |
| `Esc`     | 退出 the picker or 搜索 模式                    |

**会话 组织:**

The picker 显示s 会话s with 有帮助 meta数据:

* 会话 名称 or initial 及时
* 时间 elapsed since 最后一个 活动
* 消息 count
* Git 分支 (if 应用lic能够)

分叉ed 会话s (创建d with `/分支`, `/re风`, or `--分叉-会话`) are 分组 together under their 根 会话,制造 it easier to 查找 r兴高采烈 对话s.

  提示:

  * **名称 会话s 早**: 使用 `/重命名` when 启动ing 工作 on a 不同 任务: it's 很多 easier to 查找 "payment-集成" than "ex简单 this 功能" 更晚
  * 使用 `--继续` for 快 access to your 最多 最近 对话 in the 当前 目录
  * 使用 `--恢复 会话-名称` when you know which 会话 you 需要
  * 使用 `--恢复` (without a 名称) when you 需要 to 浏览 and 选择
  * For 脚本s, 使用 `claude --继续 --print "及时"` to 恢复 in non-交互 模式
  * Press `P` in the picker to pre视图 a 会话 之前 re总和ing it
  * The 恢复d 对话 启动s with the 相同 模型 and 配置 as the 原始

  How it 工作s:

  1. **对话 Sto狂怒**: 所有 对话s are 自动所有y 保存 本地ly with their 满 消息 history
  2. **消息 De串行ization**: When re总和ing, the 整个 消息 history is 恢复 to 主tain 上下文
  3. **工具 州**: 工具 使用方法 and 结果s from the 之前 对话 are 保存
  4. **上下文 Resto比率n**: The 对话 恢复s with 所有 之前 上下文 完整

***

## 运行 平行 Claude 代码 会话s with Git 工作树

When工作 on mul提示le 任务s at once, you 需要 每个 Claude 会话 to have its own 复制 of the 代码基础 so 更改s don't collide. Git 工作树 solve this by创建 单独工作 总监ies that 每个 have their own 文件 and 分支, while分享 the 相同 仓库 history and 远程 连接s. This 手段 you can have Claude工作 on a feature in one 工作树 while固定 a 缺陷 in another, without either 会话 干涉 with the other.

使用 the `--工作树` (`-w`) flag to 创建 an iso晚d 工作树 and 启动 Claude in it. The 值 you pass becomes the 工作树 目录 名称 and 分支 名称:

```bash  主题={空}
# 启动 Claude in a 工作树 名称d "feature-auth"
# 创建s .claude/工作树/feature-auth/ with a 新 分支
claude --工作树 feature-auth

# 启动 another 会话 in a 单独 工作树
claude --工作树 缺陷修复-123
```

If you omit the 名称, Claude gene速率s a 随机 one 自动所有y:

```bash  主题={空}
# Auto-gene速率s a 名称 像 "亮-运行中-fox"
claude --工作树
```

工作树 are 创建d at `/.claude/工作树/` and 分支 from the 默认 远程 分支, which is where `起源/负责人` points. The 工作树 分支 is 名称d `工作树-`.

The 基础 分支 is not configur能够 th粗糙 a Claude 代码 flag or设置. `起源/负责人` is a 参考 存储 in your 本地 `.Git` 目录 that Git 设置 once when you 克隆. If the 仓库's 默认 分支 更晚 更改s on GitHub or GitLab, your 本地 `起源/负责人` keeps pointing at the 旧 one, and 工作树 will 分支 from there. To re-同步 your 本地 参考 with whatever the 远程 当前ly considers its 默认:

```bash  主题={空}
Git 远程 设置-负责人 起源 -a
```

This is a 标准 Git 命令 that only 更新s your 本地 `.Git` 目录. No薄g on the 远程 服务器 更改s. If you 想要 工作树 to 基础 off a 特定 分支 rather than the 远程's 默认, 设置 it 明确ly with `Git 远程 设置-负责人 起源 your-分支-名称`.

For 满 控制 结束 how 工作树 are 创建d,包括选择 a 不同 基础 per invocation, con图 a [工作树创建 hook](/en/钩子#工作树创建). The hook 替换s Claude 代码's 默认 `Git 工作树` 日志ic 整个ly, so you can 获取 and 分支 from whatever ref you 需要.

You can also ask Claude to "工作 in a 工作树" or "启动 a 工作树" 期间 a 会话, and it will 创建 one 自动所有y.

### Sub代理 工作树

子代理 can also 使用 工作树 isolation to 工作 in 平行 without conflicts. Ask Claude to "使用 工作树 for your 代理s" or con图 it in a [习俗 sub代理](/en/sub-代理s#支持ed-front事情-字段s) by添加 `isolation: 工作树` to the 代理's front事情. 每个 sub代理 gets its own 工作树 that is 自动所有y 干净ed 上 when the sub代理 finishes without 更改s.

### 工作树 干净上

When you 退出 a 工作树 会话, Claude handles 干净上 基础d on whether you made 更改s:

* **No 更改s**: the 工作树 and its 分支 are 移除 自动所有y
* **更改s or 提交s exist**: Claude 及时s you to keep or 移除 the 工作树.保持 preserves the 目录 and 分支 so you can 回报 更晚. Removing 删除s the 工作树 目录 and its 分支, discarding 所有 un提交ted 更改s and 提交s

Sub代理 工作树 orpH值aned by a crash or an 中断ed 平行 运行 are 移除 自动所有y at 启动上 once they are 更旧 than your [`干净上期间Days`](/en/设置#可用-设置)设置, provided they have no 修改s to 跟踪ed 文件 and no un推送ed 提交s. Un跟踪ed 文件 (新 文件 never 阶段d with `Git 添加`) are not 检查ed and do not pr事件 rem椭圆形. 工作树 you 创建 with `--工作树` are never 移除 by this 扫描.

To 干净 上 工作树 外部 of a Claude 会话, 使用 [手册 工作树 管理](#manage-工作树-手册ly).

  添加 `.claude/工作树/` to your `.Gitignore` to pr事件 工作树 满意s from 应用earing as un跟踪ed 文件 in your 主 仓库.

### 复制 Git忽略 文件 to 工作树

Git 工作树 are 新鲜 检查outs, so they don't include un跟踪ed 文件 像 `.env` or `.env.本地` from your 主 仓库. To 自动所有y 复制 these 文件 when Claude 创建s a 工作树, 添加 a `.工作树include` 文件 to your 项目 根.

The 文件 使用s `.Gitignore` syn税 to 列表 which 文件 to 复制. Only 文件 that 匹配 a 模式 and are also Git忽略 get 复制, so 跟踪ed 文件 are never 重复d.

```文本 .工作树include 主题={空}
.env
.env.本地
config/秘密s.json
```

This 应用lies to 工作树 创建d with `--工作树`, sub代理 工作树, and 平行 会话s in the [桌面 应用](/en/桌面#工作-in-平行-with-会话s).

### Manage 工作树 手册ly

For 更多 控制 结束 工作树 location and 分支 配置, 创建 工作树 with Git 直接ly. This is 有用 when you 需要 to 检查 out a 特定 现有 分支 or place the 工作树 外部 the 仓库.

```bash  主题={空}
# 创建 a 工作树 with a 新 分支
Git 工作树 添加 ../项目-feature-a -b feature-a

# 创建 a 工作树 with an 现有 分支
Git 工作树 添加 ../项目-缺陷修复 缺陷修复-123

# 启动 Claude in the 工作树
cd ../项目-feature-a && claude

# 干净 上 when 完成
Git 工作树 列表
Git 工作树 移除 ../项目-feature-a
```

Learn 更多 in the [official Git 工作树 文档](https://Git-scm.com/docs/Git-工作树).

  Re成员 to initialize your 开发 环境 in 每个 新 工作树 一致ing to your 项目's 设置. De待处理 on your 栈, this might include 运行中 dep结束ency 安装 (`npm 安装`, `yarn`),建立 虚拟 环境s, or跟随 your 项目's 标准 设置 流程.

### Non-Git 版本 控制

工作树 isolation 工作s with Git by 默认. For other 版本 控制 系统s 像 SVN, Per强制, or Mercurial, con图 [工作树创建 and 工作树移除 钩子](/en/钩子#工作树创建) to provide 习俗 工作树 创建 and 干净上 日志ic. When con图d, these 钩子 替换 the 默认 Git behavior when you 使用 `--工作树`, so [`.工作树include`](#复制-Git忽略-文件-to-工作树) is not 已处理. 复制 任何 本地 配置 文件 内部 your hook 脚本 instead.

For automated 协调 of 平行 会话s with 共享 任务s and 混乱aging,看见 [代理 团队](/en/代理-团队).

***

## Get notified when Claude 需要s your 注意

When you kick off a 长-运行中 任务 and switch to another 风ow, you can 设置 上 桌面 通知s so you know when Claude finishes or 需要s your 输入. This 使用s the `通知` [hook 事件](/en/钩子-指南#get-notified-when-claude-需要s-输入), which f怒s whenever Claude is 等待ing for 许可, 空闲 and 就绪 for a 新 及时, or完成 认证.

  
    打开 `~/.claude/设置.json` and 添加 a `通知` hook that c所有s your 平台's 本地 通知 命令:

    
      
        ```json  主题={空}
        {
          "钩子": {
            "通知": [
              {
                "匹配er": "",
                "钩子": [
                  {
                    "类型": "命令",
                    "命令": "osa脚本 -e '显示 通知 \"Claude 代码 需要s your 注意\" with 标题 \"Claude 代码\"'"
                  }
                ]
              }
            ]
          }
        }
        ```
      

      
        ```json  主题={空}
        {
          "钩子": {
            "通知": [
              {
                "匹配er": "",
                "钩子": [
                  {
                    "类型": "命令",
                    "命令": "notify-发送 'Claude 代码' 'Claude 代码 需要s your 注意'"
                  }
                ]
              }
            ]
          }
        }
        ```
      

      
        ```json  主题={空}
        {
          "钩子": {
            "通知": [
              {
                "匹配er": "",
                "钩子": [
                  {
                    "类型": "命令",
                    "命令": "权力命令行.exe -命令 \"[系统.Reflection.装配]::加载With偏袒名称('系统.风ows.形式s'); [系统.风ows.形式s.消息Box]::显示('Claude 代码 需要s your 注意', 'Claude 代码')\""
                  }
                ]
              }
            ]
          }
        }
        ```
      
    

    If your 设置 文件 al就绪 has a `钩子` 键, 合并 the `通知` 条目 into it rather than 结束writing. You can also ask Claude to 写入 the hook for you by描述 what you 想要 in the CLI.
  

  
    By 默认 the hook f怒s on 所有 通知 类型s. To f怒 only for 特定 事件s, 设置 the `匹配er` 字段 to one of these 值s:

    | 匹配er              | F怒s when                                      |
    | :------------------- | :---------------------------------------------- |
    | `许可_及时`  | Claude 需要s you to 批准 a 工具 使用          |
    | `空闲_及时`        | Claude is 完成 and 等待ing for your 下一个 及时 |
    | `auth_成功`       | 认证 完成s                        |
    | `e合法ation_dia日志` | Claude is询问 you a 问题                 |
  

  
    类型 `/钩子` and 选择 `通知` to 确认 the hook 应用ears.选择 it 显示s the 命令 that will 运行. To 测试 it 结束-to-结束, ask Claude to 运行 a 命令 that requ怒s 许可 and switch a方式 from the 终端, or ask Claude to trigger a 通知 直接ly.
  

For the 完成 事件 模式 and 通知 类型s,看见 the [通知 参考](/en/钩子#通知).

***

## 使用 Claude as a unix-style 实用

### 添加 Claude to your 验证 流程

S上pose you 想要 to 使用 Claude 代码 as a linter or 代码 审查员.

**添加 Claude to your 构建 脚本:**

```json  主题={空}
// 包.json
{
    ...
    "脚本s": {
        ...
        "lint:claude": "claude -p 'you are a linter. pl容易看 at the 更改s vs. 主 and 报告 任何 问题s r兴高采烈 to typos. 报告 the 文件名称 and 行 数字 on one 行, and a 描述 of the 问题 on the second 行. do not 回报 任何 other 文本.'"
    }
}
```

  提示:

  * 使用 Claude for automated 代码 re视图 in your CI/CD 管道
  * 习俗ize the 及时 to 检查 for 特定 问题s 相关 to your 项目
  * Consider创建 mul提示le 脚本s for 不同 类型s of 验证

### 管道 in, 管道 out

S上pose you 想要 to 管道 数据 into Claude, and get 返回 数据 in a 结构化 格式.

**管道 数据 th粗糙 Claude:**

```bash  主题={空}
cat 构建-错误.txt | claude -p 'concisely ex简单 the 根 原因 of this 构建 错误' > 输出.txt
```

  提示:

  * 使用 管道s to integ速率 Claude into 现有 命令行 脚本s
  * Combine with other Unix 工具 for 强大 工作流s
  * Consider using `--输出-格式` for 结构化 输出

### 控制 输出 格式

S上pose you 需要 Claude's 输出 in a 特定 格式, e特殊ly when集成 Claude 代码 into 脚本s or other 工具.

  
    ```bash  主题={空}
    cat 数据.txt | claude -p '总和marize this 数据' --输出-格式 文本 > 摘要.txt
    ```

    This 输出s 公正 Claude's 简单 文本 响应 (默认 behavior).
  

  
    ```bash  主题={空}
    cat 代码.py | claude -p '分析 this 代码 for 缺陷s' --输出-格式 json > 分析.json
    ```

    This 输出s a JSON 数组 of 消息s with meta数据包括 成本 and 持续时间.
  

  
    ```bash  主题={空}
    cat 日志.txt | claude -p 'parse this 日志 文件 for 错误' --输出-格式 流-json
    ```

    This 输出s a series of JSON 对象s in 真实-时间 as Claude 流程es the 请求. 每个 消息 is a 有效 JSON 对象, but the 整个 输出 is not 有效 JSON if concatenated.
  

  提示:

  * 使用 `--输出-格式 文本` for 简单 集成s where you 公正 需要 Claude's 响应
  * 使用 `--输出-格式 json` when you 需要 the 满 对话 日志
  * 使用 `--输出-格式 流-json` for 真实-时间 输出 of 每个 对话 turn

***

## 运行 Claude on a 时间表

S上pose you 想要 Claude to handle a 任务 自动所有y on a recurring 基础, 像回顾 打开 PRs 每个 morning, auditing 依赖 weekly, or检查 for CI 失败s 结束night.

Pick a scheduling 选项 基础d on where you 想要 the 任务 to 运行:

| 选项                                                          | Where it 运行s                     | 最佳 for                                                                                                      |
| :-------------------------------------------------------------- | :-------------------------------- | :------------------------------------------------------------------------------------------------------------ |
| [云 计划 任务s](/en/网页-计划-任务s)                | Anthropic-管理 基础设施  | 任务s that should 运行 偶数 when your computer is off. Con图 at [claude.ai/代码](https://claude.ai/代码).  |
| [桌面 计划 任务s](/en/桌面#时间表-recurring-任务s) | Your machine, via the 桌面 应用 | 任务s that 需要 直接 access to 本地 文件, 工具, or un提交ted 更改s.                                  |
| [GitHub 行动s](/en/GitHub-行动s)                            | Your CI 管道                  | 任务s tied to repo 事件s 像 打开 PRs, or cron 时间表s that should live a长side your 工作流 config. |
| [`/loop`](/en/计划-任务s)                                  | The 当前 CLI 会话           | 快 polling while a 会话 is 打开. 任务s are 已取消 when you 退出.                                     |

  When写作 及时s for 计划 任务s, be 明确 about what 成功看s 像 and what to do with 结果s. The 任务 运行s 自治ly, so it can't ask clarifying 问题s. For 示例: "Re视图 打开 PRs 标签ed `需要s-re视图`, leave in行 comments on 任何 问题s, and post a 摘要 in the `#eng-re视图s` 松弛 通道."

***

## Ask Claude about its 能力

Claude has built-in access to its 文档 and can 答案 问题s about its own features and 限制.

### 示例 问题s

```文本  主题={空}
can Claude 代码 创建 拉取请求s?
```

```文本  主题={空}
how does Claude 代码 handle 权限?
```

```文本  主题={空}
what 技能 are 可用?
```

```文本  主题={空}
how do I 使用 MCP with Claude 代码?
```

```文本  主题={空}
how do I con图 Claude 代码 for Amazon 基岩?
```

```文本  主题={空}
what are the 限制 of Claude 代码?
```

  Claude provides 文档-基础d 答案s to these 问题s. For hands-on 演示s, 运行 `/权力上` for 交互 较少ons with animated demos, or refer to the 特定 工作流 节s above.

  提示:

  * Claude al方式s has access to the 最新 Claude 代码 文档, 注意较少 of the 版本 you're using
  * Ask 特定 问题s to get 详情ed 答案s
  * Claude can ex简单 复杂 features 像 MCP 集成, 企业 配置s, and 先进 工作流s

***

## 下一步

  
    模式s for getting the 最多 out of Claude 代码
  

  
    Understand the 代理ic loop and 上下文 管理
  

  
    添加 技能, 钩子, MCP, 子代理, and 插件s
  

  
    克隆 the 开发 container 参考 实施