常见工作流程

常见工作流程

使用 Claude Code 探索代码库、修复错误、重构、测试和其他日常任务的分步指南。

理解新代码库

快速获取代码库概览

  1. 导航到项目根目录:cd /path/to/project
  2. 启动 Claude Code:claude
  3. 请求高级概览:give me an overview of this codebase
  4. 深入了解特定组件:
    • explain the main architecture patterns used here
    • what are the key data models?
    • how is authentication handled?

查找相关代码

  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

高效修复错误

  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

重构代码

  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

使用专门的 subagents

  1. 查看可用的 subagents:/agents
  2. 自动使用 subagents:
    • review my recent code changes for security issues
    • run all tests and fix any failures
  3. 明确请求特定的 subagents:
    • use the code-reviewer subagent to check the auth module
    • have the debugger subagent investigate why users can't log in

使用 Plan Mode 进行安全的代码分析

何时使用 Plan Mode

  • 多步骤实现:当您的功能需要对许多文件进行编辑时
  • 代码探索:当您想在更改任何内容之前彻底研究代码库时
  • 交互式开发:当您想与 Claude 迭代方向时

如何使用 Plan Mode

在会话期间打开 Plan Mode:

  • 使用 Shift+Tab 循环切换权限模式
  • Plan Mode 显示 ⏸ plan mode on

在 Plan Mode 中启动新会话:

1
claude --permission-mode plan

在 Plan Mode 中运行无头查询:

1
claude --permission-mode plan -p "Analyze the authentication system and suggest improvements"

将 Plan Mode 配置为默认值

1
2
3
4
5
{
"permissions": {
"defaultMode": "plan"
}
}

使用测试

  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

创建拉取请求

  1. 总结您的更改:summarize the changes I've made to the authentication module
  2. 生成拉取请求:create a pr
  3. 审查和细化:enhance the PR description with more context about the security improvements

处理文档

  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

使用图像

将图像添加到对话中

  1. 将图像拖放到 Claude Code 窗口中
  2. 复制图像并使用 ctrl+v 将其粘贴到 CLI 中
  3. 向 Claude 提供图像路径

要求 Claude 分析图像

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

引用文件和目录

使用 @ 快速包含文件或目录:

  • 引用单个文件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

使用扩展思考(Thinking Mode)

配置 Thinking Mode

范围 如何配置
努力级别 /model 中调整或设置 CLAUDE_CODE_EFFORT_LEVEL
ultrathink 关键字 在提示中的任何地方包含 “ultrathink”
切换快捷键 Option+T(macOS)或 Alt+T(Windows/Linux)
全局默认值 使用 /config 切换 Thinking Mode
限制令牌预算 设置 MAX_THINKING_TOKENS 环境变量

恢复以前的对话

  • claude --continue 继续当前目录中最近的对话
  • claude --resume 打开对话选择器或按名称恢复
  • claude --from-pr 123 恢复链接到特定拉取请求的会话

命名您的会话

1
/rename auth-refactor

会话选择器快捷键

快捷键 操作
/ 在会话之间导航
Enter 选择并恢复突出显示的会话
R 重命名突出显示的会话
/ 搜索以过滤会话

使用 Git worktrees 运行并行会话

1
2
3
4
5
# 在名为 "feature-auth" 的 worktree 中启动 Claude
claude --worktree feature-auth

# 在单独的 worktree 中启动另一个会话
claude --worktree bugfix-123

获得通知

配置 Notification hook 在 Claude 需要您的注意时获得通知:

  1. 输入 /hooks 并选择 Notification
  2. 选择 + Match all (no filter)
  3. 添加您的通知命令(macOS 示例):
    1
    osascript -e 'display notification "Claude Code needs your attention" with title "Claude Code"'

将 Claude 用作 unix 风格的实用程序

管道数据

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

控制输出格式

  • 文本格式(默认)--output-format text
  • JSON 格式--output-format json
  • 流式 JSON 格式--output-format stream-json

询问 Claude 其功能

Claude 可以回答关于其自身功能的问题:

  • can Claude Code create pull requests?
  • how does Claude Code handle permissions?
  • what skills are available?
  • how do I use MCP with Claude Code?

本文档来自 Claude Code 官方文档

© 2026 Generative AI Discovery All Rights Reserved.
Theme by hiero