Skip to content

Claude Code 常见工作流程指南

本文介绍了使用 Claude Code 的常见工作流程。每个任务都包含清晰的说明、示例命令和最佳实践,以帮助更好地利用 Claude Code。

1️⃣ 理解新代码库

1.1 快速获取代码库概览

假设您刚加入一个新项目,需要快速了解其结构。

  1. 导航到项目根目录

    bash
    cd /path/to/project
  2. 启动 Claude Code

    bash
    claude
  3. 请求高级概览

    > give me an overview of this codebase
    > explain the main architecture patterns used here
    > what are the key data models?
    > how is authentication handled?

💡 提示

  • 从广泛的问题开始,然后缩小到特定领域
  • 询问项目中使用的编码约定和模式
  • 请求项目特定术语的词汇表

1.2 查找相关代码

假设您需要定位与特定功能相关的代码。

  1. 让 Claude 查找相关文件

    > find the files that handle user authentication
  2. 了解组件如何交互

    > how do these authentication files work together?
  3. 理解执行流程

    > trace the login process from front-end to database

💡 提示

  • 明确说明您要查找的内容
  • 使用项目中的领域语言

2️⃣ 高效修复错误

假设您遇到了错误消息,需要找到并修复其源头。

  1. 与 Claude 分享错误

    > I'm seeing an error when I run npm test
  2. 请求修复建议

    > suggest a few ways to fix the @ts-ignore in user.ts
  3. 应用修复

    > update user.ts to add the null check you suggested

💡 提示

  • 告诉 Claude 重现问题的命令并获取堆栈跟踪
  • 提及重现错误的任何步骤
  • 让 Claude 知道错误是间歇性的还是持续的

3️⃣ 重构代码

假设您需要更新旧代码以使用现代模式和实践。

  1. 识别需要重构的遗留代码

    > find deprecated API usage in our codebase
  2. 获取重构建议

    > suggest how to refactor utils.js to use modern JavaScript features
  3. 安全地应用更改

    > refactor utils.js to use ES2024 features while maintaining the same behavior
  4. 验证重构

    > run tests for the refactored code

💡 提示

  • 让 Claude 解释现代方法的好处
  • 在需要时请求更改,保持向后兼容性
  • 以小的、可测试的增量进行重构

4️⃣ 处理测试

假设您需要为未覆盖的代码添加测试。

  1. 识别未测试的代码

    > find functions in NotificationsService.swift that are not covered by tests
  2. 生成测试脚手架

    > add tests for the notification service
  3. 添加有意义的测试用例

    > add test cases for edge conditions in the notification service
  4. 运行并验证测试

    > run the new tests and fix any failures

💡 提示

  • 请求覆盖边缘情况和错误条件的测试
  • 在适当时请求单元测试和集成测试
  • 让 Claude 解释测试策略

5️⃣ 创建拉取请求

假设您需要为您的更改创建一个文档完善的拉取请求。

  1. 总结您的更改

    > summarize the changes I've made to the authentication module
  2. 使用 Claude 生成 PR

    > create a pr
  3. 审查和完善

    > enhance the PR description with more context about the security improvements
  4. 添加测试详情

    > add information about how these changes were tested

💡 提示

  • 直接让 Claude 为您创建 PR
  • 在提交前审查 Claude 生成的 PR
  • 让 Claude 突出潜在的风险或考虑因素

6️⃣ 处理文档

假设您需要为您的代码添加或更新文档。

  1. 识别未文档化的代码

    > find functions without proper JSDoc comments in the auth module
  2. 生成文档

    > add JSDoc comments to the undocumented functions in auth.js
  3. 审查和增强

    > improve the generated documentation with more context and examples
  4. 验证文档

    > check if the documentation follows our project standards

💡 提示

  • 指定您想要的文档样式(JSDoc、docstrings 等)
  • 在文档中请求示例
  • 为公共 API、接口和复杂逻辑请求文档

7️⃣ 处理图像

假设您需要在代码库中处理图像,并希望 Claude 帮助分析图像内容。

向对话添加图像

您可以使用以下任何方法:

  • 将图像拖放到 Claude Code 窗口中
  • 复制图像并使用 ctrl+v 粘贴到 CLI 中(不要使用 cmd+v
  • 提供图像路径给 claude:"Analyze this image: /path/to/your/image.png"

让 Claude 分析图像

> What does this image show?
> Describe the UI elements in this screenshot
> Are there any problematic elements in this diagram?

使用图像提供上下文

> Here's a screenshot of the error. What's causing it?
> This is our current database schema. How should we modify it for the new feature?

从视觉内容获取代码建议

> Generate CSS to match this design mockup
> What HTML structure would recreate this component?

💡 提示

  • 当文本描述不清楚或繁琐时使用图像
  • 包含错误截图、UI 设计或图表以获得更好的上下文
  • 您可以在对话中使用多个图像
  • 图像分析适用于图表、截图、模型图等

8️⃣ 使用扩展思考

假设您正在处理复杂的架构决策、具有挑战性的错误或需要深度推理的多步骤实现规划。

提供上下文并让 Claude 思考

> I need to implement a new authentication system using OAuth2 for our API. 
  Think deeply about the best approach for implementing this in our codebase.

Claude 将从您的代码库收集相关信息并使用扩展思考,这将在界面中可见。

通过后续提示完善思考

> think about potential security vulnerabilities in this approach
> think harder about edge cases we should handle

💡 获得最大价值的提示

扩展思考对以下复杂任务最有价值:

  • 规划复杂的架构更改
  • 调试复杂问题
  • 为新功能创建实现计划
  • 理解复杂的代码库
  • 评估不同方法之间的权衡

思考深度控制

  • "think" 触发基本扩展思考
  • "think more""think a lot""think harder""think longer" 触发更深层的思考

Claude 将在响应上方以斜体灰色文本显示其思考过程。

9️⃣ 恢复之前的对话

假设您一直在使用 Claude Code 处理任务,需要在稍后的会话中从中断的地方继续。

Claude Code 提供两个选项来恢复之前的对话:

  • --continue 自动继续最近的对话
  • --resume 显示对话选择器

继续最近的对话

bash
claude --continue

这会立即恢复您最近的对话,无需任何提示。

在非交互模式下继续

bash
claude --continue --print "Continue with my task"

使用 --print--continue 在非交互模式下恢复最近的对话,非常适合脚本或自动化。

显示对话选择器

bash
claude --resume

这会显示一个交互式对话选择器,显示:

  • 对话开始时间
  • 初始提示或对话摘要
  • 消息数量

使用箭头键导航并按 Enter 选择对话。

💡 提示

  • 对话历史存储在您的本地机器上
  • 使用 --continue 快速访问您最近的对话
  • 当您需要选择特定的过去对话时使用 --resume
  • 恢复时,您将在继续之前看到整个对话历史
  • 恢复的对话以与原始对话相同的模型和配置开始

🔧 工作原理

  • 对话存储:所有对话都会自动保存在本地,包含完整的消息历史
  • 消息反序列化:恢复时,整个消息历史被恢复以保持上下文
  • 工具状态:之前对话中的工具使用和结果得到保留
  • 上下文恢复:对话恢复时保持所有之前的上下文

📝 示例

bash
# 继续最近的对话
claude --continue

# 使用特定提示继续最近的对话
claude --continue --print "Show me our progress"

# 显示对话选择器
claude --resume

# 在非交互模式下继续最近的对话
claude --continue --print "Run the tests again"

🔟 使用 Git worktrees 运行并行 Claude Code 会话

假设您需要同时处理多个任务,并在 Claude Code 实例之间完全隔离代码。

理解 Git worktrees

Git worktrees 允许您将同一存储库的多个分支检出到单独的目录中。每个 worktree 都有自己的工作目录和隔离的文件,同时共享相同的 Git 历史。在官方 Git worktree 文档中了解更多

创建新的 worktree

bash
# 使用新分支创建新的 worktree
git worktree add ../project-feature-a -b feature-a

# 或使用现有分支创建 worktree
git worktree add ../project-bugfix bugfix-123

这会创建一个新目录,其中包含您存储库的单独工作副本。

在每个 worktree 中运行 Claude Code

bash
# 导航到您的 worktree
cd ../project-feature-a
# 在这个隔离环境中运行 Claude Code
claude

在另一个 worktree 中运行 Claude

bash
cd ../project-bugfix
claude

管理您的 worktrees

bash
# 列出所有 worktrees
git worktree list

# 完成后删除 worktree
git worktree remove ../project-feature-a

💡 提示

  • 每个 worktree 都有自己独立的文件状态,非常适合并行 Claude Code 会话
  • 在一个 worktree 中所做的更改不会影响其他 worktree,防止 Claude 实例相互干扰
  • 所有 worktrees 共享相同的 Git 历史和远程连接
  • 对于长时间运行的任务,您可以让 Claude 在一个 worktree 中工作,同时在另一个 worktree 中继续开发
  • 使用描述性目录名称轻松识别每个 worktree 用于哪个任务

⚙️ 环境初始化: 记住根据项目的设置在每个新 worktree 中初始化您的开发环境。根据您的技术栈,这可能包括:

  • JavaScript 项目:运行依赖安装(npm install、yarn)
  • Python 项目:设置虚拟环境或使用包管理器安装
  • 其他语言:遵循项目的标准设置过程

1️⃣1️⃣ 将 Claude 用作 Unix 风格的实用程序

11.1 将 Claude 添加到您的验证过程

假设您想将 Claude Code 用作 linter 或代码审查器。

将 Claude 添加到您的构建脚本:

json
// package.json
{
    "scripts": {
        "lint:claude": "claude -p 'you are a linter. please look at the changes vs. main and report any issues related to typos. report the filename and line number on one line, and a description of the issue on the second line. do not return any other text.'"
    }
}

💡 提示

  • 在您的 CI/CD 管道中使用 Claude 进行自动化代码审查
  • 自定义提示以检查与您项目相关的特定问题
  • 考虑为不同类型的验证创建多个脚本

11.2 管道输入,管道输出

假设您想将数据管道输入到 Claude,并以结构化格式获取数据。

通过 Claude 管道数据:

bash
cat build-error.txt | claude -p 'concisely explain the root cause of this build error' > output.txt

💡 提示

  • 使用管道将 Claude 集成到现有的 shell 脚本中
  • 与其他 Unix 工具结合使用以实现强大的工作流程
  • 考虑使用 --output-format 进行结构化输出

11.3 控制输出格式

假设您需要 Claude 的输出采用特定格式,特别是在将 Claude Code 集成到脚本或其他工具中时。

使用文本格式(默认)

bash
cat data.txt | claude -p 'summarize this data' --output-format text > summary.txt

这只输出 Claude 的纯文本响应(默认行为)。

使用 JSON 格式

bash
cat code.py | claude -p 'analyze this code for bugs' --output-format json > analysis.json

这输出包含成本和持续时间等元数据的消息 JSON 数组。

使用流式 JSON 格式

bash
cat log.txt | claude -p 'parse this log file for errors' --output-format stream-json

这在 Claude 处理请求时实时输出一系列 JSON 对象。每条消息都是有效的 JSON 对象,但如果连接,整个输出不是有效的 JSON。

💡 提示

  • 对于只需要 Claude 响应的简单集成,使用 --output-format text
  • 当您需要完整的对话日志时,使用 --output-format json
  • 对于每个对话轮次的实时输出,使用 --output-format stream-json

1️⃣2️⃣ 创建自定义斜杠命令

Claude Code 支持自定义斜杠命令,您可以创建这些命令来快速执行特定的提示或任务。

12.1 创建项目特定命令

假设您想为您的项目创建可重用的斜杠命令,所有团队成员都可以使用。

  1. 在您的项目中创建命令目录

    bash
    mkdir -p .claude/commands
  2. 为每个命令创建 Markdown 文件

    bash
    echo "Analyze the performance of this code and suggest three specific optimizations:" > .claude/commands/optimize.md
  3. 在 Claude Code 中使用您的自定义命令

    > /project:optimize

💡 提示

  • 命令名称来自文件名(例如,optimize.md 变成 /project:optimize)
  • 您可以在子目录中组织命令(例如,.claude/commands/frontend/component.md 变成 /project:frontend:component)
  • 项目命令对克隆存储库的每个人都可用
  • Markdown 文件内容成为调用命令时发送给 Claude 的提示

12.2 使用 $ARGUMENTS 添加命令参数

假设您想创建可以接受用户额外输入的灵活斜杠命令。

  1. 创建带有 $ARGUMENTS 占位符的命令文件

    bash
    echo "Find and fix issue #\$ARGUMENTS. Follow these steps: 1.Understand the issue described in the ticket 2. Locate the relevant code in our codebase 3. Implement a solution that addresses the root cause 4. Add appropriate tests 5. Prepare a concise PR description" > .claude/commands/fix-issue.md
  2. 使用带有问题编号的命令 在您的 Claude 会话中,使用带参数的命令。

    > /project:fix-issue 123

    这将在提示中将 $ARGUMENTS 替换为 "123"。

💡 提示

  • $ARGUMENTS 占位符被命令后面的任何文本替换
  • 您可以在命令模板中的任何位置放置 $ARGUMENTS
  • 其他有用的应用:为特定函数生成测试用例、为组件创建文档、审查特定文件中的代码或将内容翻译为指定语言

12.3 创建个人斜杠命令

假设您想创建在所有项目中都有效的个人斜杠命令。

  1. 在您的主文件夹中创建命令目录

    bash
    mkdir -p ~/.claude/commands
  2. 为每个命令创建 Markdown 文件

    bash
    echo "Review this code for security vulnerabilities, focusing on:" > ~/.claude/commands/security-review.md
  3. 使用您的个人自定义命令

    > /user:security-review

💡 提示

  • 个人命令以 /user: 而不是 /project: 为前缀
  • 个人命令只对您可用,不与您的团队共享
  • 个人命令在您的所有项目中都有效
  • 您可以使用这些命令在不同代码库中保持一致的工作流程

🎯 总结

通过掌握这些工作流程,您可以充分利用 Claude Code 的强大功能:

  1. 理解代码库 - 快速获取项目概览和查找相关代码
  2. 高效调试 - 系统化地修复错误和问题
  3. 代码重构 - 安全地现代化和改进代码
  4. 测试覆盖 - 添加全面的测试用例
  5. 文档管理 - 创建和维护优质文档
  6. 图像处理 - 利用视觉内容增强理解
  7. 深度思考 - 处理复杂的架构决策
  8. 会话管理 - 有效地恢复和管理对话
  9. 并行工作 - 使用 Git worktrees 处理多个任务
  10. 系统集成 - 将 Claude 集成到开发工具链中
  11. 自定义命令 - 创建可重用的工作流程命令

这些工作流程将帮助您更高效地使用 Claude Code,提升开发生产力。