
Claude Code 是 Anthropic 推出的 AI 编程助手 CLI 工具,本使用教程将带你从安装到精通。学完这篇 Claude Code 使用教程,你将掌握从基础操作到高级技巧的完整技能。
目录
- 一、Claude Code 使用教程:安装与配置
- 二、Claude Code 登录与认证
- 三、Claude Code 基础命令使用
- 四、Claude Code 代码编辑实战
- 五、Claude Code Git 操作教程
- 六、Claude Code 国内使用技巧
- 七、常见问题与解决方案
- 八、总结与进阶学习
一、Claude Code 使用教程:安装与配置
三种安装方式详解
根据官方 Claude Code 使用教程,有 3 种安装方法。
方法 1:Native Install(推荐)
macOS / Linux / WSL:
curl -fsSL https://claude.ai/install.sh | bash
Windows PowerShell:
irm https://claude.ai/install.ps1 | iex
Windows CMD:
curl -fsSL https://claude.ai/install.cmd -o install.cmd && install.cmd && del install.cmd
方法 2:Homebrew(macOS 用户)
brew install --cask claude-code
方法 3:NPM(需要 Node.js 18+)
npm install -g @anthropic-ai/claude-code
安装验证:
# 验证安装成功
claude --version
# 应该显示类似:
# claude-code version 1.x.x
系统要求
| 项目 | 要求 |
|---|---|
| 操作系统 | macOS, Linux, Windows (需 WSL) |
| Node.js | v18.0 或更高版本(NPM 安装时) |
| 网络 | 需访问 claude.ai(国内需特殊配置) |
| 账户 | Claude.ai 订阅或 Console API |
二、Claude Code 登录与认证
首次登录流程
按照这个 Claude Code 使用教程的步骤登录:
Step 1:启动 Claude Code
cd /path/to/your/project
claude
Step 2:选择登录方式
首次启动时,Claude Code 会提示:
Welcome to Claude Code!
Please login to continue:
1. Claude.ai (Subscription plans)
2. Claude Console (API access with prepaid credits)
Select option [1-2]:
Step 3:浏览器认证
选择选项后,Claude Code 会:
- 打开浏览器窗口
- 跳转到认证页面
- 完成登录后自动返回 CLI
Step 4:验证登录状态
# 登录成功后,命令行显示:
> Successfully logged in!
> Ready to help with your code.
账户类型对比
Claude.ai 账户(推荐):
- ✅ 订阅计划($20/月 Pro 或 $200/月 Max)
- ✅ 额度充足
- ✅ 适合个人开发者
Claude Console 账户:
- ✅ API 预付费额度
- ✅ 按使用量计费
- ✅ 适合企业团队
Easy Claude Code 拼车方案(性价比之选):
- 💰 大月卡(3人拼车):¥799/月,$45/日额度
- 💰 小月卡(5人拼车):¥399/月,$20/日额度
- 💰 相比官方节省 60%-80% 成本
- 👉 立即查看价格对比
切换账户
如果你有多个账户,使用 /login 命令切换:
# 在 Claude Code 会话中输入
/login
# 按提示选择账户类型并重新认证
三、Claude Code 基础命令使用
启动与退出
启动交互模式:
claude
运行一次性任务:
claude "fix the build error"
继续上次对话:
claude -c
退出 Claude Code:
# 方法 1:输入 exit
> exit
# 方法 2:按 Ctrl+C
核心斜杠命令
这些是最常用的 Claude Code 使用教程 命令:
| 命令 | 功能 | 示例 |
|---|---|---|
/help |
显示所有可用命令 | > /help |
/clear |
清除对话历史 | > /clear |
/login |
切换登录账户 | > /login |
/commit |
创建 Git 提交 | > /commit |
/reset |
重置会话状态 | > /reset |
提问技巧(初学者必读)
❌ 模糊提问:
fix the bug
✅ 具体描述:
fix the login bug where users see a blank screen after entering wrong credentials
❌ 一次性大任务:
build a complete e-commerce website with payment system
✅ 分步骤进行:
1. create a new database table for user profiles
2. create an API endpoint to get and update user profiles
3. build a webpage that allows users to see and edit their information
四、Claude Code 代码编辑实战
基础编辑操作
场景 1:添加新功能
> add a hello world function to the main file
Claude Code 会:
- 自动找到项目的主文件(如
index.js、main.py) - 显示建议的代码更改
- 请求你的批准(显示 diff)
- 批准后进行编辑
场景 2:修复 Bug
> there's a bug where users can submit empty forms - fix it
Claude Code 会:
- 分析表单验证逻辑
- 定位问题代码
- 提供修复方案(如添加前端和后端验证)
- 更新相关文件
代码重构
> refactor the authentication module to use async/await instead of callbacks
实际案例(重构前):
function login(username, password, callback) {
db.findUser(username, function(err, user) {
if (err) return callback(err);
bcrypt.compare(password, user.hash, function(err, match) {
if (err) return callback(err);
callback(null, match);
});
});
}
Claude Code 重构后:
async function login(username, password) {
const user = await db.findUser(username);
const match = await bcrypt.compare(password, user.hash);
return match;
}
编写测试
> write unit tests for the calculator functions
Claude Code 生成的测试示例:
// calculator.test.js
import { add, subtract, multiply, divide } from './calculator';
describe('Calculator', () => {
test('add should sum two numbers', () => {
expect(add(2, 3)).toBe(5);
});
test('divide should throw error on division by zero', () => {
expect(() => divide(5, 0)).toThrow('Cannot divide by zero');
});
});
批准模式
单次批准(默认):
- 每次更改都需要你按
y批准 - 适合关键代码修改
自动批准模式:
# 在会话中输入
/approve-all
# 后续所有更改自动批准(谨慎使用)
五、Claude Code Git 操作教程
查看更改
> what files have I changed?
输出示例:
Modified files:
M src/auth.js
M tests/auth.test.js
A docs/authentication.md
创建提交
自动生成 commit 消息:
> commit my changes with a descriptive message
Claude Code 会:
- 分析代码更改
- 生成符合 Conventional Commits 的消息
- 显示预览
- 执行
git commit
生成的 commit 示例:
feat(auth): implement async/await login flow
- Refactored login function to use async/await
- Added error handling for database queries
- Updated unit tests to match new async pattern
分支管理
# 创建新分支
> create a new branch called feature/user-profiles
# 切换分支
> switch to the main branch
# 查看当前分支
> what branch am I on?
合并冲突解决
> help me resolve merge conflicts
Claude Code 会:
- 检测冲突文件
- 分析冲突内容
- 提供解决建议
- 自动修复(如果可能)
冲突示例:
<<<<<<< HEAD
const API_URL = 'https://prod.api.com';
=======
const API_URL = 'https://dev.api.com';
>>>>>>> feature/api-update
Claude Code 建议:
Conflict detected in config.js:
- HEAD: Production API URL
- feature/api-update: Development API URL
Suggested resolution: Use environment variable
const API_URL = process.env.API_URL || 'https://dev.api.com';
六、Claude Code 国内使用技巧
网络问题解决方案
国内用户使用 Claude Code 时,常遇到网络限制。根据这份 Claude Code 使用教程,有 3 种解决方案:
方案 1:使用 Easy Claude Code 拼车服务(推荐)
优势:
- ✅ 无需代理:直接使用,无需 VPN
- ✅ 开箱即用:配置简单,10 分钟开通
- ✅ 成本低廉:¥399/月起,相比官方节省 60%+
配置步骤:
# Step 1: 订阅 Easy Claude Code(https://easyclaude.com)
# Step 2: 获取专属 API Key
# Step 3: 设置环境变量
export ANTHROPIC_AUTH_TOKEN="sk-xxxxxxxxxxxxxxxxxx"
export ANTHROPIC_BASE_URL="https://api.easyclaude.com"
# Step 4: 启动 Claude Code
claude
注意事项:
- ⚠️ 免费额度有限(通常 $100 初始额度)
- ⚠️ 服务稳定性不如官方和 Easy Claude Code
- ⚠️ 可能存在数据安全风险
方案 3:传统 VPN 方案
# 确保 VPN 已连接
# 无需额外配置,直接使用官方服务
claude
缺点:
- ❌ VPN 成本高(月费 $10-30)
- ❌ 网络不稳定,影响使用体验
- ❌ 仍需支付官方订阅费($20-200/月)
成本对比
| 方案 | 月费用 | 网络要求 | 稳定性 | 推荐度 |
|---|---|---|---|---|
| Easy Claude Code | ¥399 起 | 无需 VPN | ⭐⭐⭐⭐⭐ | ⭐⭐⭐⭐⭐ |
| Claude 官方 | ¥1,400+ | 需 VPN | ⭐⭐⭐⭐⭐ | ⭐⭐⭐ |
| API 中转 | 免费-¥200 | 无需 VPN | ⭐⭐⭐ | ⭐⭐ |
| VPN + 官方 | ¥1,600+ | VPN 不稳定 | ⭐⭐⭐ | ⭐ |
七、常见问题与解决方案
Q1: Claude Code 启动后一直加载
原因:网络连接问题或认证失败。
解决方法:
# Step 1: 检查网络
ping claude.ai
# Step 2: 重新登录
claude
> /login
# Step 3: 如果仍失败,清除缓存
rm -rf ~/.claude/cache
claude
Q2: "Permission denied" 错误
原因:Claude Code 没有文件修改权限。
解决方法:
# 在会话中启用自动批准(临时)
> /approve-all
# 或手动批准每次更改(推荐)
> y # 当 Claude Code 请求批准时
Q3: 如何在 WSL (Windows) 中使用?
安装步骤:
# 1. 打开 WSL 终端
wsl
# 2. 安装 Node.js(如果未安装)
curl -fsSL https://deb.nodesource.com/setup_18.x | sudo -E bash -
sudo apt-get install -y nodejs
# 3. 安装 Claude Code
npm install -g @anthropic-ai/claude-code
# 4. 启动
claude
Q4: 如何限制 Claude Code 的文件访问范围?
方法:使用项目特定的 .claudeignore 文件(类似 .gitignore)。
# 创建 .claudeignore
touch .claudeignore
# 添加要忽略的文件/目录
echo "node_modules/" >> .claudeignore
echo ".env" >> .claudeignore
echo "dist/" >> .claudeignore
Q5: Claude Code 会消耗多少额度?
实测数据(基于 Easy Claude Code 用户反馈):
| 任务类型 | 平均消耗 | 示例 |
|---|---|---|
| 简单问答 | $0.01-0.05 | "解释这个函数" |
| 代码编辑 | $0.10-0.30 | "添加用户登录功能" |
| 项目重构 | $0.50-2.00 | "重构整个模块" |
| 生成测试 | $0.20-0.50 | "为所有函数生成测试" |
Easy Claude Code 大月卡($45/日额度)可支持:
- 每日约 150-450 次简单操作
- 或 15-45 次中等复杂任务
- 或 5-10 次大型重构
八、总结与进阶学习
核心要点
通过这篇 Claude Code 使用教程,你已掌握:
- ✅ 3 种安装方法:Native、Homebrew、NPM
- ✅ 登录认证:Claude.ai 和 Console 账户
- ✅ 基础命令:启动、退出、斜杠命令
- ✅ 代码编辑:添加功能、修 Bug、重构
- ✅ Git 操作:提交、分支、冲突解决
- ✅ 国内使用:Easy Claude Code 拼车方案
- ✅ 常见问题:网络、权限、WSL、额度
立即开始使用
🎯 3 步快速上手:
Step 1:安装 Claude Code
curl -fsSL https://claude.ai/install.sh | bash
Step 2:选择方案
- 官方方案:$20-200/月 + VPN 费用
- Easy Claude Code:¥399-1899/月,无需 VPN
- 👉 查看完整价格对比
Step 3:开始使用
cd your-project
claude
> help me understand this codebase
🚀 升级体验
Easy Claude Code 拼车服务优势:
- 💰 大月卡:¥799/月,$45/日额度
- 💰 小月卡:¥399/月,$20/日额度
- ✅ 无需 VPN:国内直连
- ✅ 10 分钟开通:即买即用
- ✅ 7x24 客服支持:问题快速解决
- 👉 立即订阅
进阶学习资源
相关推荐阅读
关于 Easy Claude Code
Easy Claude Code 是专业的 Claude Code 拼车服务平台,提供从个人到企业的多种套餐。相比官方节省 60%-80% 成本,无需 VPN,10 分钟开通。了解更多
🎁 特别优惠:
- 新用户:添加客服微信获取 体验日卡 ¥6.9(原价 ¥9.9)
- 团队采购(3人以上):额外 9.5 折