CClaude Code 中文教程
学习路线 / quickstart

快速开始

官方中文文档· 查看原文 ↗ ·官方译文,未经机器翻译

欢迎使用 Claude Code!

本快速开始指南将在几分钟内让您使用 AI 驱动的编码辅助。完成本指南后,您将了解如何使用 Claude Code 完成常见的开发任务。

提示

默认配置下,Claude Code 需要能够访问 claude.ai 和 Anthropic API 等端点才能完成安装、登录和正常使用。在中国大陆的网络环境中,这些端点可能无法直接访问。开始前,请先确认所在网络能够连通这些服务。企业代理配置以及 Amazon Bedrock 等第三方提供商的网络要求,请参阅网络配置

开始前

确保您拥有:

提示

本指南涵盖终端 CLI。Claude Code 也可在网页桌面应用VS CodeJetBrains IDESlack 中使用,以及通过 GitHub ActionsGitLab 进行 CI/CD。查看所有界面

步骤 1:安装 Claude Code

To install Claude Code, use one of the following methods:

macOS, Linux, WSL:

```bash theme={null}
curl -fsSL https://claude.ai/install.sh | bash
```

**Windows PowerShell:**

```powershell theme={null}
irm https://claude.ai/install.ps1 | iex
```

**Windows CMD:**

```batch theme={null}
curl -fsSL https://claude.ai/install.cmd -o install.cmd && install.cmd && del install.cmd
```

If you see `The token '&&' is not a valid statement separator`, you're in PowerShell, not CMD. If you see `'irm' is not recognized as an internal or external command`, you're in CMD, not PowerShell. Your prompt shows `PS C:\` when you're in PowerShell and `C:\` without the `PS` when you're in CMD.

If the install command fails with `syntax error near unexpected token '<'`, a `403`, or another curl error, see [Troubleshoot installation](/docs/en/troubleshoot-install#find-your-error) to match the error to a fix and for alternative install methods.

[Git for Windows](https://git-scm.com/downloads/win) is recommended on native Windows so Claude Code can use the Bash tool. If Git for Windows is not installed, Claude Code uses PowerShell as the shell tool instead. WSL setups do not need Git for Windows.
说明

Native installations automatically update in the background to keep you on the latest version.

    brew install --cask claude-code
    ```

    Homebrew offers two casks. `claude-code` tracks the stable release channel, which is typically about a week behind and skips releases with major regressions. `claude-code@latest` tracks the latest channel and receives new versions as soon as they ship.

    

说明

Homebrew installations do not auto-update. Run brew upgrade claude-code or brew upgrade claude-code@latest, depending on which cask you installed, to get the latest features and security fixes.

    winget install Anthropic.ClaudeCode
    ```

    

说明

WinGet installations do not auto-update. Run winget upgrade Anthropic.ClaudeCode periodically to get the latest features and security fixes.

You can also install with apt, dnf, or apk on Debian, Fedora, RHEL, and Alpine.

步骤 2:登录您的账户

Claude Code 需要账户才能使用。使用 claude 命令启动交互式会话,首次使用时系统会提示您登录:

claude

对于 Claude 订阅或 Console 账户,请按照提示在浏览器中完成身份验证。要稍后切换账户或重新身份验证,请在运行的会话中输入 /login

/login

您可以使用以下任何账户类型登录:

登录后,您的凭证将被存储,您无需再次登录。

步骤 3:启动您的第一个会话

在任何项目目录中打开您的终端并启动 Claude Code:

cd /path/to/your/project
claude

您将看到 Claude Code 提示符,其中显示版本、当前模型和上方显示的工作目录。输入 /help 查看可用命令,或输入 /resume 继续之前的对话。

建议

登录后(步骤 2),您的凭证将存储在您的系统上。在凭证管理中了解更多信息。

步骤 4:提出您的第一个问题

让我们从理解您的代码库开始。尝试以下命令之一:

what does this project do?

Claude 将分析您的文件并提供摘要。您也可以提出更具体的问题:

what technologies does this project use?
where is the main entry point?
explain the folder structure

您也可以询问 Claude 关于其自身功能的问题:

what can Claude Code do?
how do I create custom skills in Claude Code?
can Claude Code work with Docker?
提示

Claude Code 根据需要读取您的项目文件。您不必手动添加上下文。

步骤 5:进行您的第一次代码更改

现在让我们让 Claude Code 进行一些实际的编码。尝试一个简单的任务:

在主文件中添加一个 hello world 函数

Claude Code 将:

  1. 找到适当的文件
  2. 向您显示建议的更改
  3. 请求您的批准
  4. 进行编辑
提示

Claude Code 在修改文件前始终请求许可。您可以批准单个更改或为会话启用"全部接受"模式。

步骤 6:在 Claude Code 中使用 Git

Claude Code 使 Git 操作变得对话式:

我更改了哪些文件?
用描述性消息提交我的更改

您也可以提示更复杂的 Git 操作:

创建一个名为 feature/quickstart 的新分支
显示我最后的 5 次提交
帮我解决合并冲突

步骤 7:修复错误或添加功能

Claude 擅长调试和功能实现。

用自然语言描述您想要的内容:

向用户注册表单添加输入验证

或修复现有问题:

有一个错误,用户可以提交空表单 - 修复它

Claude Code 将:

  • 定位相关代码
  • 理解上下文
  • 实现解决方案
  • 如果可用,运行测试

步骤 8:尝试其他常见工作流

有多种方式可以与 Claude 一起工作:

重构代码

refactor the authentication module to use async/await instead of callbacks

编写测试

write unit tests for the calculator functions

更新文档

update the README with installation instructions

代码审查

review my changes and suggest improvements
建议

像与有帮助的同事交谈一样与 Claude 交谈。描述您想要实现的目标,它将帮助您实现。

基本命令

以下是日常使用中最重要的命令。Shell 命令从您的终端运行以启动或恢复 Claude Code。会话命令在 Claude Code 启动后在其内部运行。

Shell 命令

命令 功能 示例
claude 启动交互模式 claude
claude "task" 运行一次性任务 claude "fix the build error"
claude -p "query" 运行一次性查询,然后退出 claude -p "explain this function"
claude -c 在当前目录中继续最近的对话 claude -c
claude -r 恢复之前的对话 claude -r

会话命令

命令 功能 示例
/clear 清除对话历史 /clear
/help 显示可用命令 /help
/exit 或 Ctrl+D 退出 Claude Code /exit

有关完整的 shell 命令列表,请参阅 CLI 参考,有关完整的会话命令列表,请参阅 命令参考

初学者专业提示

有关更多信息,请参阅最佳实践常见工作流

对您的请求要具体

不要说:"修复错误"

尝试:"修复登录错误,用户输入错误凭证后看到空白屏幕"

使用分步说明

将复杂任务分解为步骤:

```text theme={null}
1. 为用户配置文件创建新的数据库表
2. 创建 API 端点以获取和更新用户配置文件
3. 构建允许用户查看和编辑其信息的网页
```

让 Claude 先探索

在进行更改之前,让 Claude 理解您的代码:

```text theme={null}
分析数据库架构
```

```text theme={null}
构建一个仪表板,显示英国客户最常退货的产品
```

使用快捷方式节省时间
  • 输入 / 查看所有命令和 skills
    • 使用 Tab 进行命令补全
    • 按 ↑ 查看命令历史
    • Shift+Tab 循环切换权限模式

接下来呢? {#what’s-next}

现在您已经学习了基础知识,探索更多高级功能:

获取帮助

  • 在 Claude Code 中:输入 /help 或询问"我如何..."
  • 文档:您在这里!浏览其他指南
  • 社区:加入我们的 Discord 获取提示和支持
国内网络环境下的安装社区补充 · 非官方内容

官方安装脚本走的是境外地址,公司网络有代理限制时容易卡住。可以先确认代理环境变量已导出,再执行安装命令。

这是一条社区批注示例。批注不会被 npm run fetch 覆盖 —— 官方文档更新多少次,这里写的东西都还在。