> ## Documentation Index
> Fetch the complete documentation index at: https://traceroot.ai/docs/llms.txt
> Use this file to discover all available pages before exploring further.

# Git Context

> Correlate every span back to its source file, line, and commit

traceroot automatically captures the git repository, commit, and the exact source file and line number for each instrumented function. This powers the **Show Code** feature in the trace viewer and enables the traceroot Agent to jump directly from a failing trace to the relevant code.

## What Gets Captured

| Attribute | Description |
| - | - |
| `traceroot.git.repo` | Repository in `owner/repo` format |
| `traceroot.git.ref` | Commit SHA, tag, or branch name |
| `traceroot.git.source_file` | File path of the decorated function |
| `traceroot.git.source_line` | Line number |
| `traceroot.git.source_function` | Function name |

`traceroot.git.repo` and `traceroot.git.ref` are set at the trace level. The `traceroot.git.source_*` attributes are set per span automatically by `@observe`.

## Configuration

traceroot resolves the repository and ref independently, filling each unset value in this order:

1. Explicit initialization arguments.
2. `TRACEROOT_GIT_REPO` / `TRACEROOT_GIT_REF` environment variables.
3. GitHub Actions variables (`GITHUB_REPOSITORY` / `GITHUB_SHA`).
4. Files in `<current working directory>/.git` (`config` and `HEAD`). This step does not need the `git` binary and does not search parent directories.
5. A `git` subprocess (`git remote get-url origin` and `git rev-parse HEAD`). This step needs the binary but works when the process starts in a repository subdirectory.

Automatic repository parsing recognizes only `github.com` remotes. For GitLab, Bitbucket, or self-hosted remotes, set `TRACEROOT_GIT_REPO` explicitly.

Because the repository and ref resolve independently, one may be present while the other is missing. In that case the SDK logs `git context incomplete`; set the missing value explicitly so source correlation has both fields.

Local `.git` auto-detection is a **development convenience only**. Production containers usually ship without a `.git` directory or the `git` binary, so auto-detection silently finds nothing and the AI agent cannot correlate traces to source. In production, always inject git context explicitly (see below).

Set them explicitly via environment variables:

```bash theme={null}
TRACEROOT_GIT_REPO=your-org/your-repo
TRACEROOT_GIT_REF=9c1e4f2
```

## Git context in production

Use **build-time injection**: inject the repo (static) and commit SHA (per-build) as environment variables at build time. The SDK reads them automatically — no code change required.

```dockerfile Dockerfile theme={null}
ARG GIT_SHA
ENV TRACEROOT_GIT_REPO=your-org/your-repo   # static
ENV TRACEROOT_GIT_REF=$GIT_SHA              # dynamic, per build
```

```yaml .github/workflows/deploy.yml theme={null}
- name: Build image
  run: docker build --build-arg GIT_SHA=${{ github.sha }} -t myapp:${{ github.sha }} .
```

In **GitHub Actions**, traceroot reads the standard `GITHUB_REPOSITORY` and `GITHUB_SHA` automatically — no Dockerfile change needed. On other CI/platform providers, use the build-time injection above.

Apply this to **every traced service**, including background workers and cron/queue consumers — not just your API server. Each process resolves git context independently.

For setups that reuse one image across commits, set `TRACEROOT_GIT_REPO` / `TRACEROOT_GIT_REF` in the Kubernetes pod spec or ECS task definition at deploy time instead.

## Why It Matters

Source code correlation is what enables the traceroot Agent to perform accurate root cause analysis — it can read the exact function that produced a failing span, not just the trace data. See [Root Cause Analysis](https://traceroot.ai/docs/ai-agent/root-cause-analysis.md).


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.