← Debugging Guides

阅读中文版本

Cargo: not a git repository error

Fix Cargo git dependency errors when building in sandboxes, CI, or submodules where the source directory is not a git repository.

intermediatedebuggingrustcargoci

Symptoms

  • fatal: not a git repository (or any of the parent directories): .git
  • cargo build fails when fetching a git dependency
  • The error appears only in Docker or CI, not locally

Possible causes

  • The build directory was copied without its .git folder
  • A git dependency points at a path without a repository
  • The container lacks git so Cargo cannot fetch the dependency
  • A vendored or patched dependency lost its git metadata

Diagnosis steps

Confirm git availability and repository presence

git --version && ls -a .git

Interpretation

A missing git binary or .git directory explains failures that only occur in the build environment.

Identify which dependency triggers the fetch

cargo build -v 2>&1 | grep -i "git"

Interpretation

The verbose log names the git URL or path Cargo is trying to read.

Reproduce outside the container

Interpretation

If it works locally but not in CI, the difference is the copied tree or missing tooling.

Root cause

Cargo fetches git dependencies by invoking git against a URL or path. If git is unavailable or the referenced path is not a repository (for example a source tree copied without .git), that command exits with "not a git repository", aborting the build before compilation.

Fix

Install git in the build image, and preserve .git when copying source trees that are used as git dependencies. Prefer crates.io or a registry for reproducible builds, or vendor dependencies with cargo vendor. For submodules, ensure they are initialised and not shallow-trimmed.

Verify the fix

Re-run `cargo build` in the same environment; the git fetch succeeds and the build proceeds past dependency resolution to compilation.

Symptoms

cargo build aborts with fatal: not a git repository, typically only inside Docker or CI. Local builds on a full checkout work fine.

Diagnosis

Check for the git binary and repository metadata:

git --version
ls -a .git
cargo build -v 2>&1 | grep -i git

Fix

Install git and keep .git where needed:

RUN apt-get update && apt-get install -y git

Prefer registry dependencies, or vendor them:

cargo vendor

Then point the build at the vendored directory via configuration.

Verification

Re-run the build in the same container; dependency resolution completes and compilation starts, with no “not a git repository” message.

Making Builds Reproducible

Git dependencies trade reproducibility for freshness: they fetch whatever commit matches the revision, which makes builds depend on network access and on a working git binary. Pinning to a full commit hash, or better, publishing the crate to a private registry and depending on a version, removes git from the critical path entirely. When vendoring, commit the vendored directory and set the source replacement in .cargo/config.toml so CI never reaches the network. These choices also speed up cold builds in containers, where installing git and cloning over the network is often the slowest step.