-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgit-worktree-utils-help.sh
More file actions
508 lines (394 loc) · 15.5 KB
/
git-worktree-utils-help.sh
File metadata and controls
508 lines (394 loc) · 15.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
#!/usr/bin/env bash
# Git Worktree Utils - Help Documentation
# Sourced by git-worktree-utils.sh
# Help function
gwthelp() {
local cmd="${1:-}"
if [[ -n "$cmd" ]]; then
case "$cmd" in
gwt)
cat << 'EOF'
gwt - Create and manage git worktrees
USAGE:
gwt List all worktrees
gwt <branch> Create/switch to worktree for branch
gwt --help Show this help
DESCRIPTION:
The gwt command intelligently manages git worktrees by creating sibling
directories with standardized naming. It automatically handles:
- New branches (creates them)
- Local branches (checks them out)
- Remote branches (fetches and tracks them)
- Broken references (auto-prunes invalid worktrees)
- Orphaned directories (warns and offers cleanup)
- Submodules (initializes automatically)
DIRECTORY NAMING:
By default, worktrees are created as sibling directories using the pattern:
{base}-{branch}
Example: If you're in /code/myapp and run "gwt feature/auth", creates:
/code/myapp-feature-auth/
The pattern can be customized via GWT_DIR_PATTERN in the config file.
BRANCH DETECTION:
gwt automatically detects the branch type and handles it appropriately:
1. Local branch exists → Uses existing branch
2. Remote branch exists → Fetches and creates tracking branch
3. Branch doesn't exist → Creates new branch from current HEAD
EXAMPLES:
gwt # List all worktrees
gwt feature/new-login # Create worktree for new feature
gwt hotfix/security-patch # Create worktree for hotfix
gwt colleague-branch # Review colleague's work
# From anywhere in the repository (auto-finds repo root):
cd ~/code/myapp/src/components
gwt feature/refactor # Still works! Creates ../myapp-feature-refactor
COMMON SCENARIOS:
Emergency hotfix while working on feature:
gwt hotfix/urgent # Instantly switch to clean hotfix environment
# Fix, commit, push
cd ../myapp-feature-xyz # Return to feature work
Code review:
gwt pr/review-123 # Check out PR in isolated directory
# Test, review, comment
gwtclean # Remove when done
Parallel development:
gwt approach-a # Try solution A
gwt approach-b # Try solution B
diff -r ../myapp-approach-a ../myapp-approach-b # Compare
SAFETY FEATURES:
- Warns if directory exists without worktree
- Shows recent files before deletion
- Prompts for confirmation (configurable via GWT_CONFIRM_DELETE)
- Auto-prunes broken worktree references (configurable via GWT_AUTO_PRUNE)
SEE ALSO:
gwtclean Clean up orphaned directories
gwtlist Detailed worktree information
gwts Interactive worktree switcher
EOF
;;
gwtclean)
cat << 'EOF'
gwtclean - Clean up orphaned worktree directories
USAGE:
gwtclean
gwtclean --help
DESCRIPTION:
Performs comprehensive cleanup of the worktree environment:
1. Prunes broken worktree references from git
2. Finds orphaned directories (directories that exist but have no worktree)
3. Shows disk usage for each orphaned directory
4. Offers to delete them (with confirmation)
WHAT IT FINDS:
Orphaned directories are identified by matching patterns (configurable):
- *-feature-*
- *-hotfix-*
- *-release-*
- *-review-*
- *-epic-*
These patterns can be customized via GWT_CLEANUP_PATTERNS.
WHEN TO USE:
- After merging branches and deleting remote branches
- When worktree directories are manually moved/renamed
- Before major refactoring to clean up workspace
- Weekly maintenance to recover disk space
- When "gwt" warns about orphaned directories
EXAMPLES:
gwtclean # Interactive cleanup with confirmation
# Typical output:
# === Git Worktree Cleanup ===
# → Pruning broken worktree references...
# → Searching for orphaned directories...
#
# Found 3 orphaned directories:
# ../myapp-feature-old (458M)
# ../myapp-hotfix-merged (12M)
# ../myapp-review-pr-123 (234M)
#
# Delete all orphaned directories? (y/N)
SAFETY:
- Always shows directory contents preview before deletion
- Requires explicit confirmation (unless GWT_CONFIRM_DELETE=false)
- Shows disk space that will be freed
- Never deletes active worktrees
CONFIGURATION:
GWT_CONFIRM_DELETE=false # Skip confirmation prompts
GWT_CLEANUP_PATTERNS="..." # Custom directory patterns to find
SEE ALSO:
gwt Create and manage worktrees
gwtlist View detailed worktree information
EOF
;;
gwtlist)
cat << 'EOF'
gwtlist - List worktrees with detailed information
USAGE:
gwtlist
gwtlist --help
DESCRIPTION:
Shows comprehensive information about all worktrees including:
- Full directory path
- Current branch name
- Last commit (hash and message)
- Number of modified files
- Lock status (if locked)
- Prunable status (if directory missing)
OUTPUT FORMAT:
/path/to/worktree
Branch: feature/branch-name
Last commit: abc1234 Commit message here
Modified files: 3
[Locked] # If worktree is locked
[MISSING DIRECTORY] # If directory was deleted
EXAMPLES:
gwtlist
# Typical output:
# === Git Worktrees ===
# /Users/dev/myapp
# Branch: main
# Last commit: a1b2c3d Initial commit
#
# /Users/dev/myapp-feature-auth
# Branch: feature/auth
# Last commit: d4e5f6g Add login form
# Modified files: 2
WHEN TO USE:
- Get overview of all active development work
- Check which worktrees have uncommitted changes
- Find which worktree contains specific branch
- Identify worktrees that need attention (missing directories, locked)
NOTES:
- Requires being run from within a git repository
- For simple list, use "gwt" without arguments
- For interactive switching, use "gwts"
SEE ALSO:
gwt Create and manage worktrees
gwts Interactive worktree switcher
gwtclean Clean up orphaned directories
EOF
;;
gwts)
cat << 'EOF'
gwts - Interactive worktree switcher
USAGE:
gwts
gwts --help
DESCRIPTION:
Provides an interactive numbered menu to quickly switch between
existing worktrees. Shows current worktree and allows selection
by number.
EXAMPLES:
gwts
# Typical session:
# Select worktree to switch to:
# 1) /Users/dev/myapp [CURRENT]
# 2) /Users/dev/myapp-feature-auth
# 3) /Users/dev/myapp-hotfix-urgent
#
# Enter number (1-3): 2
# ✓ Switched to /Users/dev/myapp-feature-auth
WHEN TO USE:
- Quick navigation between active worktrees
- When you have many worktrees and don't want to type full names
- Alternative to manual "cd ../worktree-name"
KEYBOARD:
- Enter number and press Enter
- Ctrl+C to cancel
NOTES:
- Only shows currently valid worktrees
- Highlights current worktree in list
- Validates selection before switching
SEE ALSO:
gwt Create new worktrees
gwtlist View detailed worktree information
EOF
;;
config)
cat << 'EOF'
CONFIGURATION
CONFIG FILE LOCATION:
${XDG_CONFIG_HOME:-$HOME/.config}/git-worktree-utils/config
Default: ~/.config/git-worktree-utils/config
AVAILABLE OPTIONS:
GWT_DIR_PATTERN="{base}-{branch}"
Controls how worktree directories are named.
Variables:
{base} - Repository name (e.g., "myapp")
{branch} - Sanitized branch name (e.g., "feature-auth")
Examples:
"{base}-{branch}" → myapp-feature-auth
"{branch}-{base}" → feature-auth-myapp
"gwt-{base}-{branch}" → gwt-myapp-feature-auth
"{base}/{branch}" → myapp/feature-auth (nested)
GWT_AUTO_PRUNE=true
Automatically prune broken worktree references before each operation.
Options: true | false
Default: true
When true: Silently cleans up broken references
When false: You must manually run "git worktree prune"
GWT_CONFIRM_DELETE=true
Prompt for confirmation before deleting directories.
Options: true | false
Default: true
When true: Shows preview and asks "Delete? (y/N)"
When false: Automatically deletes without confirmation
GWT_CLEANUP_PATTERNS="*-feature* *-hotfix* *-release* *-review* *-epic*"
Space-separated patterns for directories that gwtclean should find.
Uses standard shell globbing patterns.
Default patterns match:
myapp-feature-*
myapp-hotfix-*
myapp-release-*
myapp-review-*
myapp-epic-*
Customize for your workflow:
GWT_CLEANUP_PATTERNS="*-feat-* *-fix-* *-wip-*"
GWT_USE_COLOR=true
Enable or disable colored output.
Options: true | false
Default: true
When false: Plain text output (useful for scripting)
EXAMPLE CONFIG FILE:
# Minimal config (uses mostly defaults)
GWT_DIR_PATTERN="{base}-{branch}"
GWT_AUTO_PRUNE=true
# Paranoid config (confirmations for everything)
GWT_CONFIRM_DELETE=true
GWT_AUTO_PRUNE=false
# Fast config (no confirmations)
GWT_CONFIRM_DELETE=false
GWT_AUTO_PRUNE=true
# Custom naming (nested structure)
GWT_DIR_PATTERN="worktrees/{base}/{branch}"
GWT_CLEANUP_PATTERNS="worktrees/*"
CREATING CONFIG:
# Copy example config
mkdir -p ~/.config/git-worktree-utils
cp config.example ~/.config/git-worktree-utils/config
# Edit with your preferences
$EDITOR ~/.config/git-worktree-utils/config
SEE ALSO:
Example config in repository: config.example
EOF
;;
*)
_gwt_print "$RED" "Unknown help topic: $cmd"
_gwt_print "$YELLOW" "Try: gwthelp (for overview) or gwthelp <command>"
return 1
;;
esac
else
# Main help overview
cat << 'EOF'
Git Worktree Utils - Comprehensive Help
═══════════════════════════════════════════════════════════════════════════
QUICK START:
gwt List all worktrees
gwt feature/name Create worktree for branch
gwtclean Clean up old worktrees
gwthelp <command> Detailed help for specific command
═══════════════════════════════════════════════════════════════════════════
CORE CONCEPT:
Instead of switching branches, switch directories. Each worktree is a
separate working directory with its own branch checked out.
Traditional workflow: Worktree workflow:
/myapp (switch branches) → /myapp (main)
/myapp-feature-auth (feature)
/myapp-hotfix-bug (hotfix)
═══════════════════════════════════════════════════════════════════════════
COMMANDS:
gwt [branch]
Create or switch to a worktree for the specified branch.
Without arguments, lists all worktrees.
Run from anywhere in your repository.
Examples:
gwt # List worktrees
gwt feature/auth # Create worktree
gwt hotfix/urgent # Emergency hotfix
Details: gwthelp gwt
gwtclean
Clean up orphaned directories and broken worktree references.
Shows disk usage and prompts for confirmation.
Examples:
gwtclean # Interactive cleanup
Details: gwthelp gwtclean
gwtlist
Show detailed information about all worktrees including branch,
last commit, and modified file count.
Examples:
gwtlist # Detailed worktree info
Details: gwthelp gwtlist
gwts
Interactive menu for quickly switching between worktrees.
Examples:
gwts # Show numbered menu
Details: gwthelp gwts
gwthelp [topic]
Show help information.
Examples:
gwthelp # This overview
gwthelp gwt # Detailed help for gwt
gwthelp config # Configuration reference
Topics: gwt, gwtclean, gwtlist, gwts, config, workflows, examples
═══════════════════════════════════════════════════════════════════════════
COMMON WORKFLOWS:
Feature Development:
gwt feature/new-feature # Create isolated environment
# ... work on feature ...
git push origin feature/new-feature
gwtclean # Clean up after merge
Emergency Hotfix:
gwt hotfix/critical # Instant clean environment
# ... fix bug ...
git push
cd ../myapp-feature-xyz # Return to feature work
Code Review:
gwt pr/review-123 # Check out PR
# ... test and review ...
cd ../myapp # Back to main work
gwtclean # Remove review worktree
Parallel Approaches:
gwt approach-a # Try solution A
gwt approach-b # Try solution B
diff -r ../myapp-approach-a ../myapp-approach-b
# Keep the better one, gwtclean the other
═══════════════════════════════════════════════════════════════════════════
CONFIGURATION:
Config file: ${XDG_CONFIG_HOME:-$HOME/.config}/git-worktree-utils/config
Quick reference:
GWT_DIR_PATTERN Directory naming pattern
GWT_AUTO_PRUNE Auto-prune broken references
GWT_CONFIRM_DELETE Prompt before deletion
GWT_CLEANUP_PATTERNS Patterns for finding orphans
GWT_USE_COLOR Enable colored output
Full details: gwthelp config
═══════════════════════════════════════════════════════════════════════════
GETTING HELP:
General help: gwthelp
Command details: gwthelp <command>
Configuration: gwthelp config
Workflows: gwthelp workflows
Examples: gwthelp examples
Available topics:
- gwt Main worktree command
- gwtclean Cleanup utility
- gwtlist Detailed listing
- gwts Interactive switcher
- config Configuration reference
- workflows Common usage patterns
- examples Real-world examples
═══════════════════════════════════════════════════════════════════════════
TROUBLESHOOTING:
"Directory already exists":
gwtclean # Will detect and offer removal
"Branch already exists":
gwt existing-branch # Just checks it out
Worktree in broken state:
git worktree prune
gwtclean
After moving repository:
git worktree repair
═══════════════════════════════════════════════════════════════════════════
For detailed help on any command: gwthelp <command>
GitHub: https://github.com/yourusername/git-worktree-utils
EOF
fi
}