worktree 0.1.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
# Worktree CLI

A powerful CLI tool for managing git worktrees with enhanced features including centralized storage, automatic config file synchronization, and intelligent branch management.

## Features

- 🗂️ **Centralized Storage** - Organizes worktrees in `~/.worktrees/<repo-name>/<branch-name>/`
- ⚙️ **Smart Config Management** - Automatically copies gitignored config files to new worktrees
- 🔄 **Branch Synchronization** - Keeps worktrees and git branches in sync
- 📋 **Comprehensive Status** - Shows detailed worktree and branch status
- 🎯 **Configurable Patterns** - Customize which files to copy via `.worktree-config.toml`
- 🛡️ **Safe Branch Names** - Automatically sanitizes branch names with slashes and special characters

## Installation

### 1. Build and Install the Binary

```bash
# Clone and build the project
git clone <repository-url>
cd worktree
cargo build --release

# Install the binary (choose one option)
sudo cp target/release/worktree-bin /usr/local/bin/
# OR add to your PATH
cp target/release/worktree-bin ~/.local/bin/  # ensure ~/.local/bin is in PATH
```

### 2. Set Up Shell Integration

The `worktree` command is a shell function that wraps `worktree-bin` to enable directory changing and enhanced completions. You need to set this up for your shell:

#### Bash

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

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

#### Zsh

Add to your `~/.zshrc`:

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

#### Fish

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

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

### 3. Enable Shell Completions (Optional)

For enhanced tab completions, add these to your shell config:

#### Bash

```bash
# Add to ~/.bashrc
eval "$(worktree-bin completions bash)"
```

#### Zsh

```bash
# Add to ~/.zshrc  
eval "$(worktree-bin completions zsh)"
```

#### Fish

```fish
# Add to ~/.config/fish/config.fish
worktree-bin completions fish | source
```

### 4. 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 current status
worktree status

# Create a new worktree for feature development
worktree create feature/auth

# Create a worktree with a new branch
worktree create -b new-feature

# List all worktrees
worktree list

# List worktrees for current repo only
worktree list --current

# Remove a worktree
worktree remove feature/auth

# Remove worktree and delete the branch
worktree remove feature/auth --delete-branch
```

## Commands

### `create` - Create a new worktree

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

**Options:**

- `-p, --path <PATH>` - Custom path for the worktree (optional)
- `-b, --create-branch` - Create a new branch if it doesn't exist

**Examples:**

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

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

# Create worktree at custom location
worktree create feature/auth --path ~/custom/path
```

### `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:**

- `-d, --delete-branch` - Also delete the associated branch

**Examples:**

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

# Remove worktree and delete branch
worktree remove feature/auth --delete-branch

# Remove worktree by path
worktree remove ~/.worktrees/my-project/feature-auth
```

### `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
```

## 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

```bash
# Start working on a new feature
worktree create -b feature/payments

# Work in the new worktree
cd ~/.worktrees/my-project/feature-payments

# When done, remove it
worktree remove feature/payments --delete-branch
```

### 2. Bug Fixes on Multiple Branches

```bash
# Create worktrees for different versions
worktree create release/v1.0
worktree create release/v2.0

# Apply fixes to both
# Config files are automatically synced
```

### 3. Code Review

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

# Review code without affecting main workspace
cd ~/.worktrees/my-project/pr-123

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

### 4. Development Environment Management

```bash
# Sync updated config to all worktrees
worktree sync-config main feature/auth
worktree sync-config main bugfix/critical

# Check which worktrees need attention
worktree status
```

## Troubleshooting

### Worktree exists but not in git

```bash
worktree status  # Shows inconsistent state
# Remove the directory manually and recreate
```

### Config files not copying

1. Check `.worktree-config.toml` syntax
2. Verify file patterns match your files
3. Ensure files aren't excluded by exclude patterns

### Permission issues

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

## Advanced Usage

### Custom Storage Location

Set a custom worktree storage location by modifying the storage module or using environment variables (future enhancement).

### 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 -b feature-name`
3. Make your changes
4. Test thoroughly
5. Submit a pull request

## License

MIT License - see LICENSE file for details.