# Worktrees
If you use `git worktree add` to keep more than one branch of the
same project checked out at once, you've probably hit the
port-collision problem: both stacks try to bind `8080`, both
docker-compose projects share the same containers, both produce
the same `COMPOSE_PROJECT_NAME`. `ampelos` solves this by detecting
the current checkout and giving each one a deterministic
**offset** you can subtract from port numbers and use to scope
container names.
## Quickstart
**1. Make a port worktree-aware:**
```toml
[env]
APP_PORT = { base = "8080", offset = "AMPELOS_WORKTREE_OFFSET" }
DB_PORT = { base = "5432", offset = "AMPELOS_WORKTREE_OFFSET" }
```
**2. Check it in two worktrees:**
```sh
# main checkout
# in a parallel `git worktree add ../proj-feat feature/login`
```
The number after `8080` is the worktree's offset. Different
branch → different slug → different offset → different port.
**3. (Optional) Switch between them in the TUI:**
```sh
ampelos # opens the dashboard
W # opens the worktree switcher modal
```
## Mental model
- **Each worktree has a slug.** Derived from the branch name (or
the worktree directory basename, or a short SHA for detached
HEAD). It's how ampelos identifies "which checkout am I in."
- **Each slug has an offset.** Either pinned in
`[worktrees.assign]` or hashed from the slug + a per-project
seed. Default modulus is 1000, so offsets live in 0..999.
- **You consume the offset.** Read `AMPELOS_WORKTREE_OFFSET`
directly, or — more often — use the `base + offset` shape in
`[env]` so derived values fall out automatically.
- **Compose stacks isolate too.** When
`[worktrees].isolate_compose = true` (the default),
`COMPOSE_PROJECT_NAME` is set to `<project>-<slug>` so each
worktree gets its own containers, networks, and volumes.
## Common tasks
### Compute a per-worktree port
```toml
[env]
APP_PORT = { base = "8080", offset = "AMPELOS_WORKTREE_OFFSET" }
```
`base.parse::<i64>() + AMPELOS_WORKTREE_OFFSET.parse::<i64>()`. Pair
it with a compose file that reads `${APP_PORT}` and the host
binding is unique per checkout.
### Pin a specific offset to a specific branch
If you want `main` to always be offset `0` and `production` also
`0` (because you never run them together), but `feature/x` to be
`7`:
```sh
ampelos worktree assign main 0
ampelos worktree assign production 0
ampelos worktree assign feature/x 7
```
Or directly in `ampelos.toml`:
```toml
[worktrees.assign]
main = 0
production = 0
"feature/x" = 7
```
`ampelos worktree assign --local` writes to `.ampelos/local.toml`
instead — useful for personal overrides you don't want to share
with the team.
### Switch between worktrees inside the TUI
`W` in the dashboard opens a modal:
- Lists every git worktree under the repo.
- Hot-reloads ampelos into the chosen one without restarting.
- The "+ new worktree" entry opens a branch-first picker — type
to filter local + remote branches, pick one to attach an
existing checkout, or take the **"create branch '<input>' off
HEAD"** sentinel for `git worktree add -b`.
The path field auto-fills as `<parent>/<slug>` but Tab into it
for a manual override.
### See what's resolved
```sh
ampelos worktree status # current slug, offset, isolation, env values
ampelos worktree list # every worktree's offset, with collision warnings
```
`list` flags two slugs that hash to the same offset — when that
happens, pin one of them with `assign`.
### Share env with tools outside ampelos
`AMPELOS_WORKTREE_OFFSET` and the `base + offset` result are only
visible inside ampelos's process tree. To make them available to
`docker compose up` (run directly), IDE launchers, etc.:
```toml
[worktrees]
dotenv = ".env"
```
Every `ampelos <anything>` invocation rewrites the managed block in
`.env`. Idempotent: when the content already matches, the file's
mtime stays put, so file watchers and `git status` don't churn.
`ampelos hooks install` (without an explicit `--stages` list) also
auto-adds `post-checkout` and `post-merge` shims so `.env` stays
fresh after a branch switch even when the developer skips ampelos.
### Per-worktree config overrides
Drop overrides into `.ampelos/worktrees/<slug>.toml` to apply only
in that checkout:
```toml
# .ampelos/worktrees/feature-x.toml
[env]
FEATURE_FLAG_NEW_UI = { value = "1" }
```
These layer between `ampelos.toml` and the injected
`AMPELOS_WORKTREE_*` vars. See [loading order](#loading-order) below.
## Reference
### Slug derivation
In order, first match wins:
1. The current branch name.
2. The linked-worktree directory basename (`git worktree add`'s
target dir).
3. `det-<7-char SHA>` for detached HEAD.
4. Empty string for non-git directories.
Slugification: `[^a-z0-9-]` → `-`, runs collapsed, leading /
trailing dashes trimmed.
### Offset assignment
`[worktrees.assign][slug]` if pinned, otherwise
`fnv1a_32("<seed>|<slug>") % modulus`. Default modulus is `1000`;
seed defaults to `[project].name`. Empty slug → offset `0`.
### Loading order
Per-worktree config layering, later wins:
```
1. Built-in defaults
2. ampelos.toml
3. .ampelos/local.toml (per-developer overrides)
4. .ampelos/worktrees/<slug>.toml (per-worktree overrides)
5. AMPELOS_WORKTREE_SLUG / _OFFSET injected
6. COMPOSE_PROJECT_NAME injected (if isolate_compose, slug non-empty,
user hasn't already set it)
7. [env] resolution (can reference AMPELOS_WORKTREE_OFFSET)
```
Per-worktree overlays live in the *current working directory's*
`.ampelos/worktrees/<slug>.toml`. Each git worktree has its own
working tree, so each maintains its own overlay file (or share
via symlink).
### Compose isolation
`[worktrees].isolate_compose = true` (the default) sets
`COMPOSE_PROJECT_NAME = <project>-<slug>` so each worktree's
docker-compose stack is independent — separate containers,
networks, and volumes. Skipped when the user has already set
`COMPOSE_PROJECT_NAME` themselves, so an explicit override always
wins.
### Built-in env variables (injected ahead of `[env]`)
| Key | Source |
|---|---|
| `AMPELOS_WORKTREE_SLUG` | Slug derivation, above. |
| `AMPELOS_WORKTREE_OFFSET` | Pinned or hashed. |
| `COMPOSE_PROJECT_NAME` | `<project>-<slug>` when `isolate_compose`. |
## Command reference
| Command | Notes |
|---|---|
| `ampelos worktree status` | Current slug, offset, isolation flag, derived env values. |
| `ampelos worktree list` | Every git worktree's computed offset, with collision warnings. |
| `ampelos worktree assign <slug> <n> [--local]` | Pin a slug to an offset. |
`ampelos worktree assign` without `--local` writes to `ampelos.toml`
(team-wide; commit it to share). With `--local`, writes to
`.ampelos/local.toml` (per-developer, this checkout only).
## See also
- [Environments](Environments) — `base + offset` lives there;
this page covers the offset source.
- [Hooks](Hooks) — `post-checkout` / `post-merge` auto-wiring for
the dotenv writer.
- [TUI](TUI) — the `W` worktree switcher modal.
- [Configuration Reference: `[worktrees]`](Configuration-Reference#worktrees).