---
title: Code Intelligence
description: Explore indexed symbols, calls, imports, execution flows, and change impact in a Git repository.
---

# Code Intelligence

GitKB parses source files with tree-sitter and builds an index of symbols, calls, imports, and implementation relationships. Use it to locate definitions, trace callers and callees, and inspect the likely impact of a change.

## Start with a Git repository

Code intelligence works in a Git repository without `git-kb init`:

```bash
cd your-project
git-kb code index --json
git-kb code symbols validateToken --json
git-kb code callers validateToken --json
```

Without a knowledge base, the index lives in Git's private metadata. In an initialized KB, it lives in the KB's local indexes and can connect code symbols to documents. Ordinary CLI code queries read the index directly; they do not require a daemon or an embedding model.

See [Supported Languages](/docs/reference/supported-languages/) for file extensions and differences in extraction coverage.

## Indexing

```bash
# Index the current directory
git-kb code index --json

# Index a directory or a file
git-kb code index src/ --json
git-kb code index src/auth.ts --json

# Preview the files without storing an index
git-kb code index --dry-run --json

# Rebuild changed files and remove entries for files that no longer exist
git-kb code index --prune src/ --json
```

Indexing respects `.gitignore` and `.gitkbignore`. Use `.gitkbignore` to exclude generated code or fixtures that you still track in Git. Content hashes let subsequent runs skip unchanged files.

In a full KB, the [daemon](/docs/core-concepts/daemon/) watches registered source roots and schedules incremental indexing after changes. Without it, re-run `git-kb code index` to refresh the index.

## Definitions and call relationships

```bash
# List definitions in a file
git-kb code symbols --file src/auth.ts --json

# Find direct callers and callees
git-kb code callers validateToken --json
git-kb code callees login --json

# Follow additional levels of callers
git-kb code callers validateToken --depth 2 --json
```

A name can match several symbols. Copy the complete `symbol_id` from JSON output when you need one exact definition. Add `--strict` to callers or callees to reject an ambiguous name.

Call resolution uses names, imports, implementation relationships, and language-specific hints. Dynamic dispatch, macros, generated code, and unsupported syntax can leave calls unresolved. The graph describes indexed relationships; inspect the source and index health when a missing edge matters.

## Change impact and unused-code candidates

```bash
# Direct callers of symbols in this file
git-kb code impact src/auth.ts --json

# Expand the traversal
git-kb code impact src/auth.ts --depth 2 --json

# Find unused-code candidates in a file, with classification evidence
git-kb code dead --file src/auth.ts --explain --json
```

Impact analysis follows recorded call edges up to the requested depth and result limit. Dead-code analysis finds symbols without recorded callers and filters inferred entrypoints, public symbols, and tests by default. Use `--include-entrypoints`, `--include-public`, or `--include-tests` to inspect those groups. Treat candidates as a review list before deleting code.

## Entrypoints, flows, and graph queries

```bash
# Refresh inferred entrypoints and execution flows
git-kb code entrypoints --refresh --json
git-kb code flows --refresh --json
git-kb code flow <flow-id> --json

# Run typed query templates
git-kb code query hotspots --json
git-kb code query unresolved-by-reason --json
git-kb code query dead-code-explain --json
```

Entrypoints and flows are derived from the indexed graph. Query templates also cover public APIs, routes, cross-file coverage, and cross-service impact. Run `git-kb code query --help` for the available templates and options.

## Inspect source and structure

```bash
# Return files, symbols, and health as one payload
git-kb code map --path-prefix src/ --detail symbols --json

# Use an exact symbol ID from the symbols or map output
git-kb code show <symbol-id> --json
git-kb code shape <symbol-id> --json

# Return a caller/callee subgraph
git-kb code graph validateToken --depth 2 --json

# Inspect the parser's syntax tree
git-kb code dump-ast src/auth.ts --lines 10-25 --json
```

`show` returns metadata and source lines. `shape` returns a function's control structure and cyclomatic complexity. `graph` returns nodes and edges for a symbol neighborhood or a whole file.

## Branches and linked worktrees

Indexing detects the Git branch; `--branch` overrides it. Queries normally use the configured default branch. An explicit `--branch` applies overlay semantics: indexed feature-branch files override their default-branch counterparts, while other files use the default index.

```bash
git-kb code index --worktree /path/to/worktree --json
git-kb code callers validateToken --branch feature/auth --json

# Preview removal of indexes for branches that are no longer live
git-kb code prune --stale --dry-run --json
```

Configure per-repository default branches under `[code.repo_default_branch_map]` in `.kb/config.toml` when repositories do not use `main`. `code prune` removes branch indexes; `code index --prune` removes entries for deleted files.

## Link documents to code

In a full KB, use a code wikilink in a document:

```markdown
The auth refactor changes [[code:src/auth.ts::validateToken]].
```

GitKB extracts `references_code` relationships. Find the documents that reference a symbol with:

```bash
git-kb code docs src/auth.ts::validateToken --json
```

`git-kb code refs <name>` finds where a name is used in code instead.

## Check index health

```bash
git-kb code stats --json
git-kb code doctor --json
```

Use these when a file is missing, calls remain unresolved, or results look stale. If `edges_pending_files` is nonzero, some files still need their call edges rebuilt; zero callers for those files is incomplete evidence. Re-run indexing for the affected paths. `git-kb code index --force` rebuilds even files whose content hashes are unchanged.

## Next steps

- [Daemon](/docs/core-concepts/daemon/) — Source watching and background services
- [Supported Languages](/docs/reference/supported-languages/) — Extraction coverage
- [CLI Reference](/docs/cli-reference/) — Commands and flags
