# How to Use CodeGraph with Git Worktrees

You create a git worktree, start an agent and ask it to fix a bug or add a new feature (or you ask your agent to start a worktree, whatever). Before changing anything it searches for symbols, read files, runs a ton of greps and follows calls across the repo.

Start another agent in another worktree and it goes through the same exactly discovery process.

You're **spending tokens** on that exploration **in each session**.

[CodeGraph](https://github.com/colbymchenry/codegraph) builds a local graph of your code and exposes it to agents through MCP. An agent can retrieve relevant source together with its call paths and deps, reducing the searches and file reads needed to understand the change.

The benefit is **saving tokens** during code discovery (each time when needed; tool calls). With worktrees, getting that benefits requires a small setup step: each worktree needs its own index, and the agent needs to query the right one.

# Connect Your Agents Once

Install the CLI, then run its integration setup:

```shell
npm i -g @colbymchenry/codegraph
codegraph install
```

The installer setup detects [supported](https://github.com/colbymchenry/codegraph#supported-agents) agents including Claude Code, Codex, and Cursor, and configures their MCP integration automatically. You do this once, rather than for every worktree. Simple and easy.

# Initialize Each Worktree

Add CG initialization to your usual worktree setup:

```shell
git worktree add -b feature/invoice-retries ../billing-invoice-retries
cd ../billing-invoice-retries
codegraph init
```

`codegraph init` creates `.codegraph/` and builds an index of that worktree's files. During normal agent use, automatic syncing keeps it updates as you edit. The index stays local, with no separate database service or API key to manage.

Initializing the main checkout **doesn't initialize** your other worktrees. Their branches can contain different functions, signatures and call paths, so pointing every agent at the main checkout's index can give them the wrong code.

There is a particular trap with worktrees nested inside the main checkout: without a local index, CG can walk up the directory tree and find the parent's `.codegraph/`. It detects and warns about this mismatch on a best-effort basis. Run `codegraph status` from the wt to check for it.

# Tell the Agent Which Index to Use

Make the wt path explicit in your repo's agent instructions:

```plaintext
Use CodeGraph first for code discovery. Always pass projectPath as the
absolute root of the worktree you are editing. If it has no local index,
report that it needs codegraph init and use built-in tools until indexed.
```

The integration makes CG available; this rule directs the agent to use it against the workspace it is actually editing. Token savings depends on that usage. Querying the graph and then repeating the same discovery through file reads wastes the benefit. Also, fewer tokens processed per task doesn't necessarily mean less occupied context: graph responses will contain source code.

Put `codegraph init` into your worktree creation script or setup hook, keep the path rule in your agent instructions, and start each agent with an index of its own branch.

Let the tokens be with you!

# Sources

- CodeGraph repository and setup: <https://github.com/colbymchenry/codegraph>
- CodeGraph overview: <https://colbymchenry.github.io/codegraph/>
- Worktree index handling: <https://github.com/colbymchenry/codegraph/issues/155>
- Agent usage guidance: <https://github.com/colbymchenry/codegraph/blob/main/src/mcp/server-instructions.ts>
