worktree 0.2.0

A powerful CLI tool for managing git worktrees with enhanced features including centralized storage, automatic config file synchronization, and intelligent branch management
Documentation
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
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
# Worktree CLI

A powerful CLI tool that transforms git worktree management from painful to effortless. Stop juggling multiple local repos, losing config files, or manually organizing worktree directories.

**The Problem:** Git worktrees are incredibly useful for parallel development, but the native git commands are cumbersome. You end up with worktrees scattered across your filesystem, config files that don't transfer, and no easy way to navigate between them.

**The Solution:** Worktree CLI provides a centralized, intelligent system that handles all the complexity for you. Create, manage, and navigate worktrees with simple commands while automatically maintaining your development environment.

## Why Use Worktree CLI?

- โšก **Zero Setup Friction** - Create worktrees anywhere in your project with one command
- ๐Ÿ  **Centralized & Organized** - All worktrees live in `~/.worktrees/<repo>/<branch>/` - no more scattered directories
- ๐Ÿ”„ **Config Sync Magic** - Your `.env`, `.vscode/`, and local config files automatically follow you to new worktrees
- ๐Ÿงญ **Effortless Navigation** - Jump between worktrees instantly with smart completions and interactive selection
- ๐Ÿ”™ **Quick Return** - Navigate back to original repo from any worktree with `worktree back`
- ๐Ÿงน **Self-Cleaning** - Automatically cleans up orphaned branches and references to prevent git clutter
- ๐Ÿ”ง **Developer-Friendly** - Shell integration with intelligent tab completions and directory changing
- ๐Ÿ›ก๏ธ **Handles Edge Cases** - Safe branch name sanitization, git config inheritance, and sync state management

## Installation

> **โš ๏ธ Important:** This tool requires shell integration to function properly. The `worktree jump` and `worktree back` commands won't work without it, and you'll miss out on intelligent tab completions. Make sure to complete both installation steps below.

### 1. Install from crates.io

```bash
# Install the latest version from crates.io
cargo install worktree
```

This will install the `worktree-bin` binary to your cargo bin directory (typically `~/.cargo/bin/`). Make sure this directory is in your PATH.

### 2. Set Up Shell Integration with Completions

**Important:** The `worktree` command is a shell function that wraps `worktree-bin` to enable directory changing and provides enhanced tab completions automatically. Without this integration, `worktree jump` and `worktree back` won't be able to change your current directory.

Add the following to your shell configuration:

#### Bash

Add to your `~/.bashrc` or `~/.bash_profile`:

```bash
# Generate and source worktree shell integration with completions
eval "$(worktree-bin init bash)"
```

#### Zsh

Add to your `~/.zshrc`:

```bash
# Generate and source worktree shell integration with completions
eval "$(worktree-bin init zsh)"
```

#### Fish

Add to your Fish config (`~/.config/fish/config.fish`):

```fish
# Generate and source worktree shell integration with completions
worktree-bin init fish | source
```

**Note:** The shell integration provides sophisticated tab completions that enhance your workflow:

**Command & Flag Completion:**

- All subcommands (`create`, `list`, `jump`, etc.) with intelligent suggestions
- All flags and options with descriptions (powered by clap)
- Context-aware completion based on your current command

**Dynamic Worktree Completion:**

- Live completion of worktree names for `worktree jump`
- Fuzzy matching - type partial names and get suggestions
- Pressing TAB on empty `worktree jump` triggers interactive selection
- Completion respects `--current` flag to show only current repository worktrees

**Smart Navigation:**

- Shell integration enables `worktree jump` and `worktree back` to actually change directories
- All other commands are delegated to the binary while maintaining completion support

### 3. Reload Your Shell

```bash
# Reload your shell configuration
source ~/.bashrc   # for bash
source ~/.zshrc    # for zsh
# or restart your terminal
```

### Verify Installation

```bash
# Test that worktree command is available
worktree --help

# Test shell integration works
worktree status
```

## Quick Start

```bash
# Check what's currently set up
worktree status

# Create a worktree for an existing branch
worktree create feature/auth

# Create a worktree with a brand new branch
worktree create --new-branch feature/payments

# Jump between worktrees instantly
worktree jump feature/auth

# Use interactive selection when you can't remember the name
worktree jump --interactive

# Navigate back to the original repo from any worktree
worktree back

# See all your worktrees
worktree list

# Clean up when branches get out of sync
worktree cleanup

# Remove a worktree when you're done (deletes branch by default)
worktree remove feature/auth

# Keep the branch but remove the worktree
worktree remove feature/payments --keep-branch
```

## Commands

| Command                   | Description                               | Key Options                         |
| ------------------------- | ----------------------------------------- | ----------------------------------- |
| `create <branch>`         | Create a new worktree                     | `--new-branch`, `--existing-branch` |
| `list`                    | List all worktrees                        | `--current`                         |
| `remove <target>`         | Remove a worktree                         | `--keep-branch`                     |
| `status`                  | Show worktree status                      | -                                   |
| `sync-config <from> <to>` | Sync config files between worktrees       | -                                   |
| `jump [target]`           | Navigate to a worktree directory          | `--interactive`, `--current`        |
| `back`                    | Navigate back to original repository      | -                                   |
| `cleanup`                 | Clean up orphaned branches and references | -                                   |
| `completions <shell>`     | Generate shell completions                | -                                   |
| `init <shell>`            | Generate shell integration                | -                                   |

### `create` - Create a new worktree

```bash
worktree create <branch> [OPTIONS]
```

**Options:**

- `--new-branch` - Force creation of a new branch (fail if it already exists)
- `--existing-branch` - Only use an existing branch (fail if it doesn't exist)

**Examples:**

```bash
# Create worktree for existing branch
worktree create feature/login

# Create worktree with new branch
worktree create --new-branch feature/new-thing
```

### `list` - List all worktrees

```bash
worktree list [OPTIONS]
```

**Options:**

- `--current` - Show worktrees for current repo only

**Examples:**

```bash
# List all managed worktrees
worktree list

# List worktrees for current repository
worktree list --current
```

### `remove` - Remove a worktree

```bash
worktree remove <target> [OPTIONS]
```

**Options:**

- `--keep-branch` - Keep the branch (only remove the worktree, branch deleted by default)

**Examples:**

```bash
# Remove worktree by branch name
worktree remove feature/auth

# Remove worktree (deletes branch by default)
worktree remove feature/auth

# Remove worktree but keep the branch
worktree remove feature/auth --keep-branch
```

### `status` - Show worktree status

```bash
worktree status
```

Displays comprehensive information about:

- Git worktrees vs managed worktrees
- Directory existence status
- Synchronization state
- Repository information

### `sync-config` - Sync config files between worktrees

```bash
worktree sync-config <from> <to>
```

**Examples:**

```bash
# Sync config from main to feature branch
worktree sync-config main feature/auth

# Sync using paths
worktree sync-config ~/.worktrees/project/main ~/.worktrees/project/feature
```

### `jump` - Navigate to a worktree directory

```bash
worktree jump [target] [OPTIONS]
```

**Options:**

- `--interactive` - Launch interactive selection mode
- `--current` - Show worktrees for current repo only

**Examples:**

```bash
# Jump to a specific worktree
worktree jump feature/auth

# Interactive selection (also triggered by pressing TAB on empty jump)
worktree jump --interactive

# Jump with tab completion - type partial name and press TAB
worktree jump feat<TAB>  # completes to available worktrees

# Current repo worktrees only
worktree jump --current
```

### `cleanup` - Clean up orphaned branches and worktree references

```bash
worktree cleanup
```

Automatically cleans up your workspace by:

- Removing git branches that have no corresponding worktree directory
- Cleaning up branch mappings for non-existent worktrees
- Removing git worktree references that point to non-existent directories

This command is useful when worktrees get out of sync due to manual deletion or filesystem issues.

**Examples:**

```bash
# Clean up orphaned branches and references
worktree cleanup
```

### `back` - Navigate back to the original repository

```bash
worktree back
```

Navigates back to the original repository directory that the current worktree was created from. This command only works when executed from within a worktree directory that was created using `worktree create`.

**Examples:**

```bash
# From within a worktree, return to the original repo
worktree back
```

### `completions` - Generate shell completions

```bash
worktree completions <SHELL>
```

Generates native shell completions for the specified shell. This is separate from the integrated completions provided by `worktree init`.

**Options:**

- `<SHELL>` - Shell to generate completions for (bash, zsh, fish)

**Examples:**

```bash
# Generate completions for bash
worktree completions bash > /usr/local/etc/bash_completion.d/worktree

# Generate completions for zsh
worktree completions zsh > ~/.local/share/zsh/site-functions/_worktree
```

## Configuration

### `.worktree-config.toml`

Create a `.worktree-config.toml` file in your repository root to customize which files are copied to new worktrees:

```toml
[copy-patterns]
include = [
    ".env*",
    ".vscode/",
    "*.local.json",
    "config/local/*",
    ".idea/",
    "docker-compose.override.yml"
]
exclude = [
    "node_modules/",
    "target/",
    ".git/",
    "*.log",
    "*.tmp",
    "dist/",
    "build/"
]
```

**Default patterns (if no config file exists):**

Include:

- `.env*` - Environment files
- `.vscode/` - VS Code settings
- `*.local.json` - Local configuration files
- `config/local/*` - Local config directories

Exclude:

- `node_modules/`, `target/` - Build artifacts
- `.git/` - Git directory
- `*.log`, `*.tmp` - Temporary files

## Storage Organization

Worktrees are organized in a clean, predictable structure:

```
~/.worktrees/
โ”œโ”€โ”€ my-project/
โ”‚   โ”œโ”€โ”€ main/
โ”‚   โ”œโ”€โ”€ feature-auth/          # branch: feature/auth
โ”‚   โ”œโ”€โ”€ bugfix-login/          # branch: bugfix/login
โ”‚   โ””โ”€โ”€ develop/
โ”œโ”€โ”€ another-repo/
โ”‚   โ”œโ”€โ”€ main/
โ”‚   โ””โ”€โ”€ feature-xyz/           # branch: feature/xyz
โ””โ”€โ”€ third-project/
    โ””โ”€โ”€ experimental/
```

**Branch Name Sanitization:**
Branch names containing slashes and special characters are automatically sanitized for safe filesystem storage:

- `feature/auth` โ†’ `feature-auth/`
- `bugfix/critical-issue` โ†’ `bugfix-critical-issue/`
- `release/v1.0` โ†’ `release-v1.0/`

The original branch names are preserved and displayed in all commands.

## Use Cases

### 1. Feature Development Workflow

```bash
# Start working on a new feature (from main repo)
worktree create --new-branch feature/payments

# Jump to the new worktree instantly
worktree jump feature/payments

# Your .env, .vscode/, and config files are already there!
# Work on your feature...

# Jump back to main when needed
worktree back

# When done, remove it (deletes branch by default)
worktree remove feature/payments
```

### 2. Parallel Development

```bash
# Work on multiple features simultaneously
worktree create --new-branch feature/auth
worktree create --new-branch feature/dashboard
worktree create bugfix/critical-issue

# Jump between them effortlessly
worktree jump auth           # Tab completion works!
worktree jump dashboard
worktree jump critical

# Or use interactive selection
worktree jump --interactive
```

### 3. Code Review & Testing

```bash
# Create temporary worktree for PR review
worktree create pr-123

# Jump to review (config already synced)
worktree jump pr-123

# Test the changes, then return to your work
worktree back

# Clean up when done
worktree remove pr-123
```

### 4. Maintenance & Cleanup

```bash
# Regular maintenance - clean up orphaned branches
worktree cleanup

# Check what's currently active
worktree status

# List all your worktrees across projects
worktree list
```

## Troubleshooting

### Worktree Commands Don't Change Directory

**Problem:** `worktree jump` or `worktree back` doesn't change your current directory.

**Solution:** You need to set up shell integration. The binary alone cannot change the shell's directory.

```bash
# Add to your shell profile (.bashrc, .zshrc, etc.)
eval "$(worktree-bin init bash)"  # or zsh/fish
source ~/.bashrc  # reload your shell
```

### Sync Issues Between Git and Filesystem

**Problem:** `worktree status` shows inconsistent state between git worktrees and directories.

**Solution:** Use the cleanup command to automatically fix sync issues.

```bash
worktree cleanup  # Removes orphaned branches and references
worktree status   # Verify everything is clean
```

### Tab Completion Not Working

**Problem:** Tab completion for `worktree jump` doesn't show worktree names.

**Solutions:**

1. Ensure shell integration is set up (see above)
2. Check that `worktree-bin` is in your PATH
3. Restart your shell after setup

### Config Files Not Copying

**Problem:** Your `.env` or config files aren't appearing in new worktrees.

**Solutions:**

1. Check `.worktree-config.toml` syntax in your repo root
2. Verify file patterns match your files (use `*` for wildcards)
3. Ensure files aren't excluded by exclude patterns
4. Check that source files exist and aren't gitignored

```bash
# Debug: see what patterns are being used
worktree create --new-branch test-config
# Check if files copied to ~/.worktrees/repo/test-config/
```

### Permission Issues

**Problem:** Cannot create or access worktree directories.

**Solutions:**

```bash
# Ensure worktree directory is writable
chmod -R u+w ~/.worktrees/

# Check disk space
df -h ~/.worktrees/

# Verify directory ownership
ls -la ~/.worktrees/
```

### Back Navigation Not Working

**Problem:** `worktree back` says no origin information available.

**Solution:** This affects worktrees created before the back feature was added.

```bash
# Recreate the worktree to enable back navigation
worktree remove old-worktree --keep-branch
worktree create old-worktree
```

## Advanced Usage

### Git Configuration Inheritance

When creating a new worktree, the tool automatically inherits git configuration from the parent repository. This includes:

- User name and email settings
- Custom git aliases and configurations
- Repository-specific settings
- Credential helpers and authentication settings

This ensures that your git workflow remains consistent across all worktrees without manual configuration.

### Origin Tracking and Back Navigation

Each worktree stores metadata about its origin repository, enabling seamless navigation:

- **Automatic origin tracking**: When creating a worktree, the tool stores the path to the original repository
- **Smart back navigation**: Use `worktree back` from any worktree to return to the original repo
- **Cross-platform paths**: Handles path canonicalization and symlinks correctly (e.g., `/var` โ†’ `/private/var` on macOS)

This feature is particularly useful when working with multiple projects or when you need to quickly return to the main repository.

### Integration with IDEs

The consistent storage structure makes it easy to:

- Configure IDE project templates
- Set up automated workflows
- Create shell aliases for common operations

### Shell Aliases

Add these to your shell profile for convenience:

```bash
alias wt='worktree'
alias wtc='worktree create'
alias wtl='worktree list --current'
alias wts='worktree status'
```

## Contributing

1. Fork the repository
2. Create a feature branch: `worktree create --new-branch feature-name`
3. Make your changes
4. Test thoroughly
5. Submit a pull request

## License

MIT License - see LICENSE file for details.