常见工作流

学习 Claude Code 的常见工作流程。每个任务都包含清晰的说明、示例命令和最佳实践。

理解新代码库

快速了解代码库概览

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

进入项目根目录
cd /path/to/project
启动 Claude Code
claude
请求高层概览
> give me an overview of this codebase
深入了解特定组件
> explain the main architecture patterns used here
> what are the key data models?
> how is authentication handled?
  • 从宽泛的问题开始,然后缩小到特定领域
  • 询问项目中使用的编码约定和模式
  • 请求项目特定术语的词汇表

查找相关代码

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

让 Claude 查找相关文件
> find the files that handle user authentication
了解组件如何交互
> how do these authentication files work together?
理解执行流程
> trace the login process from front-end to database

高效修复 Bug

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

与 Claude 分享错误
> I'm seeing an error when I run npm test
请求修复建议
> suggest a few ways to fix the @ts-ignore in user.ts
应用修复
> update user.ts to add the null check you suggested
  • 告诉 Claude 重现问题的命令以获取堆栈跟踪
  • 提及重现错误的任何步骤
  • 让 Claude 知道错误是间歇性的还是持续的

重构代码

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

识别需要重构的遗留代码
> find deprecated API usage in our codebase
获取重构建议
> suggest how to refactor utils.js to use modern JavaScript features
安全地应用更改
> refactor utils.js to use ES2024 features while maintaining the same behavior
验证重构
> run tests for the refactored code
  • 让 Claude 解释现代方法的好处
  • 在需要时请求保持向后兼容性
  • 以小的、可测试的增量进行重构

使用计划模式进行安全代码分析

计划模式指示 Claude 通过只读操作分析代码库来创建计划,非常适合探索代码库、规划复杂更改或安全地审查代码。

何时使用计划模式

如何使用计划模式

在会话期间使用 Shift+Tab 循环切换权限模式。

或者使用命令行标志启动新会话:

claude --permission-mode plan

让 Claude 采访你

对于大型功能,从最小规格开始,让 Claude 采访你以填写细节:

> Interview me about this feature before you start: user notification system
> Help me think through the requirements for authentication by asking questions
> Ask me clarifying questions to build out this spec: payment processing

处理测试

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

识别未测试的代码
> find functions in NotificationsService.swift that are not covered by tests
生成测试脚手架
> add tests for the notification service
添加有意义的测试用例
> add test cases for edge conditions in the notification service
运行并验证测试
> run the new tests and fix any failures

创建 Pull Request

假设你需要为更改创建一个文档完善的 pull request。

总结你的更改
> summarize the changes I've made to the authentication module
使用 Claude 生成 PR
> create a pr
审查和完善
> enhance the PR description with more context about the security improvements
  • 直接让 Claude 为你创建 PR
  • 在提交前审查 Claude 生成的 PR
  • 让 Claude 突出潜在风险或注意事项

处理图像

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

将图像添加到对话中

你可以使用以下任一方法:

  • 将图像拖放到 Claude Code 窗口中
  • 复制图像并使用 Ctrl+V 粘贴到 CLI
  • 提供图像路径给 Claude,例如:"Analyze this image: /path/to/image.png"
让 Claude 分析图像
> What does this image show?
> Describe the UI elements in this screenshot
> Are there any problematic elements in this diagram?
从视觉内容获取代码建议
> Generate CSS to match this design mockup
> What HTML structure would recreate this component?

引用文件和目录

使用 @ 快速包含文件或目录,无需等待 Claude 读取它们。

引用单个文件
> Explain the logic in @src/utils/auth.js

这会将文件的完整内容包含在对话中。

引用目录
> What's the structure of @src/components?

这会提供带有文件信息的目录列表。


恢复之前的对话

启动 Claude Code 时,你可以恢复之前的会话:

在活动会话中,使用 /resume 切换到不同的对话。

为会话命名

给会话起描述性名称以便稍后找到它们。

> /rename auth-refactor

稍后按名称恢复:

claude --resume auth-refactor

创建自定义斜杠命令

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

创建项目特定命令

在项目中创建命令目录
mkdir -p .claude/commands
为每个命令创建 Markdown 文件
echo "Analyze the performance of this code and suggest three specific optimizations:" > .claude/commands/optimize.md
在 Claude Code 中使用自定义命令
> /optimize
  • 命令名称来自文件名(例如 optimize.md 变成 /optimize
  • 项目命令对克隆仓库的所有人可用
  • Markdown 文件内容成为调用命令时发送给 Claude 的提示

询问 Claude 其功能

Claude 内置了对其文档的访问,可以回答关于其自身功能和限制的问题。

示例问题

> can Claude Code create pull requests?
> how does Claude Code handle permissions?
> what slash commands are available?
> how do I use MCP with Claude Code?

相关文档