# Create a worktree with an auto-generated namecodebuddy --worktree# Create a worktree with a specified namecodebuddy --worktree feature-auth# Create a worktree with a specified base branchcodebuddy --worktree --worktree-branch origin/develop # Based on the remote develop branchcodebuddy --worktree --worktree-branch feature/foo # Based on the local feature/foo branch# Run in a tmux session (recommended for long-running tasks)codebuddy --worktree --tmux# Create a worktree based on a PR/MR (for code review)codebuddy --worktree "#123" # GitHub PR numbercodebuddy --worktree "https://github.com/owner/repo/pull/123" # GitHub PR linkcodebuddy --worktree "https://gitlab.com/owner/repo/-/merge_requests/456" # GitLab MR linkcodebuddy --worktree "https://cnb.woa.com/owner/repo/-/pulls/789" # CNB PR link
> start a worktree> work in a worktree> Start a worktree
EnterWorktree tool to create an isolated working directory and switch to it.Parameter | Description | Example |
--worktree [name] | Create and enter the worktree. | --worktree or --worktree my-feature |
--worktree-branch <branch> | Specify the base branch (must be used with --worktree). | --worktree-branch origin/develop or --worktree-branch feature/foo |
--tmux | Run in a tmux session (requires tmux to be installed). | --worktree --tmux |
--tmux-classic | Use classic tmux mode (without popup). | --worktree --tmux --tmux-classic |
--worktree parameter:.codebuddy/worktrees/ directory.--worktree-branch origin/xxx is specified, the worktree is based on the remote branch origin/xxx.--worktree-branch xxx is specified, the worktree is based on the local branch xxx.origin/main or origin/master.worktree-feature-auth..worktreeinclude file.settings.json:{"worktree": {"symlinkDirectories": ["node_modules", ".next", ".cache", "dist"]}}
Configuration Item | Description | Default Value |
symlinkDirectories | Directories symbolically linked from the main repository to the worktree | [] |
.env.local and .env.development are typically excluded by .gitignore, but they are also required in a new worktree for it to run properly..worktreeinclude file in the repository root directory and list the files to copy, using the same syntax as .gitignore:# .worktreeinclude# List local files to copy to the new worktree# Environment variables.env.local.env.development.local.env.test.local# Local IDE configuration.vscode/settings.json# Local certificates or keys (if any)certs/localhost.pem
.worktreeinclude should be committed to Git so that all team members can benefit from it.git worktree command. For projects using SVN, Perforce, or no version control, you can use the worktree isolation feature by configuring WorktreeCreate / WorktreeRemove hooks.1. Is a WorktreeCreate hook configured? → Use the hook to create (the hook is preferred even in Git repositories).2. In a Git repository? → Use git worktree.3. None of the above? → Report an error and prompt to configure a hook.
git worktree..codebuddy/settings.json (project-level) or ~/.codebuddy/settings.json (global):{"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"}
Field | Description |
name | worktree name (user-specified or automatically generated by the system) |
cwd | Current Working Directory |
session_id | Current Session ID |
transcript_path | Transcript File 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"}
Field | Description |
worktree_path | Absolute path of the worktree to delete |
# Correct example: logs are output to stderr, and the path is output to stdout.echo "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"# Add VCS initialization logic here (SVN checkout, P4 sync, directory copy, etc.)cp -r "$CWD"/* "$WORKTREE_PATH/" 2>/dev/null || true# Output the absolute path to stdout on the last line (required)echo "$WORKTREE_PATH"
~/.codebuddy/hooks/worktree-remove.sh):#!/bin/bashINPUT=$(cat)WORKTREE_PATH=$(echo "$INPUT" | jq -r '.worktree_path')# Security check: ensure the path is within the expected range.if [[ "$WORKTREE_PATH" != "$HOME/.codebuddy/worktrees/"* ]]; thenecho "Refusing to remove path outside worktrees directory: $WORKTREE_PATH" >&2exit 1firm -rf "$WORKTREE_PATH"
jq to parse 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 flag:# Basic Usagecodebuddy --worktree --tmux# Use traditional mode (without popup)codebuddy --worktree --tmux --tmux-classic
Ctrl+D or type /exit to exit CodeBuddy.your-repo/├── .codebuddy/│ └── worktrees/│ ├── feature-auth/ # worktree directory│ │ ├── node_modules -> ../../node_modules # symbolic link│ │ └── ...│ └── fix-bug-123/├── .worktreeinclude # Defines local files to copy (recommended to commit)└── ...
# View all current worktreesgit worktree list# Delete a specified worktree (when there are no uncommitted changes in the worktree)git worktree remove .codebuddy/worktrees/feature-auth# Force delete (when there are uncommitted changes)git worktree remove --force .codebuddy/worktrees/feature-auth# Also delete the corresponding branchgit branch -D worktree-feature-auth# Clean up stale worktree references (the directory is deleted but Git still tracks it)git worktree prune
# Create a new branch and a worktree (outside the repository)git worktree add ../project-feature-a -b feature-a# Check out an existing branchgit worktree add ../project-bugfix bugfix-123# Enter the worktree and start CodeBuddy Codecd ../project-feature-a && codebuddy# Clean up after usegit worktree remove ../project-feature-a
isolation: worktree?isolation: worktree to the frontmatter of a custom Agent. Each time you launch this Agent with the Task tool, the system automatically creates a separate worktree for it instead of running it directly in the main repository directory..codebuddy/agents/ directory:---name: isolated-workerdescription: Work in a separate worktree without affecting the main repository.isolation: worktree---You are running in a separate git worktree.Focus on completing the task assigned to you, and report the results when done.
Task tool, it automatically creates a separate worktree for it. Multiple subagents can work simultaneously without conflicts even if they modify the same files.> Use the isolated-worker agent to create test.txt in the current directory and write "hello" to it.
# This file does not exist in the main repository.ls test.txt# → No such file or directory ✓# The file is in the subagent's worktree.ls .codebuddy/worktrees/agent-xxx/test.txt# → Exists ✓
# Create a worktree based on the remote develop branchcodebuddy --worktree feature-xxx --worktree-branch origin/develop# Create based on the local branch (preserving local unpushed commits)codebuddy --worktree feature-yyy --worktree-branch my-local-branch
# Terminal 1: Continue developing the new featurecodebuddy --worktree feature-payment# Terminal 2: Dedicated to fixing bugs without interfering with each othercodebuddy --worktree hotfix-login-crash
codebuddy --worktree refactor-esm
> Help me migrate the src/core directory from CommonJS to ESM
codebuddy --worktree "#456"
> Help me review what this PR changed and whether there are any potential issues.> Run the tests
codebuddy --worktree task-docs --tmuxcodebuddy --worktree task-tests --tmuxcodebuddy --worktree task-new-api --tmux
> Use three parallel api-worker subagents:First, process the src/api/user/ directory.Second, process the src/api/order/ directory.Third, process the src/api/product/ directory.
--worktree-branch: Should It Be a Remote Branch or a Local Branch?--worktree-branch origin/xxx → Use the remote branch (fetches the latest code first).--worktree-branch xxx → Use the local branch (preserves local unpushed commits).symlinkDirectories is configured, large directories are also shared and take up almost no additional space.git worktree remove --force .codebuddy/worktrees/my-featuregit branch -D worktree-my-featurecodebuddy --worktree my-feature
git merge.git worktree remove --force .codebuddy/worktrees/<name>git branch -D worktree-<name>
git status. You need to manually choose whether to delete it.--worktree requires a Git repository or a configured WorktreeCreate hook (non-Git projects such as SVN and Perforce can be supported through the hook).worktree- prefix./rewind command.Was this page helpful?
You can also Contact sales or Submit a Ticket for help.
Help us improve! Rate your documentation experience in 5 mins.
Feedback