Skip to main content

How to Use CodeGraph with Git Worktrees

Save tokens on code discovery by giving your agent the right CodeGraph index in every worktree.

Updated 3 min readMarkdown
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 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:

npm i -g @colbymchenry/codegraph
codegraph install

The installer setup detects supported 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:

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:

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

About the author

  • Software Engineer x Data Engineer - I make the world a better place to live with software that enables data-driven decision-making

No comments yet. Be the first to comment.