Claude Code CLI 新手教程

第 6 章:Git 与改代码

用自然语言提交、建分支、修 bug、重构、写测试——Claude 会定位代码、实现方案并运行测试。

Git 重构 测试

1对话式 Git

Claude Code 直接与 Git 集成,用自然语言即可完成常见 Git 操作:

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

也可在 Shell 中一次性完成:

claude "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
Claude 会执行 git status、git diff 等命令理解状态,再 staging、写 commit message、建分支或开 PR。

2添加功能

用自然语言描述需求,Claude 会规划方案、跨文件编写代码并验证:

add input validation to the user registration form

复杂功能建议分步描述:

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

3修复 Bug

粘贴错误信息或描述症状即可:

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

更具体的描述效果更好:

fix the login bug where users see a blank screen after entering wrong credentials

Claude 的典型流程:

  1. 定位相关代码
  2. 理解上下文与根因
  3. 实现修复
  4. 若有测试则自动运行并修复失败

4重构代码

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

重构前可让 Claude 先分析现有结构:

analyze the authentication module and suggest a refactoring plan

5编写测试

write unit tests for the calculator functions

也可要求写测试并跑通:

claude "write tests for the auth module, run them, and fix any failures"

6其他常见工作流

任务示例提示
更新文档update the README with installation instructions
代码审查review my changes and suggest improvements
修 Lintfix all lint errors across the project
升级依赖update dependencies and fix breaking changes
写 Release Noteswrite release notes for the last 10 commits
像和同事说话一样描述目标——Claude Code 会自己找文件、跑命令、迭代直到任务完成。
Claude Code CLI 新手教程 · 第 6 章