# Git Worktree Command Reference Complete reference for `git worktree` commands. --- ## Basic Commands ### Create a New Worktree ```bash # Create worktree with new branch git worktree add -b # Create worktree from existing branch git worktree add # Create worktree from specific commit git worktree add ``` **Examples:** ```bash # New feature branch git worktree add ../myapp-feature-api -b feature-api # Use existing branch git worktree add ../myapp-hotfix hotfix-login # From specific commit git worktree add ../myapp-review abc123def ``` --- ### List All Worktrees ```bash # Simple list git worktree list # Porcelain format (machine-readable) git worktree list --porcelain # With verbose output git worktree list -v ``` **Output format:** ``` /home/user/myapp abc123 [main] /home/user/myapp-feature-api def456 [feature-api] /home/user/myapp-hotfix 789ghi [hotfix-login] ``` --- ### Remove a Worktree ```bash # Remove worktree (must be clean) git worktree remove # Force remove (even with uncommitted changes) git worktree remove --force ``` **Examples:** ```bash # Clean removal git worktree remove ../myapp-feature-api # Force removal git worktree remove ../myapp-feature-api --force ``` --- ### Move a Worktree ```bash # Move worktree to new location git worktree move ``` **Example:** ```bash git worktree move ../myapp-feature-api ~/projects/myapp-feature-api ``` --- ### Prune Stale References ```bash # Clean up references to removed worktrees git worktree prune # Dry run (show what would be pruned) git worktree prune --dry-run # Verbose output git worktree prune --verbose ``` --- ## Advanced Usage ### Lock/Unlock Worktrees Prevent a worktree from being automatically pruned or removed. ```bash # Lock a worktree git worktree lock # Lock with reason git worktree lock --reason "Long-running task" # Unlock a worktree git worktree unlock ``` **Use cases:** - Worktrees on removable drives - Network-mounted worktrees - Long-running background tasks --- ### Create Detached HEAD Worktrees Useful for reviewing specific commits without creating a branch. ```bash # Checkout specific commit git worktree add --detach # Short form git worktree add --detach HEAD~5 ``` **Example:** ```bash # Review a specific commit git worktree add ../review-commit --detach abc123def ``` --- ### Sparse Checkout in Worktrees Only check out specific files/directories in a worktree (useful for large repos). ```bash # Create worktree with sparse-checkout git worktree add cd git sparse-checkout init git sparse-checkout set ``` **Example:** ```bash # Only checkout src/ and tests/ git worktree add ../myapp-feature-api feature-api cd ../myapp-feature-api git sparse-checkout init git sparse-checkout set src/ tests/ ``` --- ## Repair Commands ### Repair Worktree If worktree becomes corrupted or administrative files are damaged. ```bash # Repair a worktree git worktree repair # Repair specific worktree git worktree repair ``` --- ## Configuration Options ### Set Worktree Defaults ```bash # Default path for worktrees (relative to repo) git config worktree.guessRemote true # Automatically set up remote tracking git config push.default current ``` --- ## Common Workflows ### Workflow 1: Parallel Feature Development ```bash # 1. Create worktree for feature A git worktree add ../repo-feature-a -b feature-a # 2. Create worktree for feature B git worktree add ../repo-feature-b -b feature-b # 3. Work on both simultaneously # (open each in separate editor/Claude session) # 4. When done, merge both git checkout main git merge feature-a git merge feature-b # 5. Clean up git worktree remove ../repo-feature-a git worktree remove ../repo-feature-b git branch -d feature-a feature-b ``` --- ### Workflow 2: Hotfix During Feature Development ```bash # You're working in a feature worktree cd ../repo-feature-xyz # Need to create hotfix cd git worktree add ../repo-hotfix -b hotfix-urgent # Fix the bug cd ../repo-hotfix # ... make fixes ... git add . git commit -m "Fix urgent bug" # Merge hotfix git checkout main git merge hotfix-urgent git push # Continue feature work cd ../repo-feature-xyz # ... continue ... ``` --- ### Workflow 3: Code Review Without Switching ```bash # Create worktree for review git worktree add ../repo-review pr-branch-name # Review in separate window cd ../repo-review # ... review code ... # Clean up after review cd git worktree remove ../repo-review ``` --- ## Troubleshooting Commands ### Check Worktree Status ```bash # List all worktrees with details git worktree list -v # Check if directory is a worktree cd git rev-parse --git-dir ``` --- ### Find Orphaned Worktrees ```bash # List worktrees that no longer exist git worktree list --porcelain | grep "^worktree" | awk '{print $2}' | while read dir; do if [ ! -d "$dir" ]; then echo "Orphaned: $dir" fi done # Clean them up git worktree prune ``` --- ### Reset Worktree to Clean State ```bash # Discard all changes cd git reset --hard HEAD git clean -fd # Reset to specific branch state git fetch origin git reset --hard origin/ ``` --- ## Integration with Other Git Commands ### Fetch/Pull in Worktrees ```bash # Fetch applies to all worktrees (shared .git) git fetch origin # Pull in specific worktree cd git pull origin ``` --- ### Stash in Worktrees ```bash # Stash is per-worktree cd git stash push -m "My stash" # List stashes (shared across worktrees) git stash list # Apply stash in different worktree cd git stash apply stash@{0} ``` --- ### Rebase in Worktrees ```bash # Each worktree can have independent rebase operations cd git rebase main ``` --- ## Performance Tips ### Limit Number of Worktrees - **Recommended:** 3-5 active worktrees - **Maximum:** Depends on disk space and RAM - **Why:** Each worktree duplicates working files ### Use Sparse Checkout For large repositories: ```bash git sparse-checkout set ``` Reduces disk usage by only checking out necessary files. --- ### Share Object Database Worktrees automatically share: - `.git/objects/` (commits, trees, blobs) - `.git/refs/` (branches, tags) - `.git/config` (configuration) **No duplication of:** - Git history - Commit objects - Branch information **Duplicated:** - Working directory files - Index (staging area) - Worktree-specific config --- ## Safety and Best Practices ### Before Removing Worktrees ```bash # Check for uncommitted changes cd git status # Check if branch is merged git branch --merged main | grep # Backup if needed git stash push -m "Backup before removal" ``` --- ### Handling Merge Conflicts ```bash # During merge in worktree cd git merge main # If conflicts occur git status # See conflicting files # Edit files to resolve git add git commit ``` --- ## Quick Reference Table | Command | Purpose | Example | |---------|---------|---------| | `git worktree add -b ` | Create with new branch | `git worktree add ../wt -b feat` | | `git worktree add ` | Create from existing | `git worktree add ../wt main` | | `git worktree list` | Show all worktrees | - | | `git worktree remove ` | Remove worktree | `git worktree remove ../wt` | | `git worktree remove --force` | Force remove | `git worktree remove ../wt -f` | | `git worktree prune` | Clean stale refs | - | | `git worktree move ` | Move worktree | `git worktree move ../wt ~/wt` | | `git worktree lock ` | Prevent pruning | `git worktree lock ../wt` | | `git worktree unlock ` | Allow pruning | `git worktree unlock ../wt` | | `git worktree repair` | Fix corrupted worktree | - | --- ## Common Error Messages ### "fatal: '' already exists" **Cause:** Directory exists **Solution:** ```bash rm -rf # or use a different path ``` --- ### "fatal: invalid reference: " **Cause:** Branch name has invalid characters **Solution:** Use only alphanumeric and hyphens: ```bash # Bad: feature/my_feature # Good: feature-my-feature ``` --- ### "fatal: '' is not a working tree" **Cause:** Trying to operate on non-worktree directory **Solution:** ```bash git worktree list # Find correct path ``` --- ### "fatal: '' is already checked out" **Cause:** Branch in use by another worktree **Solution:** ```bash # Use a different branch name git worktree add ../wt -b new-branch-name ``` --- ## Resources **Official Documentation:** - https://git-scm.com/docs/git-worktree **Pro Git Book:** - Chapter on worktrees: https://git-scm.com/book/en/v2 **Git Worktree Tutorial:** - https://git-scm.com/docs/git-worktree#_examples