本文汇总 Claude Code 最实用的日常开发工作流,覆盖陌生代码库探索、高效调试、Plan Mode 安全分析、Git Worktree 并行开发和 Unix 管道集成。每个场景都附有可直接复用的提示词示例。
1. 快速探索陌生代码库
刚加入新项目?五步快速上手:
# 第一步:进入项目目录
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 方案时
- 探索问题的多种解法时
如何启用:
# 方式一:启动时指定
claude --permission-mode plan
# 方式二:会话中切换
Shift+Tab # 在 Auto-Accept / Plan Mode / 普通模式间循环
# 方式三:直接命令
/plan配置为默认模式(~/.claude/settings.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,提升工作质量:
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 实例,互不干扰:
# 快速启动:-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(单会话内并行):
# 当 Claude Code 启动 Subagent 时,自动在独立 worktree 中运行
# 避免多个 Agent 同时修改同一文件手动管理 Worktree:
# 列出所有 worktree
git worktree list
# 清理已合并的 worktree
git worktree remove .claude/worktrees/feature-auth
git worktree prune10. 通知:Claude 完成时提醒你
长任务进行中可以做别的事,完成时获得通知:
# 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 notification11. Unix 管道集成(作为命令行工具使用)
Claude Code 可以完美嵌入 Unix 工具链:
# 分析日志
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控制输出格式:
# 文本格式(默认)
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-json12. 扩展思考模式(Extended Thinking)
对于复杂问题,开启扩展思考让 Claude 在回答前深度推理:
# 快捷键切换(需先运行 /terminal-setup)
Option+T (macOS) / Alt+T (Windows/Linux)
# 或在设置中永久开启
/config → Thinking → Extended适合使用扩展思考的场景:
- 复杂架构设计决策
- 多步骤算法设计
- 安全漏洞分析
- 跨多文件的重构规划
13. 会话管理
# 恢复最近对话
claude -c
# 按名称恢复
claude -r "auth-refactor"
# 在会话中重命名(便于后续查找)
/rename auth-refactor-v2
# 使用会话选择器
/resume # 交互式选择历史会话原文:Common workflows - Claude Code Docs | 来源:Anthropic 官方文档