{"id":"26bb0ac5727e1db5","repo":"rust-lang/cargo","slug":"attempting-to-update-a-git-repository-but-offlin","errorCode":null,"errorMessage":"attempting to update a git repository, but {offline_flag} was specified","messagePattern":"attempting to update a git repository, but (.+?) was specified","errorType":"exception","errorClass":"anyhow::Error","httpStatus":null,"severity":"error","filePath":"src/sources/git/utils.rs","lineNumber":1024,"sourceCode":"/// * Dispatches `git fetch` using libgit2, gitoxide, or git CLI.\n///\n/// The `remote_url` argument is the git remote URL where we want to fetch from.\n///\n/// The `remote_kind` argument is a thing for [`-Zgitoxide`] shallow clones\n/// at this time. It could be extended when libgit2 supports shallow clones.\n///\n/// [`-Zgitoxide`]: https://doc.rust-lang.org/nightly/cargo/reference/unstable.html#gitoxide\n#[tracing::instrument(skip_all)]\npub fn fetch(\n    repo: &mut git2::Repository,\n    remote_url: &str,\n    manifest_reference: &GitReference,\n    locked_reference: &GitReference,\n    gctx: &GlobalContext,\n    remote_kind: RemoteKind,\n) -> CargoResult<()> {\n    if let Some(offline_flag) = gctx.offline_flag() {\n        anyhow::bail!(\n            \"attempting to update a git repository, but {offline_flag} \\\n             was specified\"\n        )\n    }\n\n    let shallow = remote_kind.to_shallow_setting(repo.is_shallow(), gctx);\n\n    // Flag to keep track if the rev is a full commit hash\n    let mut fast_path_rev: bool = false;\n\n    let oid_to_fetch = match github_fast_path(repo, remote_url, locked_reference, gctx) {\n        Ok(FastPathRev::UpToDate) => return Ok(()),\n        Ok(FastPathRev::NeedsFetch(rev)) => Some(rev),\n        Ok(FastPathRev::Indeterminate) => None,\n        Err(e) => {\n            debug!(\"failed to check github {:?}\", e);\n            None\n        }","sourceCodeStart":1006,"sourceCodeEnd":1042,"githubUrl":"https://github.com/rust-lang/cargo/blob/0e07a155371a6ce88ae53a2c00df940280c09a67/src/sources/git/utils.rs#L1006-L1042","documentation":"The low-level git fetch entry point `fetch()` (src/sources/git/utils.rs:1015) bails immediately when `GlobalContext::offline_flag()` returns `Some`, i.e. the user passed `--offline`/`--frozen` or set `net.offline`. Unlike error 160 (which fires at the source layer after local resolution fails), this one fires unconditionally at the start of *any* clone/update of a git repository — so it triggers whenever a git source actually needs to talk to the remote. The `{offline_flag}` token (e.g. `--offline`) is interpolated so the user knows which mechanism blocked the operation.","triggerScenarios":"Calling `cargo update -p <git-crate>` while `--offline`/`--frozen`; a git submodule update path that routes through `fetch()`; any operation that calls `GitDatabase::checkout`/`fetch` when the local clone is stale or missing AND network is disabled. The guard is the very first statement in `fetch()`, so it cannot be bypassed by the caller.","commonSituations":"Same family as 160 but hit through the update/fetch code path rather than the source-resolve path: CI with `--frozen`, laptops in airplane mode with `net.offline=true`, mirror setups where the git remote is unreachable.","solutions":["Drop the `--offline`/`--frozen` flag for this one command to allow the fetch.","Run `cargo fetch` on a networked host and copy `~/.cargo/git/` to the offline machine.","Vendor the git dependency and replace the source so `fetch()` is never called.","Set `CARGO_NET_OFFLINE=false` in the environment to override a config-level `net.offline=true`."],"exampleFix":"# before\ncargo update -p my-git-crate --offline   # -> attempting to update a git repository, but --offline was specified\n\n# after\ncargo update -p my-git-crate            # fetch the latest, then go offline","handlingStrategy":"validation","validationCode":"# Pre-flight: don't ask cargo to update git deps while offline.\nif [[ \"$CARGO_NET_OFFLINE\" == \"true\" || \" $CARGO_ARGS \" == *\" --offline\"* ]]; then\n  # populate cache first on a networked machine: cargo fetch\n  :\nfi","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Separate the 'update' step (online) from the 'build' step (offline).","Mirror `~/.cargo/git` to offline hosts instead of letting cargo fetch there.","Use vendored sources to eliminate git fetches entirely on offline hosts."],"tags":["cargo","git","offline","network"],"analyzedSha":"0e07a155371a6ce88ae53a2c00df940280c09a67","analyzedAt":"2026-08-06T01:46:58.334Z","schemaVersion":2}