Claude Code 入门与原理

Claude Code 入门与原理

快速上手

1 分钟阅读

快速入门

欢迎来到 Claude Code!

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

开始之前

确保你具备以下条件:

本指南涵盖终端 CLI。Claude Code 还可以在 Web桌面应用VS CodeJetBrains IDESlack 中使用,以及通过 GitHub ActionsGitLab 在 CI/CD 中使用。请参阅 所有界面

步骤 1:安装 Claude Code

要安装 Claude Code,请使用以下任一方法:

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

如果你看到 The token '&&' is not a valid statement separator,说明你当前在 PowerShell 中,而不是 CMD。如果你看到 'irm' is not recognized as an internal or external command,说明你当前在 CMD 中,而不是 PowerShell。当你在 PowerShell 中时,提示符显示为 PS C:\;在 CMD 中时,提示符显示为不带 PSC:\

如果安装命令失败并提示 syntax error near unexpected token '<'403 或其他 curl 错误,请参阅 安装故障排查 来匹配错误并找到解决方案,以及获取替代安装方法。

建议在原生 Windows 上安装 Git for Windows,以便 Claude Code 可以使用 Bash 工具。如果未安装 Git for Windows,Claude Code 将改用 PowerShell 作为 shell 工具。WSL 环境不需要安装 Git for Windows。

原生安装会在后台自动更新,确保你始终使用最新版本。

你也可以在 Debian、Fedora、RHEL 和 Alpine 上使用 apt、dnf 或 apk 进行安装。

步骤 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 实际编写一些代码。尝试一个简单的任务:

add a hello world function to the main file

Claude Code 会:

  1. 找到合适的文件
  2. 向你展示建议的更改
  3. 请求你的批准
  4. 执行编辑

Claude Code 在修改文件前总是会请求你的许可。你可以逐个批准更改,或者为一个会话启用 "Accept all" 模式。

步骤 6:使用 Git 与 Claude Code

Claude Code 让 Git 操作变得像对话一样自然:

what files have I changed?
commit my changes with a descriptive message

你也可以提示执行更复杂的 Git 操作:

create a new branch called feature/quickstart
show me the last 5 commits
help me resolve merge conflicts

步骤 7:修复 Bug 或添加功能

Claude 擅长调试和功能实现。

用自然语言描述你的需求:

add input validation to the user registration form

或者修复现有问题:

there's a bug where users can submit empty forms - fix it

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 参考,完整的会话命令列表请参阅 命令参考

新手进阶技巧

更多内容请参阅 最佳实践常见工作流

不要说:"fix the bug"

尝试:"fix the login bug where users see a blank screen after entering wrong credentials"

将复杂任务分解为步骤:

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 了解你的代码:

analyze the database schema
build a dashboard showing products that are most frequently returned by our UK customers
  • 输入 / 查看所有命令和技能
  • 使用 Tab 进行命令补全
  • 按 ↑ 查看命令历史
  • Shift+Tab 循环切换权限模式

下一步?

既然你已经掌握了基础知识,现在可以探索更多高级功能:

获取帮助

  • 在 Claude Code 中:输入 /help 或询问 "how do I..."
  • 文档:你正在阅读的就是!浏览其他指南
  • 社区:加入我们的 Discord 获取技巧和支持

博极客AI是专业人工智能学习平台,提供通俗易懂的AI入门教程、大模型应用、实战项目与行业动态,全站内容免费阅览,零基础也能轻松学AI,适配学生、职场新人及技术爱好者。

© 版权所有 2026 博极客AI,保留一切权利。 | 桂ICP备2026007205号 | 桂公网安备45010502001169号