何时使用 Amend
- 修复最近提交的拼写错误
- 将遗漏文件添加到上次提交
- 改善提交消息
- 仅限未推送的提交
本指南涵盖 Claude Code 中高效的 Git 工作流最佳实践,包括提交消息规范、分支管理、Rewind 与 Revert 的选择、Git Worktree 并行开发等。
Claude Code 能自动生成提交消息。通过 CLAUDE.md 配置来引导其风格。
# Claude 在完成更改后会生成如下提交:git commit -m "feat: add user authentication middleware
- Implement JWT validation- Add session management- Create auth error handling
Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>"在 CLAUDE.md 中定义提交约定:
## Git Commit Conventions
Follow Conventional Commits format:- feat: New features- fix: Bug fixes- docs: Documentation changes- refactor: Code restructuring- test: Test additions/changes- chore: Maintenance tasks
Keep first line under 72 characters.Include ticket number: [PROJ-123]You: "Implement login endpoint"
Claude: Creates commit:git commit -m "feat: implement user login endpoint [AUTH-42]
- Add POST /api/auth/login route- Validate credentials with bcrypt- Generate JWT tokens- Return user profile on success
Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>"除了默认的 Co-Authored-By 标签外,还有其他归属选项:
Assisted-by: 尾标标准何时使用 Amend
安全模式
已推送的提交绝不使用 amend(需要 force push,破坏团队历史),应创建新提交。
# 检查提交是否未推送git status# 输出: "Your branch is ahead of 'origin/main' by 1 commit"
# 修改提交You: "Add the missing test file to the last commit"
Claude:git add tests/auth.test.tsgit commit --amend --no-editYou: "Fix typo in authentication function"
Claude:# 创建新提交而不是修改git commit -m "fix: correct typo in authenticateUser function"在 CLAUDE.md 中定义分支工作流:
## CLAUDE.md Git Workflow
Always work on feature branches:1. Create branch from main: git checkout -b feature/name2. Make changes and commit regularly3. Push to remote: git push -u origin feature/name4. Create pull request when ready5. Never commit directly to main/masterfeature/user-authenticationfix/login-validation-bugrefactor/api-endpointsdocs/update-readmechore/dependency-updates# 开始新功能You: "Create a new branch for adding search functionality"Claude: git checkout -b feature/search-functionality
# 功能完成后You: "Create pull request for search feature"Claude:git push -u origin feature/search-functionality# 打开 PR 创建页面You: "Add email validation to login form"Claude: [进行更改]You: [审查 diff] "This breaks the existing flow"/rewind# 更改已撤销,回到之前状态You: "Add email validation but preserve existing flow"You: "Revert the authentication changes from the last commit"
Claude:git revert HEAD# 创建新提交: "Revert 'feat: add authentication'"更改尚未提交? → 使用 /rewind更改已提交但未推送? → 使用 git reset(谨慎!)更改已提交且已推送? → 使用 git revertGit Worktree(Git 2.5.0+ 可用)可以从同一仓库创建多个工作目录,每个目录检出到不同分支。
# 正在开发功能 Agit checkout feature-a# 工作了 2 小时...
# 突然需要紧急修复git stash # 保存当前工作git checkout maingit checkout -b hotfix# 修复 bug...git checkout feature-agit stash pop # 恢复工作# 一次性设置git worktree add ../myproject-hotfix hotfixgit worktree add ../myproject-feature-a feature-a
# 现在可以并行工作cd ../myproject-hotfix # 终端 1claude # 修复 bug
cd ../myproject-feature-a # 终端 2claude # 继续功能开发推荐使用
不推荐使用
| 命令 | 用途 |
|---|---|
/git-worktree | 创建 worktree,带分支验证、依赖符号链接、后台检查 |
/git-worktree-status | 检查后台验证任务(类型检查、测试、构建) |
/git-worktree-remove | 安全移除单个 worktree,包含合并检查和 DB 清理 |
/git-worktree-clean | 批量清理过期 worktree,附磁盘使用报告 |
# 创建(自动前缀 + 符号链接 node_modules)You: "/git-worktree auth"# → 创建 feat/auth 分支,符号链接 node_modules,后台运行检查
# 检查后台验证状态You: "/git-worktree-status"# → Type check: PASS, Tests: PASS (142 tests)
# 合并后移除You: "/git-worktree-remove feat/auth"# → 移除 worktree + 分支(本地 + 远程)+ DB 清理提醒
# 批量清理所有已合并的 worktreeYou: "/git-worktree-clean --dry-run"# → Preview: 3 merged (4.2 MB), 1 unmerged (kept)# 列出所有 worktreegit worktree list
# 移除 worktree(功能合并后)git worktree remove .worktrees/feature/new-api
# 清理过期的 worktree 引用git worktree prune每个 worktree 维护独立的 Claude Code 上下文:
# 终端 1 - Worktree Acd .worktrees/feature-aclaudeYou: "Implement user authentication"# Claude 索引 feature-a worktree
# 终端 2 - Worktree B(同时进行)cd .worktrees/feature-bclaudeYou: "Add payment integration"# Claude 索引 feature-b worktree(独立上下文)| 级别 | 位置 | 共享范围 |
|---|---|---|
| 全局 Memory | ~/.claude/CLAUDE.md | 所有 worktree 共享 |
| 项目 Memory | 仓库根 CLAUDE.md | 已提交,共享 |
| Worktree 本地 Memory | worktree 内 .claude/CLAUDE.md | 仅限该 worktree |
~/projects/├── myproject/ # 主 worktree(main 分支)│ ├── CLAUDE.md # 项目约定(已提交)│ └── .claude/├── myproject-develop/ # develop 分支 worktree│ └── .claude/ # develop 专属配置├── myproject-feature-a/ # feature-a 分支 worktree│ └── .claude/ # 功能 A 上下文└── myproject-hotfix/ # hotfix 分支 worktree └── .claude/ # 热修复上下文清晰命名 worktree
# 差git worktree add ../temp feature-x
# 好git worktree add ../myproject-feature-x feature-x添加到 .gitignore
# Worktree 目录.worktrees/worktrees/清理已合并的分支
git worktree remove myproject-feature-xgit branch -d feature-x # 删除本地分支git push origin --delete feature-x # 删除远程分支使用一致的位置
.worktrees/(隐藏目录,项目根目录中)worktrees/(可见目录,项目根目录中)../myproject-*(兄弟目录)不要提交 worktree 内容
始终确保 worktree 目录在 .gitignore 中。/git-worktree 命令会自动验证。
Claude Code 内置了 worktree 集成,超越了手动的 git worktree 工作流。
# --worktree / -w 标志:基于 HEAD 创建临时 worktreeclaude --worktreeclaude -wworktree 自动创建,Claude 在其中运行,退出时自动清理(如果没有更改)。
在 agent 的 frontmatter 中设置 isolation: "worktree",每次调用都会自动在新 worktree 中生成:
---name: refactoring-agentdescription: Large-scale refactors that must not pollute the main working treemodel: opusisolation: "worktree" # 每次调用获取独立的隔离检出---
Perform the requested refactoring. Commit your changes inside the worktree.两个新的 Hook 事件用于 worktree 生命周期:
| 事件 | 触发时机 | 用途 |
|---|---|---|
WorktreeCreate | agent worktree 创建时 | 设置 DB 分支、复制 .env、安装依赖 |
WorktreeRemove | agent worktree 销毁时 | 清理 DB 分支、删除临时凭证 |
{ "hooks": { "WorktreeCreate": [ { "matcher": "", "hooks": [ { "type": "command", "command": "scripts/worktree-setup.sh $CLAUDE_WORKTREE_PATH" } ] } ] }}将 Git Worktree 与数据库分支结合,实现真正的功能隔离。
传统工作流:Git 分支 → 共享开发数据库 → Schema 冲突 → 迁移噩梦
现代工作流:Git worktree + DB 分支 → 隔离环境 → 安全实验/git-worktree 命令自动检测你的数据库:
| 提供商 | 建议命令 |
|---|---|
| Neon | neonctl branches create |
| PlanetScale | pscale branch create |
| Supabase | 提示不支持分支 |
| 本地 Postgres | 建议基于 schema 的隔离 |
| 场景 | 创建分支? |
|---|---|
| 添加数据库迁移 | 是 |
| 重构数据模型 | 是 |
| Bug 修复(无 schema 更改) | 否 |
| 性能实验 | 是 |
# 1. 创建 worktree/git-worktree feature/payments
# 2. 按建议创建 DB 分支cd .worktrees/feature-paymentsneonctl branches create --name feature-payments --parent main
# 3. 用新 DATABASE_URL 更新 .env
# 4. 在隔离环境中工作npx prisma migrate devpnpm test
# 5. PR 合并后清理git worktree remove .worktrees/feature-paymentsneonctl branches delete feature-payments问题: Worktree 创建时报 “already checked out”
# 解决方案:不能在多个 worktree 中检出同一分支git worktree list # 查看哪些分支已检出# 使用不同分支或先移除已有的 worktree问题: 磁盘空间不足
# 每个 worktree 是完整的工作目录# 解决方案:定期清理未使用的 worktreegit worktree prune问题: 无法删除 worktree 目录
# 解决方案:使用 git worktree remove,不要使用 rm -rfgit worktree remove --force .worktrees/old-feature