Fix it fast
Most likely: The failing environment lacks the git credential or authentication mechanism available on the working machine.
Confirm this is your error
Match the output and confirm you are troubleshooting the same failure.
error: failed to get `private-crate` as a dependency
Caused by:
failed to authenticate when downloading repository
network failure seems to have happened
if a proxy or similar is necessary `net.git-fetch-with-cli` may help here
failed to authenticate when downloading repository Check the cause
Run focused checks to identify what is failing in this environment.
git ls-remote <repository-url>
grep -nE "git-fetch-with-cli" .cargo/config.toml "${CARGO_HOME:-$HOME/.cargo}/config.toml" 2>/dev/null
git config --show-origin --get-regexp "^credential\." Apply the safe fix
Use the smallest safe change that addresses the confirmed cause.
# When CLI git already succeeds in the same environment
CARGO_NET_GIT_FETCH_WITH_CLI=true cargo fetch -vv Verify it works
Repeat the relevant checks and confirm the original error is gone.
git ls-remote <repository-url>
CARGO_NET_GIT_FETCH_WITH_CLI=true cargo fetch Avoid unsafe shortcuts
Keep security and data protections in place while troubleshooting.
- Do not disable SSH host-key or TLS verification.
- Do not embed tokens or private keys in
Cargo.toml, git URLs, or container layers.
What Access Failed
This is usually a target-and-identity problem, not a syntax problem. The request made it to the server, but the server did not like the credentials, permissions, or repository path attached to it.
Fix credentials and target access
Check the target and the credential together. Most of these fixes come down to using the right URL, account or token, and scope or repository permissions in the environment that actually failed.
Missing HTTPS auth:configure the approved git credential helper or job-scoped token for the target host.
Missing SSH auth:mount the correct read-only deploy key and known-host entry into the failing job without baking private keys into the image.
Built-in git mismatch:set net.git-fetch-with-cli = true or CARGO_NET_GIT_FETCH_WITH_CLI=true when the git CLI demonstrably authenticates correctly.
Wrong URL:use the canonical HTTPS or SSH repository URL supported by the host and your organization policy.
Insufficient access:grant the CI identity read access to the dependency repository instead of reusing a broad personal credential.
Test the same repository outside Cargo
Copy the exact git URL from the verbose Cargo output, redact sensitive path segments when sharing it, and run git ls-remote <url> in the same runner.
For HTTPS, inspect git config --show-origin --get-regexp "^credential\.|^http\..*proxy" without printing stored secret values.
For SSH, confirm SSH_AUTH_SOCK is available and inspect host-key trust with ssh -T git@<host> only when that host supports the test.
Inspect net.git-fetch-with-cli in the active Cargo config so you know whether Cargo is using built-in git or the CLI path.
Why It Happens
The CI runner has no SSH key, HTTPS token, or credential-helper access for the private repository.
The SSH agent is available on the developer host but not forwarded into the container or job.
The Git CLI supports an enterprise authentication helper that Cargo’s built-in Git support does not handle in the same way.
The repository URL, username form, or known-host entry differs from the working interactive configuration.
Prove the Failing Environment Can Reach It
Run git ls-remote <url> and then the original Cargo command from the same user, network namespace, and credential context.
Cargo should move past git authentication into checkout, dependency resolution, or compilation without exposing credentials in logs.
How Cargo authenticates git fetches
Cargo can fetch git sources through its built-in git library, while net.git-fetch-with-cli delegates the operation to the installed git command. SSH agent keys, known hosts, HTTPS credential helpers, URL forms, and CI secret mounts can differ between those paths, which explains why git may work interactively while Cargo fails in another environment.
Keep Credentials and Targets Aligned
To prevent this, standardize private Git authentication in base runner images and document whether Cargo uses built-in or CLI Git, use narrowly scoped deploy credentials with rotation and avoid personal developer tokens in shared CI, and add a redacted git ls-remote preflight for critical private git dependencies.
Docs and source code
Cargo Git Authentication
Official authentication guidance for private git dependencies and git-backed registry indexes. - Source
Cargo supports several forms of authentication when reading git dependencies and registries. Cargo Configuration
Official reference for registry, proxy, network, credential, and install settings. - Source
Cargo allows local configuration for a particular package as well as global configuration.