Skip to content
ciemborPublic

About

Automatic, isolated development environments for Git branches and worktrees. Switch between branches and run multiple versions of your application side by side without port conflicts, shared databases, or manual environment management.

Topics

Resources

Stars

9 stars

Watchers

0 watching

Forks

Repository files navigation

Brunch

Brunch logo — container per worktree and branch

Brunch runs isolated development environments for Git branches and worktrees.

Each branch gets its own environment. With Compose, that means a separate project, network, containers, and named volumes. Branches checked out in the same worktree share its host port, while additional worktrees get different ports and can run in parallel.

Docker Compose is the default manager. Podman Compose, local processes, and custom project commands are also supported.

Requirements

  • Ruby 3.1+
  • Git 2.28+ (2.39.3+ for ghe worktree lifecycle events)
  • Docker Compose, Podman Compose, or another supported manager
  • git-hooks-ext 0.6.0+ for reliable branch deletion events

Installation

Install git-hooks-ext so that ghe is available on your PATH, then install Brunch:

gem install brunch

Inside each repository you want Brunch to manage:

brunch install

Brunch installs the git-hooks-ext bridge and its own event hooks without overwriting hooks owned by another tool. After upgrading Brunch or git-hooks-ext, run brunch install again in each managed repository to refresh its hooks and bridge.

Configuration

Add brunch.yml to the repository root.

For Docker Compose:

compose_file: compose.yaml

For a Rails app with SQLite databases in storage/, add Dockerfile.dev:

FROM ruby:3.4-slim

WORKDIR /rails

RUN apt-get update -qq && \
    apt-get install --no-install-recommends -y build-essential git libsqlite3-dev libvips libyaml-dev pkg-config && \
    rm -rf /var/lib/apt/lists/*

COPY Gemfile Gemfile.lock ./
RUN bundle install
COPY . .

Use the Ruby version required by your application. Then add compose.yaml:

services:
  web:
    build:
      context: .
      dockerfile: Dockerfile.dev
    command: sh -c 'bin/rails db:prepare && exec bin/rails server -b 0.0.0.0 -p 3000'
    environment:
      RAILS_ENV: development
    volumes:
      - .:/rails
      - storage:/rails/storage
    ports:
      - "127.0.0.1:${BRUNCH_PORT}:3000"

volumes:
  storage:

The bind mount makes source edits visible to Rails. The named volume keeps the SQLite database separate for each branch and preserves it across container rebuilds. If your app stores its database elsewhere, mount that location instead. Brunch runs on the host, so it does not need to be in the Rails Gemfile. After changing Gemfile or Gemfile.lock, run brunch restart to rebuild the image with the new gems; the named volume remains intact.

The first activated worktree prefers port 3000. Additional worktrees receive another free port.

To prefer a different starting port:

preferred_port: 4000

Port assignments are persisted per worktree in .git/brunch/state.json.

Existing Compose projects with several ports

By default Brunch manages the BRUNCH_PORT variable shown above. To run an existing Compose project that publishes several fixed host ports, opt in with:

ports: auto

Brunch renders the current Compose configuration, reserves every published host port, and writes a private generated configuration under .git/brunch/. It never changes the repository's compose.yaml. The first worktree keeps the declared ports whenever they are free; additional worktrees receive free ports for every published service port. Container ports can use the full range from 1 to 65535. When a declared host port is unavailable, Brunch chooses a free host port from 1024 to 49151.

For example, this requires no variables in the Compose file:

services:
  web:
    ports:
      - "3000:3000"
      - "5173:5173"
  mail:
    ports:
      - "8025:8025"

brunch ports displays all assigned mappings. Query one mapping with:

brunch port web 3000

If the same service publishes one container port more than once, brunch ports shows each host port. brunch port SERVICE CONTAINER-PORT returns the first one.

Only ports: entries that publish a host port are managed; internal service ports and expose: remain unchanged. BRUNCH_PORT continues to contain the first published port for compatibility with existing scripts.

Getting started

Commit brunch.yml and the application configuration first. The repository must contain at least one commit.

Then run:

brunch install
brunch doctor
brunch activate
brunch status

brunch activate starts the environment for the currently checked-out branch.

To print only the current worktree's port:

brunch port

Open the application on the printed port, for example:

http://127.0.0.1:3000

With Compose, the application must listen on the container port mapped in the Compose file.

Branch switching

Once the hooks are installed, normal Git branch switches automatically stop the previous branch environment and start the new one:

git switch -c feature/login
git switch main

Branches checked out in the same worktree reuse that worktree's host port.

With Compose, each branch keeps its own Compose project and named volumes, so persistent resources remain isolated between branches. Returning to a previously activated branch starts its stopped containers again, preserving files written inside them. The branch keeps the same host port.

If a resumed Compose application exits immediately, Brunch recreates that environment automatically. This recovers the worktree but discards files kept only in the container's writable layer; named volumes remain intact.

Applications that write a PID file inside the container should remove it before starting. For example, a Rails Compose command can begin with rm -f /tmp/rails-server.pid &&. A stopped container keeps its writable layer, including a stale PID file, until it is recreated.

To manually start the environment after brunch stop:

brunch activate

To rebuild and recreate its containers explicitly:

brunch restart

brunch restart may discard data stored only in a container's writable layer. Put important data, such as databases, in named volumes.

Compose builds use files from the live worktree, including uncommitted changes.

If you want source edits to appear in an already running container without rebuilding, configure a bind mount or another reload mechanism in your Compose setup.

Parallel worktrees

Additional worktrees run independently on different host ports.

Create them with git-hooks-ext:

ghe worktree add -b feature-a ../feature-a
ghe worktree add -b feature-b ../feature-b

Then:

cd ../feature-a

brunch port
brunch ports

brunch port prints the current worktree's port.

brunch ports groups the current and previously activated branches under each worktree. Stopped branches appear in gray and retain their worktree's port for the next activation.

brunch watch

brunch watch shows the same view and refreshes it every second. Press Ctrl-C to stop watching.

Switching branches inside one worktree does not affect environments running in other worktrees.

For reliable automatic handling of forced branch renames such as git branch -M source target, use a Git version supported by ghe doctor and run brunch install after upgrading git-hooks-ext.

If you create a worktree with plain Git:

git worktree add -b feature-c ../feature-c

activate Brunch manually inside it:

cd ../feature-c
brunch activate

After moving or removing worktrees with plain Git, run:

brunch cleanup

Brunch never deletes the worktree source directory. It removes environments for worktrees that Git no longer reports; it does not act as a general-purpose command for deleting the current worktree's stopped containers or volumes.

Managers

Docker Compose

Docker Compose is the default manager:

compose_file: compose.yaml

If Compose cannot stop or tear down an environment, Brunch keeps its state and control files so the operation can be retried. Recreating an environment keeps named volumes; removing it deletes them.

Podman Compose

Podman Compose uses the same Compose file contract:

manager: podman_compose
compose_file: compose.yaml

Local process

Use local_process for a development command such as bin/dev:

manager: local_process
command: bin/dev

Brunch starts and stops the process together with the branch environment and stores its output under .git/brunch/controls. It records the process start time and checks its process group before sending a stop signal, then waits for the group to exit. If a running environment was saved by an older Brunch version without process identity, Brunch reports its status as unknown and does not signal or start another process until that process has exited.

Custom commands

Use the command manager to integrate Brunch with project-specific tooling:

manager: command
commands:
  create: bin/environment create
  start: bin/environment start
  stop: bin/environment stop
  remove: bin/environment remove

Brunch runs these commands from the live worktree with:

BRUNCH_REF
BRUNCH_PORT
BRUNCH_PROJECT
BRUNCH_SNAPSHOT

BRUNCH_SNAPSHOT points to the live worktree for compatibility with manager adapters.

create is optional.

start must return after launching the environment.

stop is called when switching away from the active branch.

When a worktree environment is deleted, Brunch runs stop first, then remove to clean up manager-owned persistent resources. If either command fails, Brunch keeps its managed files and state so the operation can be retried.

Optional commands:

commands:
  status: bin/environment status
  health: bin/environment health
  logs: bin/environment logs

These power the corresponding Brunch operations.

Manager status has four meanings: running confirms a live environment, stopped confirms it is stopped, unknown means the manager cannot verify it, and unavailable means the manager cannot be used. The custom command manager reports unknown unless a status command is configured. For a saved running environment with unknown status, another activation leaves it alone rather than starting a possible duplicate. A saved stopped environment may be resumed. Compose reports unknown if its container status query fails; a successful start means the manager accepted the start command, while resume also checks that the container stays running briefly.

Worktree lifecycle

A branch checkout stops the previous environment in that worktree and starts the new one on the same host port.

Other worktrees continue running.

Removing a worktree removes its Brunch environments. Brunch keeps the information needed to shut down manager resources under .git/brunch/controls, so cleanup can still run after the worktree itself is gone.

If cleanup fails partway through multiple branch environments, Brunch records each successful removal. Running cleanup again processes only those still present in state. Moving a worktree also records each environment after a successful stop, so a retry skips environments already stopped before a later stop failed.

Brunch writes activation checkpoints to .git/brunch/state.json. After an interrupted start, the next activation checks the manager: a confirmed running environment is recorded as running without starting it again. A confirmed stopped environment can be retried. If status cannot be established, Brunch avoids starting a second copy automatically. An invalid state file is rejected and left intact for inspection.

If a custom remove command depends on project files, those files must be committed because cleanup after worktree removal uses the last committed state.

brunch cleanup removes environments belonging to worktrees that no longer exist.

Port assignments stay associated with a worktree and are checked against other Brunch reservations and visible listeners when first allocated. For a worktree without an active environment, Brunch also checks a saved Compose port before reusing it and selects another port if an unrelated process has claimed it. Saved ports of active environments stay reserved to avoid changing their published addresses during activation. An unrelated process can still claim a port between that check and manager startup; Brunch's file locks cannot prevent that external race. In that case, stop the conflicting process and retry activation.

With git-hooks-ext 0.6.0+, deleting a local branch queues its Brunch environments for removal. A detached worker waits for the Git process that triggered the hook to exit, then checks the final refs and reflogs. A rename keeps its environment under the new name; a deletion removes the managed resources, including Compose containers and volumes. A Brunch command run while the identified Git process is still working leaves the queued environment alone. If the process cannot be identified or the worker cannot start, the next regular Brunch command after Git exits, such as brunch status or brunch cleanup, performs the check. Brunch also checks for missing branches when Git emits no deletion event, on the next regular command. If Git reflogs are unavailable and Brunch cannot distinguish a rename from a deletion, it keeps the environment. A later brunch restart recreates the container in the same Compose project, preserving named volumes. For git branch -M old target, when target already has a Brunch environment, Brunch removes that overwritten environment and moves old to target.

Commands

brunch install

Install Brunch hooks for the repository.

brunch activate

Start the environment for the current branch.

brunch status

Show the current environment, manager status, and port.

brunch port

Print the current worktree's port.

brunch list

List current and stopped branch environments grouped by worktree.

brunch watch

Continuously refresh the branch-environment list in an interactive terminal.

brunch stop

Stop the active environment without removing it.

brunch restart

Recreate and start the active environment.

brunch logs
brunch logs --follow

Show environment logs. For a local process, the default command prints the last 100 lines without loading the whole log into memory.

brunch run -- bin/rails console

Run a command on the host from the live worktree.

brunch doctor

Check Git, installed hooks, configuration, manager availability, and ports.

brunch cleanup

Remove environments belonging to deleted worktrees and process pending branch deletions.

Upgrading from older configurations

Before upgrading from an older branch-only configuration, stop its running environment and clean up its legacy state.

Brunch will not reinterpret an existing non-empty branch-only state as worktree state.

Development

CLI composes the application and handles exit codes. CommandRouter dispatches worktree commands, Doctor returns diagnostics, and Reporter writes lifecycle messages. Configuration, GitRepository, StateRepository, and PortAllocator can be injected through the CLI constructor. EnvironmentLifecycle coordinates activation and maintenance, while BranchReconciler handles branch events. Managers::MANAGER_TYPES is the shared registry for construction and manager-specific configuration validation. Operations acquire locks in this order: branch, worktree, then state. Never acquire an earlier lock while holding a later one. The state lock protects short read-modify-write transactions; manager stop commands and port availability probes run while holding the relevant worktree lock, outside the global state lock. Port reservations are checked again against the latest state before committing, and a conflicting new reservation is retried. State writes use a unique temporary file, synchronize its contents, rename it atomically, and synchronize the containing directory where supported.

bundle install
bin/install-pre-commit
bundle exec rake quality
gem build brunch.gemspec

Run container integration tests with Docker Compose:

BRUNCH_INTEGRATION_MANAGER=docker_compose bundle exec rake test

Or Podman Compose:

BRUNCH_INTEGRATION_MANAGER=podman_compose bundle exec rake test

Without BRUNCH_INTEGRATION_MANAGER, container integration tests are skipped by the local quality check.

The pre-commit hook runs bundle exec rake quality without editing files. That checks RuboCop, Reek, and the full standard test suite. SimpleCov requires 100% line and branch coverage for lib/**/*.rb. RuboCop complexity limits apply to production code; scenario tests remain subject to its style and lint checks.

About

Automatic, isolated development environments for Git branches and worktrees. Switch between branches and run multiple versions of your application side by side without port conflicts, shared databases, or manual environment management.

Topics

Resources

Stars

9 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages