返回资料库 Claude Code 专题

Claude Code 常见工作流

give me an overview of this codebase

来源:code.claude.com/docs/en/common-workflows | 整理时间:2026-04-01


1. 探索新代码库

快速了解项目概况

cd /path/to/project
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?
what conventions does this project use?

找到特定功能代码

find the files that handle user authentication
how do these authentication files work together?
trace the login process from front-end to database

技巧

  • 从宽泛问题开始,逐步聚焦
  • 安装代码智能插件获得精确的"跳转到定义"能力

2. 高效修 Bug

# 给 Claude 完整的错误信息
I'm seeing an error when I run npm test
# [粘贴完整错误栈]

# 让 Claude 分析并提供方案
suggest a few ways to fix the @ts-ignore in user.ts

# 应用修复
update user.ts to add the null check you suggested

技巧

  • 提供重现步骤
  • 说明是否是偶发性问题
  • 提供完整的错误栈,不要截断

3. 代码重构

# 找到需要重构的代码
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 解释新方式的优势

4. 使用 Plan Mode 安全分析

何时使用 Plan Mode

  • 多文件改动的复杂任务
  • 还没弄清楚方向时的代码探索
  • 在改动前想充分理解代码

如何使用

# 启动 Plan Mode
claude --permission-mode plan

# 或者在会话中按 Shift+Tab 切换

# Plan Mode 中分析
I need to refactor our authentication system to use OAuth2. 
Create a detailed migration plan.

# 按 Ctrl+G 在文本编辑器中编辑计划
# 满意后切回 Normal Mode 执行

配置为默认模式

// .claude/settings.json
{
  "permissions": {
    "defaultMode": "plan"
  }
}

5. 编写和运行测试

# 找到没有测试的代码
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 会自动匹配你项目现有的测试风格、框架和断言模式。


6. 创建 Pull Request

# 直接一句话
create a pr for my changes

# 或者分步骤
summarize the changes I've made to the authentication module
create a pr
enhance the PR description with more context about the security improvements

创建 PR 后,会话会自动关联到该 PR。之后可以用 claude --from-pr 123 继续该会话。


7. 并行会话(Git Worktrees)

同时进行多个独立任务,互不干扰:

# 在独立 worktree 中开始任务
claude --worktree feature-auth
claude --worktree bugfix-login

# 清理(有修改时会提示保留或删除)
# 无修改时自动清理

.gitignore 中排除 worktree:

.claude/worktrees/

.env 等文件复制到 worktree:

# .worktreeinclude 文件
.env
.env.local
config/secrets.json

8. 恢复历史会话

claude --continue     # 继续最近的会话
claude --resume       # 打开会话选择器
claude -n oauth-work  # 给当前会话命名

# 在会话内
/resume               # 打开选择器
/rename new-name      # 重命名当前会话

9. 实时通知

当 Claude 需要输入时,发送桌面通知:

// ~/.claude/settings.json
{
  "hooks": {
    "Notification": [
      {
        "matcher": "",
        "hooks": [{
          "type": "command",
          "command": "osascript -e 'display notification \"Claude Code needs attention\" with title \"Claude Code\"'"
        }]
      }
    ]
  }
}

10. 定时任务

让 Claude 在后台自动执行定期任务:

方式 适合场景
云端定时任务 电脑关机也能运行,在 claude.ai/code 配置
桌面定时任务 需要访问本地文件或工具
GitHub Actions 与 CI/CD 流程结合
/loop 仅在当前会话中轮询

示例定时任务 prompt:

Review open PRs labeled `needs-review`, 
leave inline comments on any issues, 
and post a summary in the #eng-reviews Slack channel.

11. Unix 管道集成

# 管道输入
cat build-error.txt | claude -p 'concisely explain the root cause' > output.txt

# 结构化输出(用于脚本)
cat code.py | claude -p 'analyze for bugs' --output-format json > analysis.json

# 实时流式输出
cat log.txt | claude -p 'parse errors' --output-format stream-json

12. 使用子 Agent

# 让 Claude 用子 Agent 探索
Use subagents to investigate how authentication handles token refresh

# 审查代码
use a subagent to review this code for edge cases

# 创建自定义子 Agent
/agents  # 查看/创建 agent

自定义子 Agent(.claude/agents/security-reviewer.md):

---
name: security-reviewer
description: Reviews code for security vulnerabilities
tools: Read, Grep, Glob, Bash
model: opus
---
You are a senior security engineer...

相关链接

常见问题

Claude Code 常见工作流 适合什么读者?

Claude Code 常见工作流 适合希望系统掌握 Claude Code 专题 的读者,尤其是需要从概念快速过渡到实践的人。页面包含主题摘要、相关阅读和来源链接,便于形成可执行的学习路径。

阅读 Claude Code 常见工作流 需要多久?

当前页面预估阅读时长约 8 分钟。建议先读正文结论,再根据“同专题延伸”继续阅读,通常 20 到 40 分钟可以建立完整主题框架。

如何把 Claude Code 常见工作流 的内容用于实际项目?

先按正文中的关键概念完成最小可运行示例,再把示例嵌入你当前项目流程。你可以结合来源链接验证细节,并使用同专题文章补齐部署、协作和评估步骤。