Repository navigation
08 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. croft 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.
1. Make a port worktree-aware:
[env]
APP_PORT = { base = "8080", offset = "CROFT_WORKTREE_OFFSET" }
DB_PORT = { base = "5432", offset = "CROFT_WORKTREE_OFFSET" }2. Check it in two worktrees:
# main checkout
$ croft env | grep APP_PORT
APP_PORT=8080
# in a parallel `git worktree add ../proj-feat feature/login`
$ croft env | grep APP_PORT
APP_PORT=8087The number after 8080 is the worktree's offset. Different
branch → different slug → different offset → different port.
3. (Optional) Switch between them in the TUI:
croft # opens the dashboard
W # opens the worktree switcher modal- 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 croft 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
CROFT_WORKTREE_OFFSETdirectly, or — more often — use thebase + offsetshape in[env]so derived values fall out automatically. -
Compose stacks isolate too. When
[worktrees].isolate_compose = true(the default),COMPOSE_PROJECT_NAMEis set to<project>-<slug>so each worktree gets its own containers, networks, and volumes.
[env]
APP_PORT = { base = "8080", offset = "CROFT_WORKTREE_OFFSET" }base.parse::<i64>() + CROFT_WORKTREE_OFFSET.parse::<i64>(). Pair
it with a compose file that reads ${APP_PORT} and the host
binding is unique per checkout.
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:
croft worktree assign main 0
croft worktree assign production 0
croft worktree assign feature/x 7Or directly in croft.toml:
[worktrees.assign]
main = 0
production = 0
"feature/x" = 7croft worktree assign --local writes to .croft/local.toml
instead — useful for personal overrides you don't want to share
with the team.
W in the dashboard opens a modal:
- Lists every git worktree under the repo.
- Hot-reloads croft 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 '' off
HEAD" sentinel for
git worktree add -b.
The path field auto-fills as <parent>/<slug> but Tab into it
for a manual override.
croft worktree status # current slug, offset, isolation, env values
croft worktree list # every worktree's offset, with collision warningslist flags two slugs that hash to the same offset — when that
happens, pin one of them with assign.
CROFT_WORKTREE_OFFSET and the base + offset result are only
visible inside croft's process tree. To make them available to
docker compose up (run directly), IDE launchers, etc.:
[worktrees]
dotenv = ".env"Every croft <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.
croft 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 croft.
Drop overrides into .croft/worktrees/<slug>.toml to apply only
in that checkout:
# .croft/worktrees/feature-x.toml
[env]
FEATURE_FLAG_NEW_UI = { value = "1" }These layer between croft.toml and the injected
CROFT_WORKTREE_* vars. See loading order below.
In order, first match wins:
- The current branch name.
- The linked-worktree directory basename (
git worktree add's target dir). -
det-<7-char SHA>for detached HEAD. - Empty string for non-git directories.
Slugification: [^a-z0-9-] → -, runs collapsed, leading /
trailing dashes trimmed.
[worktrees.assign][slug] if pinned, otherwise
fnv1a_32("<seed>|<slug>") % modulus. Default modulus is 1000;
seed defaults to [project].name. Empty slug → offset 0.
Per-worktree config layering, later wins:
1. Built-in defaults
2. croft.toml
3. .croft/local.toml (per-developer overrides)
4. .croft/worktrees/<slug>.toml (per-worktree overrides)
5. CROFT_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 CROFT_WORKTREE_OFFSET)
Per-worktree overlays live in the current working directory's
.croft/worktrees/<slug>.toml. Each git worktree has its own
working tree, so each maintains its own overlay file (or share
via symlink).
[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.
| Key | Source |
|---|---|
CROFT_WORKTREE_SLUG |
Slug derivation, above. |
CROFT_WORKTREE_OFFSET |
Pinned or hashed. |
COMPOSE_PROJECT_NAME |
<project>-<slug> when isolate_compose. |
| Command | Notes |
|---|---|
croft worktree status |
Current slug, offset, isolation flag, derived env values. |
croft worktree list |
Every git worktree's computed offset, with collision warnings. |
croft worktree assign <slug> <n> [--local] |
Pin a slug to an offset. |
croft worktree assign without --local writes to croft.toml
(team-wide; commit it to share). With --local, writes to
.croft/local.toml (per-developer, this checkout only).
-
Environments —
base + offsetlives there; this page covers the offset source. -
Hooks —
post-checkout/post-mergeauto-wiring for the dotenv writer. -
TUI — the
Wworktree switcher modal. -
Configuration Reference:
[worktrees].
croft is dual-licensed under MIT or Apache 2.0. Source: github.com/nsrosenqvist/croft. Back to Home.