常见工作流程
使用 Claude Code 探索代码库、修复错误、重构、测试和其他日常任务的分步指南。
本页收集了日常开发的简短工作流程。有关提示和上下文管理的更高级指导,请参阅最佳实践。
本页涵盖:
提示工作流程
这些是日常任务的提示模式,如探索陌生代码、调试、重构、编写测试和创建 PR。每个都可以在任何 Claude Code 界面中工作;根据您的项目调整措辞。
理解新的代码库
有关在 monorepo 或大型代码库中配置 Claude Code 的信息,请参阅 Monorepos 和大型存储库。
快速获取代码库概览
假设您刚加入一个新项目,需要快速了解其结构。
theme
theme
theme1
| give me an overview of this codebase
|
theme1
| explain the main architecture patterns used here
|
theme1
| what are the key data models?
|
theme1
| how is authentication handled?
|
提示:
- 从广泛的问题开始,然后缩小到特定领域
- 询问项目中使用的编码约定和模式
- 请求项目特定术语的词汇表
查找相关代码
假设您需要定位与特定功能相关的代码。
theme1
| find the files that handle user authentication
|
theme1
| how do these authentication files work together?
|
theme1
| trace the login process from front-end to database
|
提示:
- 明确说明您要查找的内容
- 使用项目中的领域语言
- 为您的语言安装代码智能插件,以便 Claude 能够精确地进行”转到定义”和”查找引用”导航
高效修复错误
假设您遇到了错误消息,需要找到并修复其来源。
theme1
| I'm seeing an error when I run npm test
|
theme1
| suggest a few ways to fix the @ts-ignore in user.ts
|
theme1
| update user.ts to add the null check you suggested
|
提示:
- 告诉 Claude 重现问题的命令并获取堆栈跟踪
- 提及重现错误的任何步骤
- 让 Claude 知道错误是间歇性的还是持续的
重构代码
假设您需要更新旧代码以使用现代模式和实践。
theme1
| find deprecated API usage in our codebase
|
theme1
| suggest how to refactor utils.js to use modern JavaScript features
|
theme1
| refactor utils.js to use ES2024 features while maintaining the same behavior
|
theme1
| run tests for the refactored code
|
提示:
- 要求 Claude 解释现代方法的优势
- 在需要时请求更改保持向后兼容性
- 以小的、可测试的增量进行重构
使用测试
假设您需要为未覆盖的代码添加测试。
theme1
| find functions in NotificationsService.swift that are not covered by tests
|
theme1
| add tests for the notification service
|
theme1
| add test cases for edge conditions in the notification service
|
theme1
| run the new tests and fix any failures
|
Claude 可以生成遵循您项目现有模式和约定的测试。请求测试时,请明确说明您想验证的行为。Claude 检查您现有的测试文件以匹配已在使用的样式、框架和断言模式。
为了获得全面的覆盖,要求 Claude 识别您可能遗漏的边界情况。Claude 可以分析您的代码路径并建议测试错误条件、边界值和容易被忽视的意外输入。
创建拉取请求
您可以通过直接要求 Claude 创建拉取请求(”create a pr for my changes”),或逐步指导 Claude:
theme1
| summarize the changes I've made to the authentication module
|
theme
theme1
| enhance the PR description with more context about the security improvements
|
当您使用 gh pr create 创建 PR 时,会话会自动链接到该 PR。要稍后返回它,请运行 claude --from-pr <number> 或将 PR URL 粘贴到/resume 选择器搜索中。
在提交前审查 Claude 生成的 PR,并要求 Claude 突出显示潜在的风险或注意事项。
处理文档
假设您需要为代码添加或更新文档。
theme1
| find functions without proper JSDoc comments in the auth module
|
theme1
| add JSDoc comments to the undocumented functions in auth.js
|
theme1
| improve the generated documentation with more context and examples
|
theme1
| check if the documentation follows our project standards
|
提示:
- 指定您想要的文档样式(JSDoc、docstrings 等)
- 请求文档中的示例
- 请求公共 API、接口和复杂逻辑的文档
在笔记和非代码文件夹中工作
Claude Code 可以在任何目录中工作。在笔记库、文档文件夹或任何 markdown 文件集合中运行它,以搜索、编辑和重新组织内容,就像处理代码一样。
.claude/ 目录和 CLAUDE.md 与其他工具的配置目录并排存在,不会产生冲突。Claude 在每次工具调用时都会重新读取文件,因此它会在下次读取该文件时看到您在另一个应用程序中所做的编辑。
使用图像
假设您需要在代码库中使用图像,并希望 Claude 帮助分析图像内容。
您可以使用以下任何方法:
1. 将图像拖放到 Claude Code 窗口中
2. 复制图像并使用 ctrl+v 将其粘贴到 CLI 中(不要使用 cmd+v)
3. 向 Claude 提供图像路径。例如,"Analyze this image: /path/to/your/image.png"
theme1
| What does this image show?
|
theme1
| Describe the UI elements in this screenshot
|
theme1
| Are there any problematic elements in this diagram?
|
theme1
| Here's a screenshot of the error. What's causing it?
|
theme1
| This is our current database schema. How should we modify it for the new feature?
|
theme1
| Generate CSS to match this design mockup
|
theme1
| What HTML structure would recreate this component?
|
提示:
- 当文本描述不清楚或繁琐时使用图像
- 包含错误、UI 设计或图表的屏幕截图以获得更好的上下文
- 您可以在对话中使用多个图像
- 图像分析适用于图表、屏幕截图、模型等
- 当 Claude 引用图像时(例如,
[Image #1]),Cmd+Click(Mac)或 Ctrl+Click(Windows/Linux)链接以在默认查看器中打开图像
引用文件和目录
使用 @ 快速包含文件或目录,无需等待 Claude 读取它们。
theme1
| Explain the logic in @src/utils/auth.js
|
这在对话中包含文件的完整内容。
theme1
| What's the structure of @src/components?
|
这提供了带有文件信息的目录列表。
theme1
| Show me the data from @github:repos/owner/repo/issues
|
这使用 @server:resource 格式从连接的 MCP 服务器获取数据。有关详细信息,请参阅 [MCP 资源](/zh-CN/mcp#use-mcp-resources)。
提示:
- 文件路径可以是相对的或绝对的
- @ 文件引用在文件的目录和父目录中添加
CLAUDE.md 到上下文
- 目录引用显示文件列表,而不是内容
- 您可以在单个消息中引用多个文件(例如,”@file1.js and @file2.js”)
按计划运行 Claude
假设您想让 Claude 自动定期处理任务,如每天早上审查开放的 PR、每周审计依赖项或在夜间检查 CI 失败。
根据您希望任务运行的位置选择调度选项:
| 选项 |
运行位置 |
最适合 |
| Routines |
Anthropic 管理的基础设施 |
即使您的计算机关闭也应该运行的任务。也可以在 API 调用或 GitHub 事件上触发,除了计划。在 claude.ai/code/routines 配置。 |
| 桌面计划任务 |
您的机器,通过桌面应用 |
需要直接访问本地文件、工具或未提交更改的任务。 |
| GitHub Actions |
您的 CI 管道 |
与存储库事件(如打开的 PR)相关的任务,或应该与工作流配置一起存在的 cron 计划。 |
/loop |
当前 CLI 会话 |
会话打开时的快速轮询。任务在您开始新对话时停止;--resume 和 --continue 恢复未过期的任务。 |
为计划任务编写提示时,明确说明成功是什么样的以及如何处理结果。任务自主运行,所以它不能提出澄清问题。例如:"审查标记为 `needs-review` 的开放 PR,对任何问题留下内联评论,并在 `#eng-reviews` Slack 频道中发布摘要。"
询问 Claude 关于其功能
Claude 内置访问其文档,可以回答关于其自身功能和限制的问题。
示例问题
theme1
| can Claude Code create pull requests?
|
theme1
| how does Claude Code handle permissions?
|
theme1
| what skills are available?
|
theme1
| how do I use MCP with Claude Code?
|
theme1
| how do I configure Claude Code for Amazon Bedrock?
|
theme1
| what are the limitations of Claude Code?
|
Claude 基于文档提供对这些问题的答案。有关可执行示例和实际演示,请运行 `/powerup` 以获得带有动画演示的交互式课程,或参考上面的特定工作流程部分。
提示:
- Claude 始终可以访问最新的 Claude Code 文档,无论您使用的版本如何
- 提出具体问题以获得详细答案
- Claude 可以解释复杂的功能,如 MCP 集成、企业配置和高级工作流程
恢复以前的对话
当任务跨越多个会话时,从您离开的地方继续,而不是重新解释上下文。Claude Code 在本地保存每个对话。
theme
这会恢复当前目录中最近的会话;如果还没有,它会打印 No conversation found to continue 并退出。使用 claude --resume 从列表中选择,或从运行中的会话内使用 /resume。有关命名、分支和完整选择器参考,请参阅管理会话。
使用 worktrees 运行并行会话
在一个终端中处理功能,同时 Claude 在另一个终端中修复错误,而不会编辑冲突。每个 worktree 是其自己分支上的单独检出。
theme1
| claude --worktree feature-auth
|
在第二个终端中使用不同的名称运行相同的命令以启动隔离的并行会话。有关清理、.worktreeinclude 和非 git VCS 支持,请参阅 Worktrees。要从一个屏幕而不是单独的终端监视并行会话,请参阅后台代理。
编辑前规划
对于您想在接触磁盘前审查的更改,切换到 plan mode。Claude 读取文件并提出计划,但在您批准前不进行任何编辑。
theme1
| claude --permission-mode plan
|
您也可以在会话中按 Shift+Tab 切换到 plan mode。有关批准流程和在文本编辑器中编辑计划,请参阅 Plan Mode。
将研究委派给 subagents
探索大型代码库会用文件读取填充您的上下文。委派探索,以便只有发现结果返回。
theme1
| use a subagent to investigate how our auth system handles token refresh
|
subagent 在其自己的上下文窗口中读取文件并报告摘要。有关定义具有自己工具和提示的自定义代理,请参阅 Subagents。
将 Claude 管道输入脚本
以非交互方式运行 Claude 用于 CI、预提交钩子或批处理。stdin 和 stdout 像任何 Unix 工具一样工作。
theme1
| git log --oneline -20 | claude -p "summarize these recent commits"
|
有关输出格式、权限标志和扇出模式,请参阅非交互模式。
后续步骤
充分利用 Claude Code 的模式
恢复、命名和分支对话
运行隔离的并行会话
添加 skills、hooks、MCP、subagents 和插件