feat(worktree): add /worktree slash command for interactive switching

Implements the /worktree slash command, allowing users to switch the
active session to a new isolated Git worktree without restarting the CLI.

Key changes:
- Added '/worktree' slash command with streamlined messaging.
- Updated 'Config' class to support runtime updates to the target directory.
- Introduced 'WorkingDirectoryChanged' event to notify UI and hooks of path changes.
- Added comprehensive tests and updated documentation.
This commit is contained in:
Jerop Kipruto
2026-03-18 14:35:18 -04:00
parent 94ce4aba8b
commit e5f4093058
8 changed files with 328 additions and 6 deletions

View File

@@ -38,6 +38,10 @@ Alternatively, add the following to your `settings.json`:
## How to use Git worktrees
There are two ways to use Git worktrees: at startup or mid-session.
### Using the startup flag
Use the `--worktree` (`-w`) flag to create an isolated worktree and start Gemini
CLI in it.
@@ -55,6 +59,33 @@ CLI in it.
gemini --worktree
```
### Using the slash command
If you are already in a Gemini session, you can switch to a new isolated
worktree without restarting using the `/worktree` command.
- **Switch with a specific name:**
```text
/worktree feature-auth
```
- **Switch with a random name:**
```text
/worktree
```
When you use `/worktree`, Gemini:
1. **Checks your current environment:** If you are already in a worktree, it
automatically cleans it up if there are no changes, or preserves it if there
are.
2. **Creates the new worktree:** Sets up a fresh copy of the codebase and a new
branch.
3. **Pivots the session:** Switches its working directory and reloads the
project context (memory and tools) so you can immediately start working in
isolation.
> **Note:** Remember to initialize your development environment in each new
> worktree according to your project's setup. Depending on your stack, this
> might include running dependency installation (`npm install`, `yarn`), setting