本文档将为您介绍 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
提示:
- 在提问时,尽可能具体地描述你要查找的内容。
- 使用项目中的领域特定语言进行提问,以获得更精确的结果。
代码修改与重构
高效修复错误
当你遇到错误信息,需要定位并修复问题时:
- 向 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
提示:
- 提供复现问题所需的命令和完整的堆栈跟踪(Stack Trace)。
- 告知 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 解释新方案的优势。
- 如果需要,可以要求更改保持向后兼容。
- 以小步、可测试的方式进行增量重构。
软件开发全流程覆盖
编写与完善测试用例
为尚未被测试覆盖的代码添加测试:
- 识别未测试的代码
> 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
提示:
- 要求 Claude 覆盖边缘情况和错误条件。
- 根据需要,请求生成单元测试和集成测试。
- 让 Claude 解释其测试策略。
创建高质量的 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
- 补充测试细节
> add information about how these changes were tested
提示:
- 可以直接让 Claude 为你创建 PR。
- 在提交前,务必审查 Claude 生成的 PR 内容。
- 让 Claude 突出潜在的风险或需要考虑的因素。
生成与更新代码文档
为你的代码添加或更新文档:
- 识别缺少文档的代码
> find functions without proper JSDoc comments in the auth module
- 生成文档
> add JSDoc comments to the undocumented functions in auth.js
- 审查并增强文档
> improve the generated documentation with more context and examples
- 验证文档格式
> check if the documentation follows our project standards
提示:
- 指定你想要的文档风格(如 JSDoc、Python docstrings 等)。
- 要求在文档中加入代码示例。
- 为公共 API、接口和复杂逻辑请求详细文档。
高级功能与技巧
使用专门的子代理(Sub-agent)
利用专门的 AI 子代理来更高效地处理特定任务。
-
查看可用的子代理
> /agents
此命令会显示所有可用的子代理,并允许你创建新的子代理。
-
自动委派任务 Claude Code 会自动将合适的任务委派给专门的子代理:
> review my recent code changes for security issues > run all tests and fix any failures
-
明确指定子代理
> use the code-reviewer subagent to check the auth module > have the debugger subagent investigate why users can't log in
-
创建自定义子代理 使用
/agents
命令并选择 “Create New subagent”,然后根据提示定义:- 子代理类型:例如
api-designer
、performance-optimizer
。 - 使用时机:描述何时应激活此子代理。
- 可访问工具:限制其可以使用的工具。
- 专用系统提示:为其设定独特的行为指令。
- 子代理类型:例如
提示:
- 在项目目录的
.claude/agents/
中创建子代理,方便团队共享。 - 使用描述性的
description
字段以启用自动委派。 - 将工具访问权限限制在每个子代理实际需要的最小范围内。
使用计划模式(Plan Mode)进行安全分析
计划模式会指示 Claude 仅通过只读操作来分析代码库并制定计划,非常适合代码探索、规划复杂变更或安全审查。
何时使用计划模式
- 多步骤实现:当你的功能需要修改多个文件时。
- 代码探索:当你想在动手修改前彻底研究代码库时。
- 交互式开发:当你想与 Claude 迭代讨论实现方向时。
如何使用计划模式
-
在会话中切换 使用
Shift+Tab
可以在不同权限模式间循环切换。⏵⏵ accept edits on
(自动接受模式) ->⏸ plan mode on
(计划模式)。 -
以计划模式启动新会话
claude --permission-mode plan
-
在“无头模式”下运行 使用
-p
标志直接在计划模式下运行查询:claude --permission-mode plan -p "Analyze the authentication system and suggest improvements"
示例:规划复杂重构
claude --permission-mode plan
> I need to refactor our authentication system to use OAuth2. Create a detailed migration plan.
Claude 将分析当前实现并生成一份全面的计划。你可以通过后续问题来完善它:
> What about backward compatibility?
> How should we handle database migration?
将计划模式设为默认
在配置文件中进行如下设置:
{
"permissions": {
"defaultMode": "plan"
}
}
利用扩展思考(Extended Thinking)处理复杂任务
当你面对复杂的架构决策、棘手的错误或需要深度推理的多步骤任务时,可以激活 Claude 的扩展思考能力。
-
提供上下文并让 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 > keep thinking about edge cases we should handle
提示:
- 扩展思考最适用于复杂任务,如架构规划、复杂问题调试等。
- 不同的提示词会触发不同深度的思考:
think
:触发基本扩展思考。keep thinking
,think more
,think longer
:触发更深层次的思考。
处理图像输入
当需要分析代码库中的图像内容时,Claude 可以提供帮助。
-
向对话中添加图像
- 将图像文件拖放到 Claude Code 窗口。
- 复制图像并使用
Ctrl+V
(Windows/Linux) 或Cmd+V
(macOS) 粘贴。 - 提供图像的本地路径,例如:
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 设计稿、架构图、数据库模式图等。
- 可以在一次对话中处理多张图像。
快速引用文件与目录
使用 @
符号可以快速将文件或目录的上下文包含到对话中。
-
引用单个文件
> Explain the logic in @src/utils/auth.js
这会将该文件的完整内容注入到当前对话中。
-
引用目录
> What's the structure of @src/components?
这会提供该目录下的文件列表信息。
-
引用 MCP 资源
> Show me the data from @github:repos/owner/repo/issues
这会从已连接的 MCP 服务器获取数据。
提示:
- 文件路径可以是相对路径或绝对路径。
- 引用多个文件:
@file1.js and @file2.js
。
恢复历史对话
Claude Code 提供了两种方式来恢复之前的对话:
-
自动继续最近的对话
claude --continue
-
在非交互模式下继续
claude --continue --print "Continue with my task"
-
显示对话选择器
claude --resume
此命令会显示一个交互式列表,包含对话开始时间、初始提示和消息数,你可以用箭头键选择并恢复。
提示:
- 对话历史记录存储在你的本地机器上。
- 恢复对话时,会保留完整的上下文和工具状态。
集成与自动化
结合 Git 工作树(Worktree)并行会话
如果你需要同时处理多个任务,并希望 Claude Code 实例之间完全隔离,可以使用 Git 工作树。
-
创建新的工作树
git worktree add ../project-feature-a -b feature-a git worktree add ../project-bugfix bugfix-123
这会为同一仓库创建出独立的、位于不同目录的工作副本。
-
在每个工作树中运行 Claude
- 在终端 A:
cd ../project-feature-a claude
- 在终端 B:
cd ../project-bugfix claude
- 在终端 A:
提示:
- 每个工作树的文件状态是独立的,非常适合并行开发。
- 一个工作树中的更改不会影响另一个,避免了 Claude 实例间的干扰。
- 所有工作树共享同一个 Git 历史记录。
作为 Unix 风格的命令行工具
将 Claude 添加到验证流程
你可以将 Claude 作为 Linter 或代码审查工具集成到构建脚本中。
// 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.'"
}
管道化输入与输出
将数据通过管道传递给 Claude,并以结构化格式获取输出。
cat build-error.txt | claude -p 'concisely explain the root cause of this build error' > output.txt
控制输出格式
使用 --output-format
标志来控制输出,方便脚本集成。
- 文本格式 (默认)
claude -p 'summarize this' --output-format text
- JSON 格式
输出包含元数据(如成本、耗时)的消息 JSON 数组。
claude -p 'analyze this code' --output-format json
- 流式 JSON 格式
实时输出一系列 JSON 对象,适用于流式处理。
claude -p 'parse this log' --output-format stream-json
创建自定义斜杠命令
你可以创建自定义的斜杠命令来快速执行特定任务。
创建项目特定命令
- 在项目中创建命令目录
mkdir -p .claude/commands
- 为每个命令创建 Markdown 文件
文件
.claude/commands/optimize.md
的内容如下:Analyze the performance of this code and suggest three specific optimizations:
- 在 Claude Code 中使用
命令
> /optimize
/optimize
会将optimize.md
文件的内容作为提示发送。
使用 $ARGUMENTS
添加命令参数
- 创建带参数的命令文件
文件
.claude/commands/fix-issue.md
内容:Find and fix issue $ARGUMENTS. 1. Understand the issue described in the ticket. 2. Locate the relevant code. 3. Implement a solution. 4. Add tests. 5. Prepare a concise PR description.
- 使用带参数的命令
这会用
> /fix-issue 123
123
替换提示中的$ARGUMENTS
。
创建个人斜杠命令
如果你希望创建在所有项目中都可用的个人命令,请将命令文件放在 ~/.claude/commands
目录下。这些命令仅对你个人可见。
了解 Claude 自身功能
你可以直接向 Claude 提问关于其自身功能和限制的问题,它内置了对其文档的访问能力。
示例问题:
> can Claude Code create pull requests?
> how does Claude Code handle permissions?
> what slash commands are available?
> how do I configure Claude Code for Amazon Bedrock?
> what are the limitations of Claude Code?
提示:
- Claude 始终可以访问最新的 Claude Code 文档。
- 提出具体问题以获得更详细的答案。
👉 如果你需要 ChatGPT 代充 / Claude / Claude Code / 镜像 / 中转 API:
- 购买 / 了解更多:ai4.plus
- 备用入口:kk4099.com