实战

Claude Code 日常工作流实战:探索代码库、Plan Mode、Git Worktree 并行开发与管道集成

Claude Code 13 大日常工作流实战:五步探索陌生代码库(从宏观到微观的提示词序列)、Plan Mode 安全规划(--permission-mode plan/Shift+Tab 切换/配置为默认)、专用 Subagent 配置、TDD 测试工作流、Git Worktree 并行开发(-w flag 自动创建/两终端并行任务)、Unix 管道集成(git diff | claude -p/--output-format json/CI 成本控制),以及扩展思考模式和会话管理。

2026/3/56分钟 阅读ClaudeEagle

本文汇总 Claude Code 最实用的日常开发工作流,覆盖陌生代码库探索、高效调试、Plan Mode 安全分析、Git Worktree 并行开发和 Unix 管道集成。每个场景都附有可直接复用的提示词示例。

1. 快速探索陌生代码库

刚加入新项目?五步快速上手:

bash
# 第一步:进入项目目录
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? > find the files that handle user authentication > how do these authentication files work together? > trace the login process from front-end to database

技巧

  • 使用项目专用的领域词汇,Claude 会更准确地定位相关代码
  • 安装代码智能插件(支持「跳转到定义」和「查找引用」)可显著提升 Claude 的导航能力

2. 高效调试 Bug

# 场景:npm test 报错 > I'm seeing an error when I run npm test > what's the most likely cause and how should I fix it? > apply the fix

调试技巧

  • 直接粘贴错误堆栈到对话中(Ctrl+V!cat error.log
  • 让 Claude 解释根本原因,而不只是给出修复——更有助于学习
  • 修复后立即跟一句:run the tests again and confirm it's fixed

3. Plan Mode:先规划,后执行

Plan Mode 让 Claude 只分析和规划,不实际修改任何文件

何时使用 Plan Mode

  • 处理复杂重构或架构变更前
  • 不确定改动影响范围时
  • 需要让同事 Review 方案时
  • 探索问题的多种解法时

如何启用

bash
# 方式一:启动时指定
claude --permission-mode plan

# 方式二:会话中切换
Shift+Tab  # 在 Auto-Accept / Plan Mode / 普通模式间循环

# 方式三:直接命令
/plan

配置为默认模式~/.claude/settings.json):

json
{
  "defaultPermissionMode": "plan"
}

复杂重构规划示例

> I need to migrate our authentication system from JWT to session-based auth. > Enter plan mode and: > 1. Identify all files that would need to change > 2. Outline the migration steps > 3. Flag any breaking changes or risks > Don't modify any files yet

Claude 会列出完整的变更计划,你审核后再让它执行:looks good, now implement the plan

4. 专用 Subagent

创建专注特定任务的 Subagent,提升工作质量:

bash
claude --agents '{
  "code-reviewer": {
    "description": "代码审查员,代码修改后自动调用",
    "prompt": "你是资深代码审查员,重点关注:代码质量、安全漏洞、测试覆盖、性能问题。对每个发现的问题给出严重程度(high/medium/low)。",
    "tools": ["Read", "Grep", "Glob"]
  }
}'

5. 测试工作流

# 让 Claude 先写测试,再实现功能(TDD 风格) > write tests for the user registration flow > now implement the code to make these tests pass # 让 Claude 补充测试覆盖 > what test cases are we missing for the payment module? > add tests for these edge cases # 运行并修复测试 > run the tests and fix any failures

6. 创建 Pull Request

# 让 Claude 总结改动并生成 PR 描述 > create a pull request for these changes # 或者分步骤 > summarize what changes I've made > generate a PR description following our team's template > run git commit with a conventional commit message

7. 文档处理

# 生成文档 > generate JSDoc comments for all exported functions in src/api/ # 更新 README > update the README to reflect the new authentication flow # 解释代码 > explain this function in plain English and add a comment block

8. 处理图片

# 粘贴截图(Ctrl+V) > here's a screenshot of the UI bug — identify the issue and fix it # 引用图片文件 > @/path/to/mockup.png implement this design in React

9. Git Worktree 并行开发

Git Worktree 让你在同一仓库的不同分支上同时运行多个 Claude Code 实例,互不干扰

bash
# 快速启动:-w 自动创建 worktree
claude -w feature-auth
# 等同于在 .claude/worktrees/feature-auth 创建并切换到新 worktree

# 同时打开第二个终端
claude -w bug-fix-payment

两个终端并行工作

终端 1(feature-auth) 终端 2(bug-fix-payment) > implement OAuth2 login | > fix the payment timeout bug > add unit tests | > add regression test > create PR | > create hotfix PR

Subagent Worktree(单会话内并行):

bash
# 当 Claude Code 启动 Subagent 时,自动在独立 worktree 中运行
# 避免多个 Agent 同时修改同一文件

手动管理 Worktree

bash
# 列出所有 worktree
git worktree list

# 清理已合并的 worktree
git worktree remove .claude/worktrees/feature-auth
git worktree prune

10. 通知:Claude 完成时提醒你

长任务进行中可以做别的事,完成时获得通知:

bash
# macOS 系统通知
claude --append-system-prompt "When you complete the task, run: \
osascript -e 'display notification \"Task complete\" with title \"Claude Code\"'"

# 或者在会话中
> after you're done, send me a desktop notification

11. Unix 管道集成(作为命令行工具使用)

Claude Code 可以完美嵌入 Unix 工具链:

bash
# 分析日志
cat server.log | claude -p "identify the most frequent errors and suggest fixes"

# 代码审查
git diff | claude -p "review this diff for security issues"

# 批量处理文件
ls *.py | claude -p "which of these files might have SQL injection vulnerabilities?"

# 结构化输出(适合脚本化)
cat api-spec.json | claude -p "generate TypeScript types" --output-format json

# 加入 CI/CD 验证
git diff HEAD~1 | claude -p "does this change break any API contracts?" \
  --max-turns 3 --max-budget-usd 0.10

控制输出格式

bash
# 文本格式(默认)
claude -p "summarize this" --output-format text

# JSON 格式(脚本友好)
claude -p "extract all function names" --output-format json

# 流式 JSON(实时处理)
claude -p "analyze this log" --output-format stream-json

12. 扩展思考模式(Extended Thinking)

对于复杂问题,开启扩展思考让 Claude 在回答前深度推理:

bash
# 快捷键切换(需先运行 /terminal-setup)
Option+T (macOS) / Alt+T (Windows/Linux)

# 或在设置中永久开启
/config → Thinking → Extended

适合使用扩展思考的场景:

  • 复杂架构设计决策
  • 多步骤算法设计
  • 安全漏洞分析
  • 跨多文件的重构规划

13. 会话管理

bash
# 恢复最近对话
claude -c

# 按名称恢复
claude -r "auth-refactor"

# 在会话中重命名(便于后续查找)
/rename auth-refactor-v2

# 使用会话选择器
/resume  # 交互式选择历史会话

原文:Common workflows - Claude Code Docs | 来源:Anthropic 官方文档

相关文章推荐

实战Claude Code Hooks 实战:每次保存自动格式化、拦截危险命令、桌面通知Claude Code Hooks 实战教程:五个即用示例(桌面通知/文件自动格式化/危险命令拦截/压缩后上下文注入/配置变更审计)、Hook 配置位置(全局/项目/本地)、退出码含义(允许/上下文/阻止)、七大 Hook 事件速查表、Prompt-based AI 判断 Hook 进阶用法。2026/3/14实战实战者说:Harper Reed 分享 Claude Code 真实工作流——TDD、预提交钩子与 prompt_planHarper Reed(奥巴马 2012 竞选技术总监)分享 Claude Code 真实工作流:GPT-4o 打磨想法→推理模型生成 spec.md→生成 prompt_plan.md→Claude 自动执行计划,配合 TDD、Linting 和 Pre-commit Hooks 三大防御性实践,实现 30-45 分钟完成完整开发计划。2026/3/2实战从 Cursor 用户到 Claude Code 深度用户:最实用的使用技巧合集Builder.io CTO Steve Sewell 从 Cursor 切换到 Claude Code 的实战经验:跳过权限提示的正确姿势、终端非直觉操作大全、消息队列让 Claude 24/7 工作、让 Claude 自动建立项目配置、自定义斜杠命令,以及处理 18000 行超大文件的独特能力。2026/2/28实战Harper Reed 的 Claude Code 实战工作流:Spec 驱动开发 + TDD + 提交计划的黄金组合Harper Reed 分享的 Claude Code Spec 驱动开发工作流:用推理模型(o1/o3)生成规格说明和提示计划,Claude Code 按计划逐步执行并自动追踪进度。配合 TDD、Linting、Pre-commit Hooks 三大防御性编码实践,团队在 30-45 分钟内完成新功能开发。2026/2/28实战Claude Code 命令行工具开发实战:用 AI 快速构建专业 CLI 工具Claude Code 辅助命令行工具(CLI)开发的完整实战指南:Python Click/Typer、Go Cobra、Rust Clap 技术栈选型、用 Claude Code 生成完整 CLI 项目结构(参数解析/子命令/全局选项)、交互式提示和彩色输出、配置文件管理、Shell 自动补全生成、跨平台打包(PyInstaller/goreleaser),以及发布到 PyPI/npm/Homebrew 的完整流程。2026/3/26实战Claude Code Vue 3 实战完全指南:Composition API 开发到企业级前端工程化Claude Code 辅助 Vue 3 开发的完整实战指南:Composition API 组件生成(setup/ref/computed)、Pinia 状态管理代码生成、Vue Router 4 路由配置、TypeScript 类型定义生成(Props/Emits)、Composables 抽象、Vitest 单元测试生成、性能优化(虚拟滚动/v-memo),以及 Options API 迁移和响应式丢失问题排查的 Prompt 模板。2026/3/26