bonsai

command module
v0.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 7, 2026 License: MIT Imports: 7 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

             &&& &&  &&&
          &&&\/&\|&()|/@,&&&
          &&\/(/&/&||/&_/)_&&
         &&/&_&&/|&&/&/__%_/_&
          &&& &&|&&|/&&_%_&&&&
            &---&&\&\|&/&--&
                 \|||/
                  |||
                  |||
                \ ||| /
            ~-=-~^-'^`-~-=-~

bonsai

A terminal UI for growing and pruning git worktrees.

Manage worktrees, sync files, run scripts, and drive git — without leaving your keyboard.


Why

Juggling git worktrees by hand is tedious: creating them, copying over untracked files like .env, running dev servers per branch, tracking which one has an open PR, and cleaning them up. bonsai puts all of that in one fast TUI — and every feature is also a plain CLI command you can script.

Features

  • 🌳 Worktree list with ahead/behind arrows (↑N ↓M) and #PR badges auto-detected via gh.
  • ✨ Create worktrees from a new branch, an existing branch, or a GitHub PR.
  • 📋 Copy files from main into a worktree via fuzzy finder — ranked by how often you copy them.
  • 🔧 Run package scripts and custom aliases as background processes — many per worktree, switchable.
  • 🪝 Lifecycle hooks (on_worktree_create / on_worktree_delete) with {variable} and $BONSAI_* substitution.
  • 🔀 Git ops inline: pull, push, commit, rebase (drops to a real shell for conflict resolution).
  • 🗑️ Prune with an optional merge-PR-first step and a clear preview of exactly what will happen.
  • 🖥️ Everything works from the CLI too — bonsai create, bonsai copy, bonsai x <alias>, and more.

Install

./install.sh

Builds the binary and installs it to /usr/local/bin (or ~/.local/bin if that isn't writable). Override the location with PREFIX:

PREFIX="$HOME/.local" ./install.sh
With Go
go install github.com/Tiago-0liveira/bonsai@latest
From source
go build -o bonsai .

Requirements

  • Go 1.26+ (to build)
  • git on your PATH
  • gh (optional) — only needed for PR badges, creating worktrees from PRs, and merge-on-prune. Everything else works without it.

Quick start

cd your-repo
bonsai

Press n to create a worktree, enter to drop into a shell in the selected one, ? for the full keymap, q to quit.

Keybindings

Key Action
tab / shift+tab Cycle focus between panes
enter Open a shell in the selected worktree
n New worktree (new branch / existing branch / PR)
c Copy a file from main into the worktree (fuzzy)
s Run a package.json script
p Aliases menu (run one, or + new alias)
v Switch which process's output is shown
ctrl+p Git pull
ctrl+u Git push
C Git commit (stages all)
r Git rebase onto a chosen branch
x Prune the worktree (with optional PR merge)
R Refresh worktrees, metrics, and PRs
? Toggle full help
q / ctrl+c Quit

CLI

Any argument runs a subcommand instead of the TUI:

bonsai list                     # list worktrees (branch, path)
bonsai create <branch>          # new worktree on a new branch → prints its path
bonsai create --existing <br>   # new worktree on an existing branch
bonsai create --pr <number>     # new worktree checked out from a GitHub PR
bonsai copy <file> <worktree>   # copy a main-repo file into a worktree
bonsai path <branch|main>       # print a worktree's path (for cd)
bonsai x <alias> [worktree]     # run an alias (default: current directory)
bonsai x --list                 # list available aliases
bonsai alias add <name> <cmd…>  # add a user alias
bonsai alias list               # list aliases
bonsai alias rm <name>          # remove a user alias
bonsai shell-init               # print a shell 'bcd' cd helper
bonsai help                     # full usage
Jump into a worktree from your shell

A binary can't change its parent shell's directory, so bonsai ships a helper:

eval "$(bonsai shell-init)"     # add to ~/.zshrc or ~/.bashrc
bcd feat/login                  # cd into that worktree
bcd                             # cd back to main

Configuration

bonsai reads .bonsai.yaml from your repo root. See the annotated example — it documents every field. In short:

upstream: origin/main          # base ref for ↑ahead/↓behind and prune's merge target

hooks:
  on_worktree_create:
    - "cp {main}/.env {new_worktree}/.env"
    - "npm install"
  on_worktree_delete:
    - "echo 'removing {worktree_name}'"

aliases:
  - name: dev
    command: "npm run dev"
Hook & alias variables

Use {name} inside hook commands and alias commands. Each is also exported to hooks as $BONSAI_<NAME> (uppercased).

Variable Value
{new_worktree} / {worktree} Absolute path of the worktree
{worktree_name} Worktree directory name (e.g. repo-feat-login)
{main} / {main_branch_path} Absolute path of the main worktree
{branch} Branch name
{base_branch} Base branch from upstream (origin/main → main)
{repo} Repository directory name
{pr_number} Connected PR number, or empty

User aliases you add in-app or via bonsai alias add persist to ~/.config/bonsai/state.json (which also tracks file-copy frequency for fuzzy ranking). Aliases in .bonsai.yaml are read-only project defaults.

How prune works

x (prune) is local teardown and is destructive. The confirmation modal previews the exact pipeline, e.g.:

merge PR #20 to main → run on_worktree_delete → delete worktree → delete branch
  • The merge step is off by default — toggle it with space. It only appears for branches with a connected PR and runs gh pr merge.
  • delete worktree uses --force; delete branch uses -D. Unpushed commits and uncommitted changes are lost.
  • It never touches the remote unless you explicitly enable merge.

Architecture

Strict split between presentation and core:

  • internal/ui — all Bubble Tea (model / update / view / components). No os/exec, file I/O, or raw git.
  • internal/core — git, gh, config, exec, fs, pkgmgr. Plain Go types and errors, independently testable.
  • internal/cli — the non-interactive subcommands, sharing the same core.

Built with Bubble Tea, Lip Gloss, Viper, and sahilm/fuzzy.

License

MIT

Documentation

Overview

Command bonsai is a git worktree manager TUI. main wires the core layer (config, state, git discovery) into the Bubble Tea presentation layer.

Directories

Path Synopsis
internal
cli
Package cli implements bonsai's non-interactive subcommands.
Package cli implements bonsai's non-interactive subcommands.
core/config
Package config loads the .bonsai.yaml project config via viper and persists mutable state (file-copy frequency, aliases) to the user config directory.
Package config loads the .bonsai.yaml project config via viper and persists mutable state (file-copy frequency, aliases) to the user config directory.
core/exec
Package exec manages independent background subprocesses.
Package exec manages independent background subprocesses.
core/fs
Package fs handles fuzzy file search over the main repo tree and copying files from the main repo into a worktree.
Package fs handles fuzzy file search over the main repo tree and copying files from the main repo into a worktree.
core/gh
Package gh wraps the GitHub CLI (`gh`) for the operations bonsai needs.
Package gh wraps the GitHub CLI (`gh`) for the operations bonsai needs.
core/git
Package git wraps the Git CLI.
Package git wraps the Git CLI.
core/pkgmgr
Package pkgmgr parses project package manifests and extracts runnable scripts.
Package pkgmgr parses project package manifests and extracts runnable scripts.
ui
Package ui is the Bubble Tea presentation layer.
Package ui is the Bubble Tea presentation layer.
ui/components/modals
Package modals provides a single reusable centered overlay that operates in one of several modes: free text input, static selection, live fuzzy search, or yes/no confirmation.
Package modals provides a single reusable centered overlay that operates in one of several modes: free text input, static selection, live fuzzy search, or yes/no confirmation.
ui/components/terminal
Package terminal is the right-pane viewport showing process output or git log.
Package terminal is the right-pane viewport showing process output or git log.
ui/components/worktreelist
Package worktreelist is the left-pane list of git worktrees.
Package worktreelist is the left-pane list of git worktrees.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL