# 自动生成名称创建 worktreecodebuddy --worktree# 指定名称创建 worktreecodebuddy --worktree feature-auth# 指定基础分支创建 worktreecodebuddy --worktree --worktree-branch origin/develop # 基于远程 develop 分支codebuddy --worktree --worktree-branch feature/foo # 基于本地 feature/foo 分支# 在 tmux 会话中运行(推荐用于长时间任务)codebuddy --worktree --tmux# 基于 PR/MR 创建 worktree(用于代码审查)codebuddy --worktree "#123" # GitHub PR 编号codebuddy --worktree "https://github.com/owner/repo/pull/123" # GitHub PR 链接codebuddy --worktree "https://gitlab.com/owner/repo/-/merge_requests/456" # GitLab MR 链接codebuddy --worktree "https://cnb.woa.com/owner/repo/-/pulls/789" # CNB PR 链接
> start a worktree> work in a worktree> 启动 worktree
EnterWorktree 工具创建隔离工作目录并切换过去。参数 | 说明 | 示例 |
--worktree [name] | 创建并进入 worktree | --worktree 或 --worktree my-feature |
--worktree-branch <branch> | 指定基础分支(需配合 --worktree) | --worktree-branch origin/develop 或 --worktree-branch feature/foo |
--tmux | 在 tmux 会话中运行(需要安装 tmux) | --worktree --tmux |
--tmux-classic | 使用传统 tmux 模式(不使用 popup) | --worktree --tmux --tmux-classic |
--worktree 参数启动时:.codebuddy/worktrees/ 目录下创建新的 worktree--worktree-branch origin/xxx,基于远程分支 origin/xxx--worktree-branch xxx,基于本地分支 xxxorigin/main 或 origin/master)worktree-feature-auth).worktreeinclude 文件等)settings.json 中配置 worktree 行为:{"worktree": {"symlinkDirectories": ["node_modules", ".next", ".cache", "dist"]}}
配置项 | 说明 | 默认值 |
symlinkDirectories | 从主仓库符号链接到 worktree 的目录 | [] |
.env.local、.env.development 等本地配置文件通常被 .gitignore 排除,但新 worktree 中同样需要它们才能正常运行。.worktreeinclude 文件,列出需要复制的文件(语法与 .gitignore 相同):# .worktreeinclude# 列出需要复制到新 worktree 的本地文件# 环境变量.env.local.env.development.local.env.test.local# 本地 IDE 配置.vscode/settings.json# 本地证书或密钥(如有)certs/localhost.pem
.worktreeinclude 应该提交到 Git,这样团队成员都能受益。git worktree 命令。对于使用 SVN、Perforce 或没有版本控制的项目,可以通过配置 WorktreeCreate / WorktreeRemove hooks 来使用 worktree 隔离功能。1. 配置了 WorktreeCreate hook? → 使用 hook 创建(即使在 Git 仓库中也优先使用 hook)2. 在 Git 仓库中? → 使用 git worktree3. 都不满足? → 报错并提示配置 hook
git worktree.codebuddy/settings.json(项目级)或 ~/.codebuddy/settings.json(全局)中配置:{"hooks": {"WorktreeCreate": [{"hooks": [{"type": "command","command": "bash ~/.codebuddy/hooks/worktree-create.sh"}]}],"WorktreeRemove": [{"hooks": [{"type": "command","command": "bash ~/.codebuddy/hooks/worktree-remove.sh"}]}]}}
{"hook_event_name": "WorktreeCreate","session_id": "abc123def456","cwd": "/Users/user/project","transcript_path": "/Users/user/.codebuddy/sessions/abc123/transcript.md","name": "feature-auth"}
字段 | 说明 |
name | worktree 名称(用户指定或系统自动生成) |
cwd | 当前工作目录 |
session_id | 当前会话 ID |
transcript_path | 对话记录文件路径 |
{"hook_event_name": "WorktreeRemove","session_id": "abc123def456","cwd": "/Users/user/project","transcript_path": "/Users/user/.codebuddy/sessions/abc123/transcript.md","worktree_path": "/tmp/codebuddy-worktrees/feature-auth"}
字段 | 说明 |
worktree_path | 要删除的 worktree 绝对路径 |
# 正确示例:日志输出到 stderr,路径输出到 stdoutecho "Initializing SVN checkout..." >&2echo "/home/user/.codebuddy/worktrees/feature-auth"
git status)。~/.codebuddy/hooks/worktree-create.sh):#!/bin/bashset -eINPUT=$(cat)NAME=$(echo "$INPUT" | jq -r '.name')CWD=$(echo "$INPUT" | jq -r '.cwd')WORKTREE_PATH="$HOME/.codebuddy/worktrees/$NAME"mkdir -p "$WORKTREE_PATH"# 在这里添加 VCS 初始化逻辑(SVN checkout、P4 sync、目录复制等)cp -r "$CWD"/* "$WORKTREE_PATH/" 2>/dev/null || true# 最后一行输出绝对路径到 stdout(必需)echo "$WORKTREE_PATH"
~/.codebuddy/hooks/worktree-remove.sh):#!/bin/bashINPUT=$(cat)WORKTREE_PATH=$(echo "$INPUT" | jq -r '.worktree_path')# 安全检查:确保路径在预期范围内if [[ "$WORKTREE_PATH" != "$HOME/.codebuddy/worktrees/"* ]]; thenecho "Refusing to remove path outside worktrees directory: $WORKTREE_PATH" >&2exit 1firm -rf "$WORKTREE_PATH"
jq 解析 JSON。macOS:brew install jq,Linux:apt install jq。{"hooks": {"WorktreeCreate": [{"hooks": [{"type": "command","command": "bash -c 'INPUT=$(cat); NAME=$(echo $INPUT | jq -r .name); DIR=\\"$HOME/.codebuddy/worktrees/$NAME\\"; svn checkout https://svn.example.com/repo/trunk \\"$DIR\\" >&2 && echo \\"$DIR\\"'"}]}],"WorktreeRemove": [{"hooks": [{"type": "command","command": "bash -c 'cat | jq -r .worktree_path | xargs rm -rf'"}]}]}}
{"hooks": {"WorktreeCreate": [{"hooks": [{"type": "command","command": "bash -c 'INPUT=$(cat); NAME=$(echo $INPUT | jq -r .name); CWD=$(echo $INPUT | jq -r .cwd); DIR=\\"$HOME/.codebuddy/worktrees/$NAME\\"; cp -r \\"$CWD\\" \\"$DIR\\" && echo \\"$DIR\\"'"}]}],"WorktreeRemove": [{"hooks": [{"type": "command","command": "bash -c 'cat | jq -r .worktree_path | xargs rm -rf'"}]}]}}
--tmux 参数可以在独立的 tmux 会话中运行 CodeBuddy:# 基本用法codebuddy --worktree --tmux# 使用传统模式(不使用 popup)codebuddy --worktree --tmux --tmux-classic
Ctrl+D 或输入 /exit 退出 CodeBuddyyour-repo/├── .codebuddy/│ └── worktrees/│ ├── feature-auth/ # worktree 目录│ │ ├── node_modules -> ../../node_modules # 符号链接│ │ └── ...│ └── fix-bug-123/├── .worktreeinclude # 定义需要复制的本地文件(建议提交)└── ...
# 查看当前所有 worktreegit worktree list# 删除指定 worktree(worktree 内没有未提交改动时)git worktree remove .codebuddy/worktrees/feature-auth# 强制删除(有未提交改动时)git worktree remove --force .codebuddy/worktrees/feature-auth# 同时删除对应的分支git branch -D worktree-feature-auth# 清理失效的 worktree 引用(目录已删但 Git 还记着)git worktree prune
# 新建分支并创建 worktree(放在仓库外)git worktree add ../project-feature-a -b feature-a# 检出已有分支git worktree add ../project-bugfix bugfix-123# 进入 worktree,启动 CodeBuddy Codecd ../project-feature-a && codebuddy# 用完后清理git worktree remove ../project-feature-a
isolation: worktree 是什么isolation: worktree,每次用 Task 工具启动这个 Agent 时,系统会自动为它创建一个独立的 worktree,而不是在主仓库目录里直接运行。.codebuddy/agents/ 目录下创建 Markdown 文件:---name: isolated-workerdescription: 在独立 worktree 中工作,不影响主仓库isolation: worktree---您在一个独立的 git worktree 中运行。请专注完成分配给您的任务,完成后汇报结果。
Task 工具启动该 Agent 时,会自动为它创建独立的 worktree。多个子代理可以同时工作,即使修改了相同的文件也不会冲突。> 使用 isolated-worker agent,在当前目录创建 test.txt,写入 "hello"
# 主仓库里没有这个文件ls test.txt# → No such file or directory ✓# 文件在子代理的 worktree 里ls .codebuddy/worktrees/agent-xxx/test.txt# → 存在 ✓
# 基于远程 develop 分支创建 worktreecodebuddy --worktree feature-xxx --worktree-branch origin/develop# 基于本地分支创建(保留本地未推送的提交)codebuddy --worktree feature-yyy --worktree-branch my-local-branch
# 终端 1:继续开发新功能codebuddy --worktree feature-payment# 终端 2:专门修 bug,互不干扰codebuddy --worktree hotfix-login-crash
codebuddy --worktree refactor-esm
> 帮我把 src/core 目录从 CommonJS 迁移到 ESM
codebuddy --worktree "#456"
> 帮我看看这次 PR 改了什么,有没有潜在问题> 运行一下测试
codebuddy --worktree task-docs --tmuxcodebuddy --worktree task-tests --tmuxcodebuddy --worktree task-new-api --tmux
> 使用三个并行的 api-worker 子代理:第一个处理 src/api/user/ 目录第二个处理 src/api/order/ 目录第三个处理 src/api/product/ 目录
--worktree-branch 应该传远程分支还是本地分支?--worktree-branch origin/xxx → 使用远程分支(会先 fetch 最新代码)--worktree-branch xxx → 使用本地分支(保留本地未推送的提交)symlinkDirectories,大型目录也会共享,几乎不占额外空间。git worktree remove --force .codebuddy/worktrees/my-featuregit branch -D worktree-my-featurecodebuddy --worktree my-feature
git merge。git worktree remove --force .codebuddy/worktrees/<名称>git branch -D worktree-<名称>
git status)。需要手动选择是否删除。--worktree 需要 Git 仓库或配置 WorktreeCreate hook(非 Git 项目如 SVN、Perforce 可通过 hook 支持)worktree- 为前缀/rewind 命令文档反馈