Skip to content

About

Visualize the fork tree of your Claude Code sessions and jump to any branch — with zero LLM tokens. It's pure static local code; no model round-trip is involved.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Latest commit

 

History

16 Commits

Folders and files

Repository files navigation

branch-graph

Visualize the fork tree of your Claude Code sessions and jump to any branch — with zero LLM tokens. It's pure static local code; no model round-trip is involved.

This is the Rust implementation: one binary, no runtime to install, and fast enough that a 284 MB project scans in about a tenth of a second.

When you run /branch (or claude --fork-session), Claude Code creates a new session that copies the history up to a divergence point. Over time a project grows a tree of forked sessions, but the built-in /resume picker is a flat list that doesn't show which session branched from which. branch-graph reconstructs and draws that tree.

Branches in my-project

6  ● a4f3d8c2  Draft initial README outline
5  ● 9b6e1f47  Design payment retry logic
4  ├─● c8a2b91d  stripe-webhooks
3  │  ├─● 5e7f3a06  Add signature verification for Stripe webhooks
1  │  └─● 1d4c9b82  Handle partial refunds and capture expiration (most recent)
2  └─● 7f2e8c15  paypal-webhooks

The number on each row is a recency rank, not a row position: 1 is the branch you were in most recently, 2 the one before it, and so on. So branch-graph 1 always resumes the newest branch. The tree itself is drawn oldest-first, so a fork always sits under what it came from — which is why the number column doesn't run in order down the page.

Install

curl -fsSL https://raw.githubusercontent.com/KCSAbeywickrama/branch-graph/rust/install.sh | sh

Downloads a prebuilt binary for your platform, verifies its SHA-256 against the release checksums, then installs branch-graph into ~/.local/bin. No Rust toolchain needed.

Prebuilt binaries cover macOS (Apple Silicon and Intel) and Linux (x86_64 and arm64, statically linked). Any other platform builds from source automatically, which does need a Rust toolchain (1.70 or newer — rustup.rs).

BINDIR=/usr/local/bin ./install.sh   # choose another bin dir
VERSION=v1.0.0 ./install.sh          # pin a specific release
FROM_SOURCE=1 ./install.sh           # skip the download, build locally

This also installs cbg, a short alias for branch-graph — use whichever you prefer, they're interchangeable everywhere in this README.

If the install script warns that the bin dir isn't on your PATH, add it (e.g. to ~/.zshrc):

export PATH="$HOME/.local/bin:$PATH"

Uninstall:

./uninstall.sh

Working on the code

install-dev.sh points ~/.local/bin/branch-graph at target/release/branch-graph with a symlink instead of copying it, so cargo build --release updates the installed command in place with no reinstall while you work:

./install-dev.sh

(cargo clean removes what the symlink points at, so rebuild or re-run it after one.)

Verifying a download

Every release ships a SHA256SUMS file, which install.sh checks before it installs anything. To check a manual download yourself:

sha256sum -c SHA256SUMS --ignore-missing

The binaries are ad-hoc signed, not notarized by Apple. curl, wget and git never set macOS's quarantine attribute, so an installed binary runs as-is — but a browser download is quarantined, and Gatekeeper blocks it until you clear that:

xattr -d com.apple.quarantine ./branch-graph

Interactive picker (separate terminal)

The main way to use branch-graph. Run it in a normal terminal (not inside Claude Code) and it opens an interactive picker over your project's fork tree:

branch-graph   # or the short alias: cbg

Here's the same layout and spacing as the real implementation, with an example branch tree picked to show every kind of label branch-graph can display — the branch list on top, a detail panel for the selected branch below it, updating live as you move the selection:

Branches in my-project

8  ● a4f3d8c2  Draft initial README outline  10/07  9:14AM
7  ● 9b6e1f47  Design payment retry logic  12/07  4:03PM
6  ├─● c8a2b91d  stripe-webhooks  13/07 10:22AM
5  │  ├─● 5e7f3a06  Add signature verification for Stripe webhoo…  14/07  1:47PM
4  │  └─● 1d4c9b82  Handle partial refunds and capture expiration  15/07  3:42PM
3  ├─● 7f2e8c15  paypal-webhooks  15/07  5:08PM
2  │  └─● b3a6d904  Wire up PayPal IPN handler  16/07 11:30AM
1  └─● e91f4a73  Add structured logging to webhook handlers  (most recent)

────────────────────────────────────────────────────────────────────────────────
1d4c9b82-3f6e-4a71-9c58-7b2e8f4a6d91
forked from c8a2b91d @ 4f8a2c17 · last active 7/15/2026, 3:42:18 PM

Handle partial refunds and capture expiration edge cases. When a Stripe
webhook reports a partial capture followed later by a refund, our local
order state machine currently double-fires the fulfillment webhook
because it doesn't check whether the capture was already reconciled. We
need to track capture state per payment intent and make the refund
handler idempotent so replayed webhook events don't trigger duplicate
…

↑/↓/hover: navigate   ⇧↑/↓: by recency   s: search   p/←: parent   →: child   Enter/click: resume   Esc: quit

Row 4 is the current selection — shown in reverse video in a real terminal (a plain-text block can't reproduce that), with the detail panel below always describing whichever row is selected. The tree lines (├─/└─/│) mirror the fork structure exactly as /branch created it, and each label follows Claude Code's own precedence for naming a session:

  • Explicit branch name — set with /branch <name> or /rename, shown bold. Rows 6 (stripe-webhooks) and 3 (paypal-webhooks). A name you set always wins over Claude's generated title, so a /rename shows up here immediately.
  • AI-generated title — Claude's own summary of the session (aiTitle), shown bold. Rows 8, 7, and 1.
  • First prompt — falls back to the branch's own first typed message when it has no title or name, shown as plain text. Rows 5, 4, and 2.

(If none of those exist, branch-graph falls back further to a stored slug, then the raw session id.) Labels are truncated to whatever room the row has left, as row 5 shows.

Trailing each label is when that branch was last active, in short DD/MM h:mmAM form, so you can see at a glance which branches are fresh and which are stale. On the one row that carries a marker, the marker takes that slot instead: row 1's (most recent) — the newest session in the project — or ← current when you launched branch-graph from inside a session of this project. A branch that lives in one of the project's worktrees is tagged ⎇ <worktree> ahead of that. The full timestamp for the selected row is in the detail panel below.

  • ↑/↓ or j/k, or mouse hover — move the selection.
  • Shift+↑/↓ or K/J — step by row number instead of tree position: up to the next older branch (higher number), down to the next newer one (lower number), wherever it sits in the tree. In a search they step through the matches only, skipping grey context rows.
  • p or ← — jump the selection to the branch this one was forked from (its parent). On a root branch it does nothing.
  • → — descend to this branch's most recent child. On a branch with no forks off it, it does nothing.
  • The detail panel shows the focused branch's session id, fork point, full last-active time, and more of its starting prompt.
  • s or / — search (see below).
  • Enter or left-click a branch — launches a Claude session on it (claude -r <sessionId>), handing the terminal over. This is the key advantage of the separate terminal: the picker switches you to the branch directly, no copy-paste.
  • Esc — quit.

Search

Press s (or /) and type. The tree filters as you type, and each match keeps its parent branches — greyed out — so you can still see where it hangs off:

Branches in my-project
search: paypal   2 of 8

7  ● 9b6e1f47  Design payment retry logic  12/07  4:03PM      ← grey (context only)
3  └─● 7f2e8c15  paypal-webhooks  15/07  5:08PM               ← selected
2     └─● b3a6d904  Wire up PayPal IPN handler  16/07 11:30AM

Only rows 3 and 2 match — the count says 2 of 8, not 3, because row 7 is kept purely as context: it's their parent, so the matches still have a place in the tree. Context rows are greyed out so the real hits stand out, and the connectors are redrawn for the smaller tree — row 3 becomes a └─ once its siblings are gone.

The selection starts on the first actual match (row 3), never on a grey context row, and stays put as you keep typing for as long as that branch still matches. Context rows are still fully selectable and resumable if you want one.

  • ↑/↓ and Shift+↑/↓ still move the selection while you type — every other printable key is text, so j, k, J, K, p and q type instead of navigating.
  • Backspace deletes, Ctrl-U clears the query.
  • Enter accepts the filter and drops you back into normal navigation over the matching rows. Resuming a branch then takes a second Enter, so the handoff to claude is always deliberate.
  • Esc unwinds one layer per press: cancel typing → clear the filter → quit. A search is never lost to a stray keypress.

Rows keep their original numbers while filtered, so a row still matches branch-graph <n>.

Matching runs entirely locally — no tokens, no network — over branch names, titles, first prompts, and session ids (not full transcript bodies). It is case-insensitive, and word order doesn't matter: auth flow finds "flow for authentication". Against names and titles you can also skip letters, so lcns finds "Choose license for distribution"; first prompts match on text you actually typed.

It auto-activates whenever it's attached to a real terminal. Force it with -i / --interactive, or disable it with --no-interactive. Piped output, --json, --list, a numeric index, or running inside Claude Code all fall back to plain output.

While the picker is open, mouse tracking captures clicks, so normal terminal text-selection is intercepted — hold Option (iTerm2) or Shift (most terminals) to select text instead.

Going up one level: ..

The most common move — back up one fork level from the branch you were just on — skips the picker entirely:

cbg ..          # or: cbg -p, cbg --parent

It reads as "up one level" exactly like cd .., and no shell mangles it. In a terminal it launches claude -r on the parent straight away, handing the terminal over, same as pressing Enter in the picker. Piped, or inside Claude Code, it prints the /resume <id> line instead — a nested claude would be the wrong thing there.

The branch it goes up from is the session you're inside when that session belongs to the project being shown, otherwise the most recently written one — the same anchor the ← current and (most recent) markers use. If that branch is a tree root, or its parent transcript isn't in this project, .. says so and exits non-zero rather than guessing.

Flags

Flag Description
.., -p, --parent Go up one fork level from the most recent branch, with no picker and no row number. Takes precedence over the picker.
-i, --interactive Force the interactive picker (requires a terminal).
--no-interactive Force plain list output.
--project <path> Inspect a different project: pass a working dir, or a ~/.claude/projects/<dir> path.
--json Machine-readable output (index, sessionId, parent, label, current, resume, and continues / worktree where they apply).
--list Force the list view.
--color / --no-color Force or disable ANSI color. Also honors FORCE_COLOR / NO_COLOR.
-h, --help Show help.

Running inside a Claude Code session (side feature)

You can also run branch-graph from within a Claude Code session via the ! prefix, which runs it locally. The process itself uses zero LLM tokens, but its output lands directly in the conversation, where Claude reads and interprets it, which does cost tokens:

!branch-graph        # list the fork tree, with a /resume line per branch
!branch-graph 2      # print only the /resume line for branch #2
!branch-graph ..     # print the /resume line for the parent of the most recent branch
!cbg                 # short alias, same behavior

It prints a ready /resume <id> for each branch; paste that to switch to the branch in place, with no separate claude process.

Limitations of this mode:

  • No interactive picker, no auto-switch. A command launched from the ! prefix is a child process and cannot drive the Claude Code TUI, so it only surfaces the /resume <id> line — you do the switch by pasting that built-in slash command yourself. For navigation and one-key switching, use the picker in a separate terminal.
  • Output is a static list: no detail panel, hover, or keyboard navigation.

Color still works here: it's detected via the CLAUDECODE environment variable (the ! prefix pipes stdout rather than using a TTY), so output stays colored. Plain pipes and file redirects stay color-free. Override with --color / --no-color or the FORCE_COLOR / NO_COLOR env vars.

How it works

  • Reads the session transcripts for the current project under ~/.claude/projects/<dir>/<sessionId>.jsonl.
  • Builds the tree from each forked session's forkedFrom: { sessionId, messageUuid }. /branch writes that; /fork and claude --resume <id> --fork-session don't, so for a session without it the parent is traced from content instead: a copy keeps its parent's message ids, and its parent is the oldest session that also holds the message where the copy diverged. "Oldest" is by transcript file creation time, so on a filesystem that keeps none, such copies are drawn as roots.
  • A copy of a /branch fork keeps that fork's forkedFrom tags, which then name the copy's grandparent. A /branch writes its first message after the tags as it creates the file, so when that message is older than the file, the tags came along in a copy and the session is placed by content too. Without that, /forks from a session made by /branch would show up as its siblings rather than its children.
  • Claude Code sometimes moves a running conversation to a new session id, leaving a continued-in record in the old transcript. That's still one conversation, so it's drawn as one branch under its newest id (the one /resume needs), with every fork taken under any of its ids as a child, and its earlier ids listed after it (continues 24bc9536 → f15b74a9). Search finds it by any of them. If the old id is later resumed and gets new messages, it's a branch of its own again.
  • When a session enters a git worktree, Claude Code moves its transcript to the worktree's own project dir, so a fork that went off into a worktree would vanish from this project's tree. branch-graph also reads the project dirs named after this one plus a dash, which is where worktrees inside or beside the project land (<project>/.claude/worktrees/<name>, <project>.worktrees/<name>), and draws a session from them when it started in this project or is related by fork to something here. Such rows are tagged ⎇ <worktree>. Resuming one works as for any branch: claude -r searches the project's worktrees for the id, and puts a session that is still in its worktree back there. A worktree kept anywhere else isn't found.
  • Marks the current session via the CLAUDE_CODE_SESSION_ID environment variable (← current (this session)). If that session isn't part of the project being shown (e.g. you used --project, or ran it from a plain terminal), it instead flags the most recently written session as (most recent) so there's always a useful anchor.
  • Takes each branch's last-active time from its transcript file's modification time — shown short (15/08 1:05PM) after the label in the picker, in full in the detail panel.
  • Numbers the rows by that same last-active time, newest first, so 1 is always the most recent branch. Ties fall back to the drawing order, so the numbering is stable across runs for a project you haven't touched.
  • Surfaces a ready /resume <id> for each branch.

Development

cargo test                   # scanning, tree building, search, and text layout
cargo clippy --all-targets   # clean
cargo fmt
cargo build --release        # refresh the binary the dev symlink points at

That last one is what makes a change show up in the installed branch-graph / cbg: install-dev.sh symlinks them into target/release, so a release build is the whole install step. After a cargo clean the link's target is gone, so rebuild or re-run the script.

The code is split so each piece reads on its own:

File What lives there
src/scan.rs Reading one transcript: fork point, active path, and how a branch gets its name
src/tree.rs Building the fork forest and the tree art
src/search.rs The offline matcher: substring, abbreviation, and the ancestor-keeping filter
src/render.rs Color, width and truncation, timestamps, the plain list view
src/tui.rs The picker: frame rendering, mouse, and the escape-sequence parser
src/term.rs termios, window size, poll/read, signals, local time
src/main.rs Arguments, project-dir resolution, the parallel scan, output modes

Two dependencies, both for the reasons above: serde_json to parse transcripts and libc for the terminal syscalls the picker needs.

Differences from the Node implementation

--help, --json, --list, <n> and .. were diffed byte for byte against the Node version — output and exit codes — across every project on this machine, the search filter was compared against the original's own functions over a 118-branch tree, and the picker's rendered frames were compared frame by frame. What follows is everything that is deliberately not identical:

  • Faster. Transcripts are scanned in parallel, and each JSONL line is parsed into a narrow struct so the large message and tool-result payloads are skipped instead of allocated. On a 284 MB project (118 transcripts): full scan ~117 ms versus ~1.4 s, and .. ~9 ms versus ~47 ms.
  • Timestamps are locale-pinned. The detail panel's full stamp is always M/D/YYYY, h:mm:ss AM/PM. Node used toLocaleString(), which moves with the machine's locale.
  • Resize is polled, not signalled. The picker samples the window size a few times a second rather than handling SIGWINCH, so a resize repaints within ~200 ms.
  • A damaged transcript degrades instead of failing. An unreadable or truncated session file becomes a branch labelled with its short id instead of taking down the run — which matters because every branch forked from it would vanish with it.
  • An out-of-range branch number echoes what you typed. branch-graph 99999999999999999999 reports that number back; Node printed its float-rounded form.

Limitations

  • Resumes whole sessions (/branch forks). There is no per-message resume in Claude Code.
  • Within-session rewind branches are intentionally excluded — Claude Code has no way to navigate to abandoned rewind branches (no such feature exists yet; checked 2026-06-27).
  • In the in-session (! prefix) mode, the tool surfaces the /resume <id> line but cannot perform the switch itself; the separate-terminal picker can.

Disclaimer

branch-graph is a third-party tool and is not affiliated with Anthropic.

About

Visualize the fork tree of your Claude Code sessions and jump to any branch — with zero LLM tokens. It's pure static local code; no model round-trip is involved.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages