Skip to content

Commit 70b07bf

Browse files
Copilotpelikhan
andcommitted
Add git worktree documentation to reference section
Co-authored-by: pelikhan <4175913+pelikhan@users.noreply.github.com>
1 parent f670834 commit 70b07bf

File tree

1 file changed

+274
-0
lines changed
  • docs/src/content/docs/reference

1 file changed

+274
-0
lines changed
Lines changed: 274 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,274 @@
1+
---
2+
title: Git Interface
3+
description: Git interface and worktree functionality for repository operations
4+
sidebar:
5+
order: 52
6+
---
7+
8+
The Git interface provides comprehensive functionality for repository operations, including support for git worktrees that allow managing multiple working directories from a single repository.
9+
10+
## Core Git Interface
11+
12+
The `git` object provides access to git repository operations through both direct methods and system scripts.
13+
14+
### Worktree Operations
15+
16+
Git worktrees allow you to check out multiple branches of the same repository simultaneously in separate working directories. This is useful for working on multiple features or comparing different branches without switching contexts.
17+
18+
#### worktreeAdd
19+
20+
Creates a new worktree at the specified path.
21+
22+
```typescript
23+
await git.worktreeAdd(path: string, commitish?: string): Promise<string>
24+
```
25+
26+
**Parameters:**
27+
- `path` - Path where the new worktree will be created
28+
- `commitish` - Optional commit, branch, or tag to checkout in the new worktree
29+
30+
**Example:**
31+
```javascript
32+
// Create a worktree for the main branch
33+
await git.worktreeAdd("/path/to/feature-worktree")
34+
35+
// Create a worktree and checkout a specific branch
36+
await git.worktreeAdd("/path/to/feature-worktree", "feature-branch")
37+
38+
// Create a worktree from a specific commit
39+
await git.worktreeAdd("/path/to/hotfix-worktree", "abc123")
40+
```
41+
42+
#### worktreeList
43+
44+
Lists all worktrees in the repository with their current state.
45+
46+
```typescript
47+
await git.worktreeList(): Promise<GitWorktree[]>
48+
```
49+
50+
**Returns:** Array of `GitWorktree` objects with the following properties:
51+
- `path` - Path to the worktree
52+
- `head` - Current commit SHA
53+
- `branch?` - Branch name (if on a branch)
54+
- `bare?` - Whether the worktree is bare
55+
- `detached?` - Whether the worktree is in detached HEAD state
56+
57+
**Example:**
58+
```javascript
59+
const worktrees = await git.worktreeList()
60+
for (const wt of worktrees) {
61+
console.log(`${wt.path}: ${wt.head}`)
62+
if (wt.branch) console.log(` Branch: ${wt.branch}`)
63+
if (wt.bare) console.log(" (bare)")
64+
if (wt.detached) console.log(" (detached)")
65+
}
66+
```
67+
68+
#### worktreeRemove
69+
70+
Removes a worktree from the repository.
71+
72+
```typescript
73+
await git.worktreeRemove(path: string, force?: boolean): Promise<string>
74+
```
75+
76+
**Parameters:**
77+
- `path` - Path of the worktree to remove
78+
- `force` - Force removal even if worktree is dirty (default: false)
79+
80+
**Example:**
81+
```javascript
82+
// Remove a clean worktree
83+
await git.worktreeRemove("/path/to/feature-worktree")
84+
85+
// Force remove a dirty worktree
86+
await git.worktreeRemove("/path/to/feature-worktree", true)
87+
```
88+
89+
#### worktreePrune
90+
91+
Cleans up stale worktree information for worktrees that have been deleted manually.
92+
93+
```typescript
94+
await git.worktreePrune(): Promise<string>
95+
```
96+
97+
**Example:**
98+
```javascript
99+
// Clean up stale worktree references
100+
await git.worktreePrune()
101+
```
102+
103+
## System Script: git_worktree
104+
105+
The `git_worktree` system script provides LLM-accessible tools for managing git worktrees. Include it in your script to enable AI assistance with worktree operations.
106+
107+
```javascript
108+
script({
109+
title: "Worktree Management",
110+
system: ["git_worktree"]
111+
})
112+
113+
$`List all worktrees and create a new one for the feature branch`
114+
```
115+
116+
### Available Tools
117+
118+
When the `git_worktree` system script is included, the following tools become available to the LLM:
119+
120+
#### git_worktree_list
121+
122+
Lists all worktrees in a human-readable format.
123+
124+
**Parameters:** None
125+
126+
**Usage:**
127+
```javascript
128+
// The LLM can call this tool to see current worktrees
129+
// Example output:
130+
// /repo/main (abc123), branch: refs/heads/main
131+
// /repo/feature (def456), branch: refs/heads/feature-branch
132+
// /repo/hotfix (ghi789), detached
133+
```
134+
135+
#### git_worktree_add
136+
137+
Creates a new worktree with optional branch specification.
138+
139+
**Parameters:**
140+
- `path` (required) - Path where the new worktree will be created
141+
- `commitish` (optional) - Commit, branch, or tag to checkout
142+
143+
**Usage:**
144+
```javascript
145+
// The LLM can create worktrees based on conversation context
146+
// Examples:
147+
// - "Create a worktree at /tmp/feature for the feature-branch"
148+
// - "Set up a worktree for testing the hotfix commit abc123"
149+
```
150+
151+
#### git_worktree_remove
152+
153+
Removes an existing worktree with safety controls.
154+
155+
**Parameters:**
156+
- `path` (required) - Path of the worktree to remove
157+
- `force` (optional) - Force removal even if dirty (default: false)
158+
159+
**Usage:**
160+
```javascript
161+
// The LLM can safely remove worktrees
162+
// Will prompt before force removal if worktree is dirty
163+
```
164+
165+
#### git_worktree_prune
166+
167+
Cleans up stale worktree information.
168+
169+
**Parameters:** None
170+
171+
**Usage:**
172+
```javascript
173+
// The LLM can maintain worktree hygiene
174+
// Removes references to manually deleted worktrees
175+
```
176+
177+
### Configuration
178+
179+
The system script supports a `cwd` parameter to operate on repositories other than the current working directory:
180+
181+
```javascript
182+
script({
183+
system: ["git_worktree"],
184+
systemVars: {
185+
"system.git_worktree.cwd": "/path/to/different/repo"
186+
}
187+
})
188+
```
189+
190+
## Use Cases
191+
192+
### Feature Development
193+
194+
```javascript
195+
// Create isolated environments for different features
196+
await git.worktreeAdd("/workspace/feature-auth", "feature-auth")
197+
await git.worktreeAdd("/workspace/feature-ui", "feature-ui")
198+
199+
// Work on features independently without context switching
200+
```
201+
202+
### Release Management
203+
204+
```javascript
205+
// Maintain multiple release versions simultaneously
206+
await git.worktreeAdd("/releases/v1.0", "release/v1.0")
207+
await git.worktreeAdd("/releases/v2.0", "release/v2.0")
208+
209+
// Apply hotfixes to specific versions
210+
```
211+
212+
### Code Review
213+
214+
```javascript
215+
// Create worktree for reviewing pull requests
216+
await git.worktreeAdd("/review/pr-123", "origin/pull/123/head")
217+
218+
// Compare changes side by side with main branch
219+
const worktrees = await git.worktreeList()
220+
```
221+
222+
### Testing and CI
223+
224+
```javascript
225+
script({
226+
system: ["git_worktree"]
227+
})
228+
229+
$`Create separate worktrees for testing each feature branch,
230+
run tests in parallel, and report results for each branch.`
231+
```
232+
233+
## Integration with Other Tools
234+
235+
Worktrees work seamlessly with other GenAIScript features:
236+
237+
```javascript
238+
// Use with file operations
239+
const clone = git.client("/path/to/worktree")
240+
const files = await clone.listFiles("modified")
241+
242+
// Combine with diff operations
243+
const diff = await clone.diff({ staged: true })
244+
245+
// Access worktree-specific logs
246+
const commits = await clone.log()
247+
```
248+
249+
## Best Practices
250+
251+
1. **Clean up unused worktrees** - Use `worktreeRemove` when done with development
252+
2. **Use descriptive paths** - Make worktree purposes clear from their paths
253+
3. **Regular pruning** - Run `worktreePrune` to maintain repository cleanliness
254+
4. **Force removal sparingly** - Only use force when you're certain data loss is acceptable
255+
5. **System script integration** - Leverage AI assistance for complex worktree workflows
256+
257+
## Error Handling
258+
259+
Worktree operations may fail for various reasons:
260+
261+
```javascript
262+
try {
263+
await git.worktreeAdd("/existing/path")
264+
} catch (error) {
265+
// Handle path already exists, invalid branch, etc.
266+
console.error("Worktree creation failed:", error.message)
267+
}
268+
```
269+
270+
Common error scenarios:
271+
- Path already exists or is not empty
272+
- Invalid branch or commit reference
273+
- Insufficient permissions
274+
- Worktree is locked or in use

0 commit comments

Comments
 (0)