$npx -y skills add NeoLabHQ/context-engineering-kit --skill git-worktreesUse when working on multiple branches simultaneously, context switching without stashing, reviewing PRs while developing, testing in isolation, or comparing implementations across branches - provides git worktree commands and workflow patterns for parallel development with multip
| 1 | # Git Worktrees |
| 2 | |
| 3 | ## Overview |
| 4 | |
| 5 | Git worktrees enable checking out multiple branches simultaneously in separate directories, all sharing the same repository. Create a worktree instead of stashing changes or cloning separately. |
| 6 | |
| 7 | **Core principle:** One worktree per active branch. Switch contexts by changing directories, not branches. |
| 8 | |
| 9 | ## Core Concepts |
| 10 | |
| 11 | | Concept | Description | |
| 12 | |---------|-------------| |
| 13 | | **Main worktree** | Original working directory from `git clone` or `git init` | |
| 14 | | **Linked worktree** | Additional directories created with `git worktree add` | |
| 15 | | **Shared `.git`** | All worktrees share same Git object database (no duplication) | |
| 16 | | **Branch lock** | Each branch can only be checked out in ONE worktree at a time | |
| 17 | | **Worktree metadata** | Administrative files in `.git/worktrees/` tracking linked worktrees | |
| 18 | |
| 19 | ## Quick Reference |
| 20 | |
| 21 | | Task | Command | |
| 22 | |------|---------| |
| 23 | | Create worktree (existing branch) | `git worktree add <path> <branch>` | |
| 24 | | Create worktree (new branch) | `git worktree add -b <branch> <path>` | |
| 25 | | Create worktree (new branch from ref) | `git worktree add -b <branch> <path> <start>` | |
| 26 | | Create detached worktree | `git worktree add --detach <path> <commit>` | |
| 27 | | List all worktrees | `git worktree list` | |
| 28 | | Remove worktree | `git worktree remove <path>` | |
| 29 | | Force remove worktree | `git worktree remove --force <path>` | |
| 30 | | Move worktree | `git worktree move <old> <new>` | |
| 31 | | Lock worktree | `git worktree lock <path>` | |
| 32 | | Unlock worktree | `git worktree unlock <path>` | |
| 33 | | Prune stale worktrees | `git worktree prune` | |
| 34 | | Repair worktree links | `git worktree repair` | |
| 35 | | Compare files between worktrees | `diff ../worktree-a/file ../worktree-b/file` | |
| 36 | | Get one file from another branch | `git checkout <branch> -- <path>` | |
| 37 | | Get partial file changes | `git checkout -p <branch> -- <path>` | |
| 38 | | Cherry-pick a commit | `git cherry-pick <commit>` | |
| 39 | | Cherry-pick without committing | `git cherry-pick --no-commit <commit>` | |
| 40 | | Merge without auto-commit | `git merge --no-commit <branch>` | |
| 41 | |
| 42 | ## Essential Commands |
| 43 | |
| 44 | ### Create a Worktree |
| 45 | |
| 46 | ```bash |
| 47 | # Create worktree with existing branch |
| 48 | git worktree add ../feature-x feature-x |
| 49 | |
| 50 | # Create worktree with new branch from current HEAD |
| 51 | git worktree add -b new-feature ../new-feature |
| 52 | |
| 53 | # Create worktree with new branch from specific commit |
| 54 | git worktree add -b hotfix-123 ../hotfix origin/main |
| 55 | |
| 56 | # Create worktree tracking remote branch |
| 57 | git worktree add --track -b feature ../feature origin/feature |
| 58 | |
| 59 | # Create worktree with detached HEAD (for experiments) |
| 60 | git worktree add --detach ../experiment HEAD~5 |
| 61 | ``` |
| 62 | |
| 63 | ### List Worktrees |
| 64 | |
| 65 | ```bash |
| 66 | # Simple list |
| 67 | git worktree list |
| 68 | |
| 69 | # Verbose output with additional details |
| 70 | git worktree list -v |
| 71 | |
| 72 | # Machine-readable format (for scripting) |
| 73 | git worktree list --porcelain |
| 74 | ``` |
| 75 | |
| 76 | **Example output:** |
| 77 | |
| 78 | ``` |
| 79 | /home/user/project abc1234 [main] |
| 80 | /home/user/project-feature def5678 [feature-x] |
| 81 | /home/user/project-hotfix ghi9012 [hotfix-123] |
| 82 | ``` |
| 83 | |
| 84 | ### Remove a Worktree |
| 85 | |
| 86 | ```bash |
| 87 | # Remove worktree (working directory must be clean) |
| 88 | git worktree remove ../feature-x |
| 89 | |
| 90 | # Force remove (discards uncommitted changes) |
| 91 | git worktree remove --force ../feature-x |
| 92 | ``` |
| 93 | |
| 94 | ### Move a Worktree |
| 95 | |
| 96 | ```bash |
| 97 | # Relocate worktree to new path |
| 98 | git worktree move ../old-path ../new-path |
| 99 | ``` |
| 100 | |
| 101 | ### Lock/Unlock Worktrees |
| 102 | |
| 103 | ```bash |
| 104 | # Lock worktree (prevents pruning if on removable storage) |
| 105 | git worktree lock ../feature-x |
| 106 | git worktree lock --reason "On USB drive" ../feature-x |
| 107 | |
| 108 | # Unlock worktree |
| 109 | git worktree unlock ../feature-x |
| 110 | ``` |
| 111 | |
| 112 | ### Prune Stale Worktrees |
| 113 | |
| 114 | ```bash |
| 115 | # Remove stale worktree metadata (after manual directory deletion) |
| 116 | git worktree prune |
| 117 | |
| 118 | # Dry-run to see what would be pruned |
| 119 | git worktree prune --dry-run |
| 120 | |
| 121 | # Verbose output |
| 122 | git worktree prune -v |
| 123 | ``` |
| 124 | |
| 125 | ### Repair Worktrees |
| 126 | |
| 127 | ```bash |
| 128 | # Repair worktree links after moving directories manually |
| 129 | git worktree repair |
| 130 | |
| 131 | # Repair specific worktree |
| 132 | git worktree repair ../feature-x |
| 133 | ``` |
| 134 | |
| 135 | ## Workflow Patterns |
| 136 | |
| 137 | ### Pattern 1: Feature + Hotfix in Parallel |
| 138 | |
| 139 | To fix a bug while feature work is in progress: |
| 140 | |
| 141 | ```bash |
| 142 | # Create worktree for hotfix from main |
| 143 | git worktree add -b hotfix-456 ../project-hotfix origin/main |
| 144 | |
| 145 | # Switch to hotfix directory, fix, commit, push |
| 146 | cd ../project-hotfix |
| 147 | git add . && git commit -m "fix: resolve critical bug #456" |
| 148 | git push origin hotfix-456 |
| 149 | |
| 150 | # Return to feature work |
| 151 | cd ../project |
| 152 | |
| 153 | # Clean up when done |
| 154 | git worktree remove ../project-hotfix |
| 155 | ``` |
| 156 | |
| 157 | ### Pattern 2: PR Review While Working |
| 158 | |
| 159 | To review a PR without affecting current work: |
| 160 | |
| 161 | ```bash |
| 162 | # Fetch PR branch and create worktree |
| 163 | git fetch origin pull/123/head:pr-123 |
| 164 | git worktree add ../project-review pr-123 |
| 165 | |
| 166 | # Review: run tests, inspect |