Appearance
Claude Code 最佳实践和使用技巧
Claude Code 是 Anthropic 推出的智能编程助手,直接集成到终端环境中,能够理解代码库并通过自然语言命令帮助编程。
本文从实践者的角度,系统分享 Claude Code 的最佳实践和使用技巧。
1️⃣ 初始化配置
1.1 安装 Claude Code
bash
# 全局安装
npm install -g @anthropic-ai/claude-code
# 验证安装
claude --version
1.2 创建 CLAUDE.md 文件
CLAUDE.md
是 Claude Code 的核心配置文件,会被自动读取并加入到上下文中。
文件内容应包含:
- 项目基础信息和架构说明
- 常用命令和构建脚本
- 代码规范和约定
- 测试指南
- 开发环境配置
- 其他必要的上下文信息(可通过
@path/to/import
来引用项目文件)
创建方式:
bash
# 使用 /init 命令自动生成
claude> /init
# 或者手动创建并填充内容
touch CLAUDE.md
文件位置优先级:
- 项目根目录:
./CLAUDE.md
(推荐,可提交到 git) - 项目本地:
./CLAUDE.local.md
(不提交到 git) - 全局配置:
~/.claude/CLAUDE.md
- 父目录和子目录中也会被自动读取
1.3 配置工具权限
Claude Code 默认采用保守的权限策略。可以通过四种方式显式授权:
- 启动时的交互式授权提示
- 运行
/permissions
命令 - 手动编辑
.claude/settings.json
- 启动参数
--allowedTools
bash
# 使用 /permissions 命令管理权限
> /permissions
# 或者通过命令行参数
claude --allowedTools Edit,Bash(git commit:*)
推荐允许的工具:
- Edit: 文件编辑
- Bash(git commit:*): Git 提交操作
- WebFetch(*): 访问 URL 网址
1.4 安装配套工具
推荐安装 GitHub CLI,自动化 GitHub 工作流:
bash
# 安装 GitHub CLI
brew install gh
# 配置 GitHub 认证
gh auth login
2️⃣ 工作流程
2.1 探索 - 计划 - 编程 - 提交流程
这是最通用的工作流程,适用于大多数开发任务:
bash
# 1. 探索阶段 - 了解现有代码
"请阅读相关文件了解当前架构,但暂时不要编写代码"
# 2. 计划阶段 - 使用扩展思考(按两次 Shift+TAB 进入 PLAN 模式)
"请 ultrathink 并制定详细的实现计划,并指示 sub-agents 并行验证关键细节"
# 3. 编码阶段
"请按照计划实现功能"
# 4. 提交阶段
"请提交更改并创建 PR"
2.2 测试驱动开发流程
对于可以通过测试验证的功能:
bash
# 1. 编写测试
"请基于期望的输入输出编写测试,确保测试会失败"
# 2. 运行测试确认失败
"运行测试确认失败,不要编写实现代码"
# 3. 提交测试
"请提交测试代码"
# 4. 实现功能
"请编写代码使测试通过,不要修改测试"
# 5. 提交实现
"请提交实现代码"
2.3 UI 开发迭代流程
对于前端开发:
bash
# 1. 提供设计图
# 拖拽图片到 Claude Code 界面
# 2. 实现 UI
"请按照设计图实现界面"
# 3. 截图对比
"请截图当前实现,与设计图对比并改进"
# 4. 迭代优化
"请继续优化,直到效果满意"
3️⃣ 进阶功能和技巧
3.1 使用 MCP 扩展工具
Claude Code 支持 MCP(Model Context Protocol)来扩展功能:
json
// .mcp.json 示例
{
"mcpServers": {
"playwright": {
"args": ["@playwright/mcp@latest", "--headless"],
"command": "npx"
},
"context7": {
"args": ["-y", "@upstash/context7-mcp@latest"],
"command": "npx"
}
}
}
3.2 自定义斜杠命令
在 .claude/commands/
目录创建自定义命令。
示例:创建 issue.md
用于修复 GitHub Issue
markdown
# .claude/commands/issue.md
Please analyze and fix the Github issue: $ARGUMENTS.
Follow these steps:
## PLAN
1. Use 'gh issue view' to get the issue details
2. Understand the problem described in the issue
3. Ask clarifying questions if necessary
4. Understand the prior art for this issue
- Search the scratchpads for previous thoughts related to the issue
- Search PRs to see if you can find history on this issue
- Search the codebase for relevant files
5. Think harder about how to break the issue down into a series of small, manageable tasks.
6. Document your plan in a new scratchpad
- include the issue name in the filename
- include a link to the issue in the scratchpad.
## CREATE
- Create a new branch for the issue
- Solve the issue in small, manageable steps, according to your plan.
- Commit your changes after each step.
## TEST
- Use puppeteer via MCP to test the changes if you have made changes to the UI
- Write unit tests to describe the expected behavior of your code
- Run the full test suite to ensure you haven't broken anything
- If the tests are failing, fix them
- Ensure that all tests are passing before moving on to the next step
## SUBMIT
- Push and open a PR on Github.
Remember to use the GitHub CLI ('gh') for all Github-related tasks.
使用方法:
bash
> /issue 1234
3.3 上下文管理
Claude Code 的上下文窗口有限,且上下文过长会导致幻觉严重,需要合理管理:
管理策略:
- 使用
@
引用文件 - 任务切换时使用
/clear
清空上次任务信息 - 长时间会话定期压缩上下文
- 必要时从历史会话恢复
- 将重要信息通过
# <context>
记录到 CLAUDE.md 中
命令格式:
bash
# 使用文件引用
请参考 @src/components/UserProfile.tsx 的结构
# 压缩上下文
> /compact
# 清理上下文
> /clear
# 增加记忆
# <context>
# 恢复历史会话
/resume
启动时恢复会话:
bash
# 续聊上次会话
claude --continue
claude --continue --print # 适合脚本调用
# 从历史会话中选择再继续
claude --resume
4️⃣ 项目组织和管理
4.1 使用 ROADMAP.md 管理项目
创建项目路线图:
markdown
# 项目开发路线图
## 开发流程
1. **任务规划**
- 分析现有代码基础
- 更新 ROADMAP.md 添加新任务
- 优先级任务插入到最后完成任务之后
2. **任务创建**
- 在 `/tasks` 目录创建详细任务文件
- 命名格式:`XXX-description.md`
- 包含规格说明、相关文件、验收标准
3. **任务实现**
- 按照任务文件中的步骤实现
- 每完成一步更新任务文件进度
- 每步完成后暂停等待进一步指令
## 任务列表
### 已完成 ✅
- **任务 001: 数据库架构** ✅
- 参考: `/tasks/001-database.md`
- 实现了核心表结构和关系
### 进行中 🔄
- **任务 002: 用户界面** 🔄
- 参考: `/tasks/002-ui.md`
- 正在实现登录和注册页面
### 计划中 📋
- **任务 003: API 接口**
- 设计 RESTful API
- 实现用户认证
4.2 任务模板
markdown
# 任务 XXX: 功能描述
## 进度摘要
**状态**: 未开始
- [ ] 步骤 1: 创建组件
- [ ] 步骤 2: 实现逻辑
- [ ] 步骤 3: 编写测试
- [ ] 步骤 4: 集成验证
## 概述
详细描述功能需求和预期效果。
## 当前状态分析
分析现有代码基础和相关文件。
## 目标状态
描述完成后的期望状态。
## 实现步骤
### 步骤 1: 创建组件
详细描述第一步需要做什么。
**涉及文件:**
- `src/components/NewComponent.tsx`
- `src/types/index.ts`
### 步骤 2: 实现逻辑
详细描述第二步需要做什么。
## 验收标准
### 功能要求
- [ ] 功能 A 正常工作
- [ ] 功能 B 正常工作
### 技术要求
- [ ] 通过所有测试
- [ ] 代码符合规范
- [ ] 性能满足要求
## 相关文件
### 新建文件
- `src/components/NewComponent.tsx`
- `src/utils/helper.ts`
### 修改文件
- `src/app/page.tsx`
- `src/types/index.ts`
## 注意事项
- 保持向后兼容性
- 确保响应式设计
- 考虑无障碍性
4.3 并行工作流
当需要同时开发多个功能分支时,可结合 git worktree
使用:
bash
# 创建新的工作树
git worktree add ../feature-branch feature/new-feature
# 在新工作树中启动 Claude Code
cd ../feature-branch
claude
# 管理多个工作树
git worktree list
git worktree remove ../feature-branch
5️⃣ 常用命令速查
基础命令
bash
claude # 启动交互模式
claude "描述任务" # 一次性命令
claude --continue # 继续上次会话
claude --resume # 选择历史会话
交互命令
bash
/init # 初始化项目配置
/permissions # 管理工具权限
/clear # 清空上下文
/compact # 压缩上下文
/resume # 恢复会话
文件操作
bash
@filename # 引用文件
@folder/ # 引用目录
# <context> # 添加上下文标记
自定义命令
bash
/project:command-name # 项目级自定义命令
/user:command-name # 用户级自定义命令
6️⃣ 最佳实践总结
项目初始化
- 创建详细的 CLAUDE.md 配置文件
- 设置合适的工具权限
- 建立项目文档结构
工作流程
- 遵循探索-计划-编程-提交的标准流程
- 对重要功能采用测试驱动开发
- UI 开发采用设计图对比迭代
上下文管理
- 及时清理和压缩上下文
- 合理使用文件引用
- 重要信息记录到配置文件
项目管理
- 使用 ROADMAP.md 规划项目
- 创建详细的任务模板
- 利用 git worktree 并行开发
效率提升
- 创建常用的自定义命令
- 配置 MCP 扩展工具
- 结合 GitHub CLI 自动化流程
通过以上最佳实践,你将能够充分发挥 Claude Code 的强大功能,显著提升开发效率和代码质量。
💡 提示:建议将本指南作为参考手册,在实际开发中根据项目特点调整和优化工作流程。