6 6
7> 使用 Claude Code 探索代码库、修复错误、重构、测试和其他日常任务的分步指南。7> 使用 Claude Code 探索代码库、修复错误、重构、测试和其他日常任务的分步指南。
8 8
9本页收集了日常开发的简短工作流程。有关提示和上下文管理的更高级指导,请参阅[最佳实践](/zh-CN/best-practices)。9本页收集了日常开发的简短工作流程。有关提示和上下文管理的更高级指导,请参阅[最佳实践](/docs/zh-CN/best-practices)。
10 10
11本页涵盖:11本页涵盖:
12 12
27 理解新的代码库27 理解新的代码库
28</h3>28</h3>
29 29
30有关在 monorepo 或大型代码库中配置 Claude Code 的信息,请参阅 [Monorepos 和大型存储库](/zh-CN/large-codebases)。30有关在 monorepo 或大型代码库中配置 Claude Code 的信息,请参阅 [Monorepos 和大型存储库](/docs/zh-CN/large-codebases)。
31 31
32<h4 id="get-a-quick-codebase-overview">32<h4 id="get-a-quick-codebase-overview">
33 快速获取代码库概览33 快速获取代码库概览
40 ```bash theme={null}40 ```bash theme={null}
41 cd /path/to/project 41 cd /path/to/project
42 ```42 ```
43
44 将 `/path/to/project` 替换为您项目的路径。
43 </Step>45 </Step>
44 46
45 <Step title="启动 Claude Code">47 <Step title="启动 Claude Code">
49 </Step>51 </Step>
50 52
51 <Step title="请求高级概览">53 <Step title="请求高级概览">
52 ```text theme={null}54 ```text wrap theme={null}
53 give me an overview of this codebase55 give me an overview of this codebase
54 ```56 ```
55 </Step>57 </Step>
56 58
57 <Step title="深入了解特定组件">59 <Step title="深入了解特定组件">
58 ```text theme={null}60 ```text wrap theme={null}
59 explain the main architecture patterns used here61 explain the main architecture patterns used here
60 ```62 ```
61 63
62 ```text theme={null}64 ```text wrap theme={null}
63 what are the key data models?65 what are the key data models?
64 ```66 ```
65 67
66 ```text theme={null}68 ```text wrap theme={null}
67 how is authentication handled?69 how is authentication handled?
68 ```70 ```
69 </Step>71 </Step>
85 87
86<Steps>88<Steps>
87 <Step title="要求 Claude 查找相关文件">89 <Step title="要求 Claude 查找相关文件">
88 ```text theme={null}90 ```text wrap theme={null}
89 find the files that handle user authentication91 find the files that handle user authentication
90 ```92 ```
91 </Step>93 </Step>
92 94
93 <Step title="获取有关组件如何交互的上下文">95 <Step title="获取有关组件如何交互的上下文">
94 ```text theme={null}96 ```text wrap theme={null}
95 how do these authentication files work together?97 how do these authentication files work together?
96 ```98 ```
97 </Step>99 </Step>
98 100
99 <Step title="理解执行流程">101 <Step title="理解执行流程">
100 ```text theme={null}102 ```text wrap theme={null}
101 trace the login process from front-end to database103 trace the login process from front-end to database
102 ```104 ```
103 </Step>105 </Step>
108 110
109 * 明确说明您要查找的内容111 * 明确说明您要查找的内容
110 * 使用项目中的领域语言112 * 使用项目中的领域语言
111 * 为您的语言安装[代码智能插件](/zh-CN/discover-plugins#code-intelligence),以便 Claude 能够精确地进行"转到定义"和"查找引用"导航113 * 为您的语言安装[代码智能插件](/docs/zh-CN/discover-plugins#code-intelligence),以便 Claude 能够精确地进行"转到定义"和"查找引用"导航
112</Tip>114</Tip>
113 115
114***116***
121 123
122<Steps>124<Steps>
123 <Step title="与 Claude 分享错误">125 <Step title="与 Claude 分享错误">
124 ```text theme={null}126 ```text wrap theme={null}
125 I'm seeing an error when I run npm test127 I'm seeing an error when I run npm test
126 ```128 ```
127 </Step>129 </Step>
128 130
129 <Step title="请求修复建议">131 <Step title="请求修复建议">
130 ```text theme={null}132 ```text wrap theme={null}
131 suggest a few ways to fix the @ts-ignore in user.ts133 suggest a few ways to fix the @ts-ignore in user.ts
132 ```134 ```
133 </Step>135 </Step>
134 136
135 <Step title="应用修复">137 <Step title="应用修复">
136 ```text theme={null}138 ```text wrap theme={null}
137 update user.ts to add the null check you suggested139 update user.ts to add the null check you suggested
138 ```140 ```
139 </Step>141 </Step>
155 157
156假设您需要更新旧代码以使用现代模式和实践。158假设您需要更新旧代码以使用现代模式和实践。
157 159
160有关将整个代码库迁移到新语言的信息,请参阅博客上的[Anthropic 如何使用 Claude Code 运行大规模代码迁移](https://claude.com/blog/ai-code-migration)。
161
158<Steps>162<Steps>
159 <Step title="识别用于重构的遗留代码">163 <Step title="识别用于重构的遗留代码">
160 ```text theme={null}164 ```text wrap theme={null}
161 find deprecated API usage in our codebase165 find deprecated API usage in our codebase
162 ```166 ```
163 </Step>167 </Step>
164 168
165 <Step title="获取重构建议">169 <Step title="获取重构建议">
166 ```text theme={null}170 ```text wrap theme={null}
167 suggest how to refactor utils.js to use modern JavaScript features171 suggest how to refactor utils.js to use modern JavaScript features
168 ```172 ```
169 </Step>173 </Step>
170 174
171 <Step title="安全地应用更改">175 <Step title="安全地应用更改">
172 ```text theme={null}176 ```text wrap theme={null}
173 refactor utils.js to use ES2024 features while maintaining the same behavior177 refactor utils.js to use ES2024 features while maintaining the same behavior
174 ```178 ```
175 </Step>179 </Step>
176 180
177 <Step title="验证重构">181 <Step title="验证重构">
178 ```text theme={null}182 ```text wrap theme={null}
179 run tests for the refactored code183 run tests for the refactored code
180 ```184 ```
181 </Step>185 </Step>
199 203
200<Steps>204<Steps>
201 <Step title="识别未测试的代码">205 <Step title="识别未测试的代码">
202 ```text theme={null}206 ```text wrap theme={null}
203 find functions in NotificationsService.swift that are not covered by tests207 find functions in NotificationsService.swift that are not covered by tests
204 ```208 ```
205 </Step>209 </Step>
206 210
207 <Step title="生成测试脚手架">211 <Step title="生成测试脚手架">
208 ```text theme={null}212 ```text wrap theme={null}
209 add tests for the notification service213 add tests for the notification service
210 ```214 ```
211 </Step>215 </Step>
212 216
213 <Step title="添加有意义的测试用例">217 <Step title="添加有意义的测试用例">
214 ```text theme={null}218 ```text wrap theme={null}
215 add test cases for edge conditions in the notification service219 add test cases for edge conditions in the notification service
216 ```220 ```
217 </Step>221 </Step>
218 222
219 <Step title="运行并验证测试">223 <Step title="运行并验证测试">
220 ```text theme={null}224 ```text wrap theme={null}
221 run the new tests and fix any failures225 run the new tests and fix any failures
222 ```226 ```
223 </Step>227 </Step>
237 241
238<Steps>242<Steps>
239 <Step title="总结您的更改">243 <Step title="总结您的更改">
240 ```text theme={null}244 ```text wrap theme={null}
241 summarize the changes I've made to the authentication module245 summarize the changes I've made to the authentication module
242 ```246 ```
243 </Step>247 </Step>
244 248
245 <Step title="生成拉取请求">249 <Step title="生成拉取请求">
246 ```text theme={null}250 ```text wrap theme={null}
247 create a pr251 create a pr
248 ```252 ```
249 </Step>253 </Step>
250 254
251 <Step title="审查和细化">255 <Step title="审查和细化">
252 ```text theme={null}256 ```text wrap theme={null}
253 enhance the PR description with more context about the security improvements257 enhance the PR description with more context about the security improvements
254 ```258 ```
255 </Step>259 </Step>
256</Steps>260</Steps>
257 261
258当您使用 `gh pr create` 创建 PR 时,会话会自动链接到该 PR。要稍后返回它,请运行 `claude --from-pr 123`,将 123 替换为 PR 编号,或将 PR URL 粘贴到[`/resume` 选择器](/zh-CN/sessions#use-the-session-picker)搜索中。262要稍后找到会话,请运行 `claude --from-pr 1234`,将 1234 替换为您自己的 PR 编号,这会打开会话选择器,筛选链接到该 PR 的会话,或将 PR URL 粘贴到 [`/resume` 选择器](/docs/zh-CN/sessions#use-the-session-picker)搜索中。当 Claude 使用 `gh pr create` 或 `glab mr create` 创建拉取请求时,Claude Code 会将会话链接到 PR,以及当 Claude [处理现有 PR](/docs/zh-CN/agent-view#pull-request-status) 时。
259 263
260<Tip>264<Tip>
261 在提交前审查 Claude 生成的 PR,并要求 Claude 突出显示潜在的风险或注意事项。265 在提交前审查 Claude 生成的 PR,并要求 Claude 突出显示潜在的风险或注意事项。
269 273
270<Steps>274<Steps>
271 <Step title="识别未记录的代码">275 <Step title="识别未记录的代码">
272 ```text theme={null}276 ```text wrap theme={null}
273 find functions without proper JSDoc comments in the auth module277 find functions without proper JSDoc comments in the auth module
274 ```278 ```
275 </Step>279 </Step>
276 280
277 <Step title="生成文档">281 <Step title="生成文档">
278 ```text theme={null}282 ```text wrap theme={null}
279 add JSDoc comments to the undocumented functions in auth.js283 add JSDoc comments to the undocumented functions in auth.js
280 ```284 ```
281 </Step>285 </Step>
282 286
283 <Step title="审查和增强">287 <Step title="审查和增强">
284 ```text theme={null}288 ```text wrap theme={null}
285 improve the generated documentation with more context and examples289 improve the generated documentation with more context and examples
286 ```290 ```
287 </Step>291 </Step>
288 292
289 <Step title="验证文档">293 <Step title="验证文档">
290 ```text theme={null}294 ```text wrap theme={null}
291 check if the documentation follows our project standards295 check if the documentation follows our project standards
292 ```296 ```
293 </Step>297 </Step>
324 您可以使用以下任何方法:328 您可以使用以下任何方法:
325 329
326 1. 将图像拖放到 Claude Code 窗口中330 1. 将图像拖放到 Claude Code 窗口中
327 2. 复制图像并使用 Ctrl+V 将其粘贴到 CLI 中。在 macOS 上,Cmd+V 也适用于 iTerm2。331 2. 复制图像并使用 `Ctrl+V` 将其粘贴到 CLI 中,或在 [Windows 和 WSL 上使用 `Alt+V`](/docs/zh-CN/interactive-mode#general-controls)
328 3. 向 Claude 提供图像路径。例如,"Analyze this image: /path/to/your/image.png"332 3. 向 Claude 提供图像路径。例如,"Analyze this image: /path/to/your/image.png"
329 </Step>333 </Step>
330 334
331 <Step title="要求 Claude 分析图像">335 <Step title="要求 Claude 分析图像">
332 ```text theme={null}336 ```text wrap theme={null}
333 What does this image show?337 What does this image show?
334 ```338 ```
335 339
336 ```text theme={null}340 ```text wrap theme={null}
337 Describe the UI elements in this screenshot341 Describe the UI elements in this screenshot
338 ```342 ```
339 343
340 ```text theme={null}344 ```text wrap theme={null}
341 Are there any problematic elements in this diagram?345 Are there any problematic elements in this diagram?
342 ```346 ```
343 </Step>347 </Step>
344 348
345 <Step title="使用图像获取上下文">349 <Step title="使用图像获取上下文">
346 ```text theme={null}350 ```text wrap theme={null}
347 Here's a screenshot of the error. What's causing it?351 Here's a screenshot of the error. What's causing it?
348 ```352 ```
349 353
350 ```text theme={null}354 ```text wrap theme={null}
351 This is our current database schema. How should we modify it for the new feature?355 This is our current database schema. How should we modify it for the new feature?
352 ```356 ```
353 </Step>357 </Step>
354 358
355 <Step title="从视觉内容获取代码建议">359 <Step title="从视觉内容获取代码建议">
356 ```text theme={null}360 ```text wrap theme={null}
357 Generate CSS to match this design mockup361 Generate CSS to match this design mockup
358 ```362 ```
359 363
360 ```text theme={null}364 ```text wrap theme={null}
361 What HTML structure would recreate this component?365 What HTML structure would recreate this component?
362 ```366 ```
363 </Step>367 </Step>
383 387
384<Steps>388<Steps>
385 <Step title="引用单个文件">389 <Step title="引用单个文件">
386 ```text theme={null}390 ```text wrap theme={null}
387 Explain the logic in @src/utils/auth.js391 Explain the logic in @src/utils/auth.js
388 ```392 ```
389 393
391 </Step>395 </Step>
392 396
393 <Step title="引用目录">397 <Step title="引用目录">
394 ```text theme={null}398 ```text wrap theme={null}
395 What's the structure of @src/components?399 What's the structure of @src/components?
396 ```400 ```
397
398 这提供了带有文件信息的目录列表。
399 </Step>401 </Step>
400 402
401 <Step title="引用 MCP 资源">403 <Step title="引用 MCP 资源">
402 ```text theme={null}404 ```text wrap theme={null}
403 Show me the data from @github:repos/owner/repo/issues405 Show me the data from @github:repos/owner/repo/issues
404 ```406 ```
405 407
406 这使用 @server:resource 格式从连接的 MCP 服务器获取数据。有关详细信息,请参阅 [MCP 资源](/zh-CN/mcp#use-mcp-resources)。408 这使用 @server:resource 格式从连接的 MCP 服务器获取数据。有关详细信息,请参阅 [MCP 资源](/docs/zh-CN/mcp#use-mcp-resources)。
407 </Step>409 </Step>
408</Steps>410</Steps>
409 411
411 提示:413 提示:
412 414
413 * 文件路径可以是相对的或绝对的415 * 文件路径可以是相对的或绝对的
416 * 键入 `@` 打开路径建议菜单,然后按 Enter 或 Tab 接受突出显示的路径,再按 Enter 发送消息
414 * @ 文件引用在文件的目录和父目录中添加 `CLAUDE.md` 到上下文417 * @ 文件引用在文件的目录和父目录中添加 `CLAUDE.md` 到上下文
415 * 目录引用显示文件列表,而不是内容418 * 目录引用显示文件列表,而不是内容
416 * 您可以在单个消息中引用多个文件(例如,"@file1.js and @file2.js")419 * 您可以在单个消息中引用多个文件(例如,"@file1.js and @file2.js")
427根据您希望任务运行的位置选择调度选项:430根据您希望任务运行的位置选择调度选项:
428 431
429| 选项 | 运行位置 | 最适合 |432| 选项 | 运行位置 | 最适合 |
430| :--------------------------------------- | :---------------- | :---------------------------------------------------------------------------------------------------------------- |433| :--------------------------------------- | :------------------ | :---------------------------------------------------------------------------------------------------------------- |
431| [Routines](/zh-CN/routines) | Anthropic 管理的基础设施 | 即使您的计算机关闭也应该运行的任务。也可以在 API 调用或 GitHub 事件上触发,除了计划。在 [claude.ai/code/routines](https://claude.ai/code/routines) 配置。 |434| [Routines](/docs/zh-CN/routines) | 云端,默认由 Anthropic 管理 | 即使您的计算机关闭也应该运行的任务。也可以在 API 调用或 GitHub 事件上触发,除了计划。在 [claude.ai/code/routines](https://claude.ai/code/routines) 配置。 |
432| [桌面计划任务](/zh-CN/desktop-scheduled-tasks) | 您的机器,通过桌面应用 | 需要直接访问本地文件、工具或未提交更改的任务。 |435| [桌面计划任务](/docs/zh-CN/desktop-scheduled-tasks) | 您的机器,通过桌面应用 | 需要直接访问本地文件、工具或未提交更改的任务。 |
433| [GitHub Actions](/zh-CN/github-actions) | 您的 CI 管道 | 与存储库事件(如打开的 PR)相关的任务,或应该与工作流配置一起存在的 cron 计划。 |436| [GitHub Actions](/docs/zh-CN/github-actions) | 您的 CI 管道 | 与存储库事件(如打开的 PR)相关的任务,或应该与工作流配置一起存在的 cron 计划。 |
434| [`/loop`](/zh-CN/scheduled-tasks) | 当前 CLI 会话 | 会话打开时的快速轮询。任务在您开始新对话时停止;`--resume` 和 `--continue` 恢复未过期的任务。 |437| [`/loop`](/docs/zh-CN/scheduled-tasks) | 当前 CLI 会话 | 会话打开时的快速轮询。任务在您开始新对话时停止;`--resume` 和 `--continue` 恢复未过期的任务。 |
435 438
436<Tip>439<Tip>
437 为计划任务编写提示时,明确说明成功是什么样的以及如何处理结果。任务自主运行,所以它不能提出澄清问题。例如:"审查标记为 `needs-review` 的开放 PR,对任何问题留下内联评论,并在 `#eng-reviews` Slack 频道中发布摘要。"440 为计划任务编写提示时,明确说明成功是什么样的以及如何处理结果。任务自主运行,所以它不能提出澄清问题。例如:"审查标记为 `needs-review` 的开放 PR,对任何问题留下内联评论,并在 `#eng-reviews` Slack 频道中发布摘要。"
449 示例问题452 示例问题
450</h4>453</h4>
451 454
452```text theme={null}455```text wrap theme={null}
453can Claude Code create pull requests?456can Claude Code create pull requests?
454```457```
455 458
456```text theme={null}459```text wrap theme={null}
457how does Claude Code handle permissions?460how does Claude Code handle permissions?
458```461```
459 462
460```text theme={null}463```text wrap theme={null}
461what skills are available?464what skills are available?
462```465```
463 466
464```text theme={null}467```text wrap theme={null}
465how do I use MCP with Claude Code?468how do I use MCP with Claude Code?
466```469```
467 470
468```text theme={null}471```text wrap theme={null}
469how do I configure Claude Code for Amazon Bedrock?472how do I configure Claude Code for Amazon Bedrock?
470```473```
471 474
472```text theme={null}475```text wrap theme={null}
473what are the limitations of Claude Code?476what are the limitations of Claude Code?
474```477```
475 478
497claude --continue500claude --continue
498```501```
499 502
500这会恢复当前目录中最近的会话;如果还没有,它会打印 `No conversation found to continue` 并退出。使用 `claude --resume` 从列表中选择,或从运行中的会话内使用 `/resume`。有关命名、分支和完整选择器参考,请参阅[管理会话](/zh-CN/sessions)。503这会恢复当前目录中最近的会话;如果还没有,它会打印 `No conversation found to continue` 并退出。使用 `claude --resume` 从列表中选择,或从运行中的会话内使用 `/resume`。有关命名、分支和完整选择器参考,请参阅[管理会话](/docs/zh-CN/sessions)。
501 504
502<h2 id="run-parallel-sessions-with-worktrees">505<h2 id="run-parallel-sessions-with-worktrees">
503 使用 worktrees 运行并行会话506 使用 worktrees 运行并行会话
504</h2>507</h2>
505 508
506在一个终端中处理功能,同时 Claude 在另一个终端中修复错误,而不会编辑冲突。每个 worktree 是其自己分支上的单独检出。509在一个终端中处理功能,同时 Claude 在另一个终端中修复错误,而不会编辑冲突。每个 [git worktree](https://git-scm.com/docs/git-worktree) 是其自己分支上的单独检出,从现有提交创建,因此存储库首先需要至少一个提交。
507 510
508```bash theme={null}511```bash theme={null}
509claude --worktree feature-auth512claude --worktree feature-auth
510```513```
511 514
512在第二个终端中使用不同的名称运行相同的命令以启动隔离的并行会话。有关清理、`.worktreeinclude` 和非 git VCS 支持,请参阅 [Worktrees](/zh-CN/worktrees)。要从一个屏幕而不是单独的终端监视并行会话,请参阅[后台代理](/zh-CN/agent-view)。515在第二个终端中使用不同的名称运行相同的命令以启动隔离的并行会话。在没有提交的存储库中,该命令失败并显示 `Failed to resolve base branch "HEAD": git rev-parse failed`。有关清理、`.worktreeinclude` 和非 git VCS 支持,请参阅 [Worktrees](/docs/zh-CN/worktrees)。要从一个屏幕而不是单独的终端监视并行会话,请参阅[后台代理](/docs/zh-CN/agent-view)。
513 516
514<h2 id="plan-before-editing">517<h2 id="plan-before-editing">
515 编辑前规划518 编辑前规划
516</h2>519</h2>
517 520
518对于您想在接触磁盘前审查的更改,切换到 plan mode。Claude 读取文件并提出计划,但在您批准前不进行任何编辑。521对于您想在接触磁盘前审查的更改,切换到 plan mode。Claude 读取文件并提出计划,但在您批准前不进行任何编辑。状态栏在 plan mode 激活时显示 `⏸ plan mode on`。
519 522
520```bash theme={null}523```bash theme={null}
521claude --permission-mode plan524claude --permission-mode plan
522```525```
523 526
524您也可以在会话中按 `Shift+Tab` 切换到 plan mode。有关批准流程和在文本编辑器中编辑计划,请参阅 [Plan mode](/zh-CN/permission-modes#analyze-before-you-edit-with-plan-mode)。527您也可以在会话中按 `Shift+Tab` 直到状态栏显示 `⏸ plan mode on` 来切换到 plan mode。有关批准流程和在文本编辑器中编辑计划,请参阅 [Plan mode](/docs/zh-CN/permission-modes#analyze-before-you-edit-with-plan-mode)。
525 528
526<h2 id="delegate-research-to-subagents">529<h2 id="delegate-research-to-subagents">
527 将研究委派给 subagents530 将研究委派给 subagents
529 532
530探索大型代码库会用文件读取填充您的上下文。委派探索,以便只有发现结果返回。533探索大型代码库会用文件读取填充您的上下文。委派探索,以便只有发现结果返回。
531 534
532```text theme={null}535```text wrap theme={null}
533use a subagent to investigate how our auth system handles token refresh536use a subagent to investigate how our auth system handles token refresh
534```537```
535 538
536subagent 在其自己的上下文窗口中读取文件并报告摘要。有关定义具有自己工具和提示的自定义代理,请参阅 [Subagents](/zh-CN/sub-agents)。539subagent 在其自己的上下文窗口中读取文件并报告摘要。有关定义具有自己工具和提示的自定义代理,请参阅 [Subagents](/docs/zh-CN/sub-agents)。
537 540
538<h2 id="pipe-claude-into-scripts">541<h2 id="pipe-claude-into-scripts">
539 将 Claude 管道输入脚本542 将 Claude 管道输入脚本
545git log --oneline -20 | claude -p "summarize these recent commits"548git log --oneline -20 | claude -p "summarize these recent commits"
546```549```
547 550
548有关输出格式、权限标志和扇出模式,请参阅[非交互模式](/zh-CN/headless)。551有关输出格式、权限标志和扇出模式,请参阅[非交互模式](/docs/zh-CN/headless)。
549 552
550<h2 id="next-steps">553<h2 id="next-steps">
551 后续步骤554 后续步骤
552</h2>555</h2>
553 556
554<CardGroup cols={2}>557<CardGroup cols={2}>
555 <Card title="最佳实践" icon="lightbulb" href="/zh-CN/best-practices">558 <Card title="最佳实践" icon="lightbulb" href="/docs/zh-CN/best-practices">
556 充分利用 Claude Code 的模式559 充分利用 Claude Code 的模式
557 </Card>560 </Card>
558 561
559 <Card title="管理会话" icon="rotate-left" href="/zh-CN/sessions">562 <Card title="管理会话" icon="rotate-left" href="/docs/zh-CN/sessions">
560 恢复、命名和分支对话563 恢复、命名和分支对话
561 </Card>564 </Card>
562 565
563 <Card title="Worktrees" icon="code-branch" href="/zh-CN/worktrees">566 <Card title="Worktrees" icon="code-branch" href="/docs/zh-CN/worktrees">
564 运行隔离的并行会话567 运行隔离的并行会话
565 </Card>568 </Card>
566 569
567 <Card title="扩展 Claude Code" icon="puzzle-piece" href="/zh-CN/features-overview">570 <Card title="扩展 Claude Code" icon="puzzle-piece" href="/docs/zh-CN/features-overview">
568 添加 skills、hooks、MCP、subagents 和插件571 添加 skills、hooks、MCP、subagents 和插件
569 </Card>572 </Card>
570</CardGroup>573</CardGroup>