What Are Worktrees?
Git worktrees allow you to check out multiple branches from a single repository simultaneously. Each worktree has its own working directory, enabling parallel work without context switching.Parallel Development
Work on multiple features or bug fixes simultaneously without switching branches
Clean Isolation
Each session gets a pristine branch with no uncommitted changes from other work
How HAPI Uses Worktrees
When you spawn a session withsessionType: 'worktree', the HAPI runner:
Worktree Environment Variables
When a session runs in a worktree, the runner sets these environment variables:HAPI_WORKTREE_BASE_PATH
The root directory of the base repository:HAPI_WORKTREE_BRANCH
The branch name created for this worktree:HAPI_WORKTREE_NAME
The worktree name (also the directory name):HAPI_WORKTREE_PATH
The full path to the worktree directory:HAPI_WORKTREE_CREATED_AT
Worktree creation timestamp in milliseconds:Worktree Directory Structure
Worktrees are created in a sibling directory to your repository:Creating Worktree Sessions
Via Web/Mobile App
When spawning a session through the web interface or Telegram Mini App:- Select “Git Worktree” as the session type
- Choose your repository base directory
- Optionally provide a worktree name (e.g., “feature-auth”)
- The runner creates the worktree and spawns the session
Via Runner RPC
Programmatically spawn worktree sessions:Worktree Naming
Worktree names follow a specific pattern:Default Naming (No Hint)
Default Naming (No Hint)
If you don’t provide a
worktreeName, HAPI generates a name with the format MMDD-XXXX:MM: Month (01-12)DD: Day (01-31)XXXX: Random hex suffix (4 characters)
0106-a3f2 (January 6th with random suffix)Named Worktrees (With Hint)
Named Worktrees (With Hint)
If you provide a
worktreeName, it’s converted to a URL-safe slug:- Lowercase conversion
- Replace non-alphanumeric with hyphens
- Strip leading/trailing hyphens
"Feature Auth"→feature-auth"fix/bug-123"→fix-bug-123"My New Feature"→my-new-feature
Collision Handling
Collision Handling
If a worktree name or branch already exists, HAPI appends a random hex suffix:
- First attempt:
feature-auth - Second attempt:
feature-auth-7b9e - Third attempt:
feature-auth-d4c1
Branch Naming
Worktree branches are prefixed withhapi- to distinguish them from regular branches:
Worktree Cleanup
The runner automatically cleans up worktrees when sessions end:Manual Cleanup
To manually remove a worktree:Cleanup on Errors
Worktrees are cleaned up in error scenarios:- Spawn fails (no PID returned)
- Session webhook timeout
- Directory creation error
- Exception during spawn
Worktree Detection
HAPI can automatically detect if a session is running in a worktree:Use Cases
Feature Development
Work on multiple features in parallel without branch switching
Bug Fixes
Isolate bug fix work while keeping feature development active
Code Review
Check out a PR branch in a worktree for side-by-side comparison
Experimentation
Test risky changes in an isolated worktree without affecting main work
Worktree Metadata in Sessions
HAPI sessions track worktree information in session metadata:- Stored in the hub database
- Visible in the web interface
- Included in session exports
- Used for session restoration
Limitations
Error Handling
Common errors and solutions:'Path is not a Git repository'
'Path is not a Git repository'
Cause: Base directory is not a git repoSolution: Initialize git or provide correct repository path:
'Failed to create worktree: fatal: 'hapi-name' already exists'
'Failed to create worktree: fatal: 'hapi-name' already exists'
Cause: Branch name collisionSolution: Use a different worktree name or delete the existing branch:
'Failed to create worktree after multiple attempts'
'Failed to create worktree after multiple attempts'
Cause: Random name collisions (rare)Solution: Try again or manually specify a unique worktree name
'Worktree base directory missing'
'Worktree base directory missing'
Cause: Base path doesn’t existSolution: Verify the repository path exists and is accessible
Best Practices
Use Descriptive Names
Provide meaningful worktree names for easy identification:
feature-auth, bugfix-123, refactor-apiClean Up Regularly
Remove unused worktrees to save disk space and avoid clutter
Merge Frequently
Merge worktree branches back to main regularly to avoid long-lived branches
Monitor Disk Usage
Each worktree duplicates your working directory. Large repos consume significant space.
Advanced: Worktree Persistence
Worktrees persist after the runner stops or restarts:- Runner restart: Sessions continue running in worktrees
- Machine reboot: Worktrees remain on disk but sessions are lost
- Orphaned worktrees: Use
git worktree listandgit worktree pruneto clean up
Troubleshooting
Session Not Detecting Worktree
-
Check environment variables:
-
Verify git worktree:
-
Check runner logs:
Worktree Not Cleaned Up
If a worktree remains after session exit:-
Check if process is still alive:
-
Manually remove worktree:
-
Check runner logs for cleanup failures:
Related Topics
Runner
Background daemon that manages worktree sessions
Configuration
Environment variables and settings
Sessions
Managing AI coding sessions