go: error obtaining VCS status: exit status 128
Go runs git to stamp version data and git exits 128. Dubious ownership, missing .git metadata and container builds are the usual triggers.

Introduction & Quick Summary
go: error obtaining VCS status: exit status 128
Use -buildvcs=false to disable VCS stamping.The Go toolchain runs git status --porcelain (or the equivalent for Mercurial, Bazaar, Fossil and Subversion) to stamp version information into the binary, and that command exited 128. The build then fails even though the code compiled, because stamping is treated as part of a reproducible build.
Exit 128 from git means "fatal", and the two usual reasons are:
detected dubious ownership in repository. The build runs as a different user than the one that owns the checkout, which is the default inside containers and on mounted volumes.- The directory is not a usable repository. No
gitbinary, a shallow or broken.git, a worktree whose metadata path is missing, or aGIT_DIRpointing elsewhere.
| Context | Typical trigger | Recommended fix |
|---|---|---|
| Docker build | COPY . . as root, build as non-root | -buildvcs=false in the image |
| Mounted volume | Host UID differs from container UID | safe.directory or disable stamping |
| CI checkout | Shallow clone without tags | Ensure .git is present, then stamp |
go install from a zip | No .git at all | Nothing to fix; it should not fail |
| Nested module | Git root far above the module | -buildvcs=false per module |
Core Mechanics, Signatures & Reproducible Setup
go build stamps vcs.revision, vcs.time and vcs.modified into the binary, which go version -m reads back. Stamping is enabled by default when the package being built is inside a repository and the toolchain can run the VCS command. -buildvcs=auto (the default) skips stamping when the repository is not found but fails the build when the VCS command errors, which is why a broken repository is worse than no repository.
Force the same failure in a scratch directory:
mkdir -p /tmp/vcs-demo && cd /tmp/vcs-demo
git init -q && printf 'module demo\n\ngo 1.22\n' > go.mod
printf 'package main\n\nfunc main() {}\n' > main.go
chown -R root:root /tmp/vcs-demo # as another user now
go build ./...
# go: error obtaining VCS status: exit status 128Confirm the underlying git failure, which the Go message hides:
git -C /tmp/vcs-demo status --porcelain; echo "exit=$?"
# fatal: detected dubious ownership in repository at '/tmp/vcs-demo'Three fixes, from narrowest to broadest:
# 1. Trust this specific directory for this user
git config --global --add safe.directory /workspace/app
# 2. Turn stamping off for the build (no version info in the binary)
go build -buildvcs=false -o /out/app ./cmd/app
# 3. Persist the choice for a container build
ENV GOFLAGS=-buildvcs=falseDisabling stamping is the right answer for container images, where the metadata describes the build machine rather than the artifact and is usually recorded separately in the image label anyway.
Key Comparison Table
| Cause | git status exit | Visible to user | Best fix |
|---|---|---|---|
| Dubious ownership | 128 | Yes, with a hint | safe.directory |
| Not a repository | 128 | not a git repository | Nothing to stamp |
| Git not installed | 127 | exec: "git": not found | Install git or disable |
Corrupt .git/HEAD | 128 | not a valid object name | Re-clone |
| Missing worktree metadata | 128 | not a working tree | git worktree repair |
Read-only .git | 128 | Permission denied | Mount writable or disable |
| Value | Behavior | Use case |
|---|---|---|
-buildvcs=true | Fail if VCS cannot be queried | Release pipelines that require provenance |
-buildvcs=auto | Default; skip only if no repo | General development |
-buildvcs=false | Never stamp | Container builds, mounted volumes |
GOFLAGS=-buildvcs=false | Same, project-wide | Repos that are always built in containers |
GOVCS | Restricts allowed VCS per module path | Module download policy, unrelated flag |
Practical Edge Cases, Error Fixes & Deep Dive Code Examples
Case 1: Multi-stage Docker builds
The usual sequence copies the source as root, then switches to a non-root user before go build:
FROM golang:1.22 AS build
WORKDIR /src
COPY go.mod go.sum ./
RUN go mod download
COPY . .
RUN CGO_ENABLED=0 go build -trimpath -buildvcs=false -o /out/app ./cmd/appAdding the source as a non-root owner works too, but -buildvcs=false also removes the need for .git inside the image, which is a smaller image and a smaller secret surface.
Case 2: safe.directory in a CI runner with a shared cache
git config --global --add safe.directory "$PWD"
go build ./...On a runner that builds many directories, add each one rather than *. safe.directory=* disables a real security check that protects against a repository owned by another user being treated as trusted.
Case 3: Worktrees and submodules
go build runs inside a subdirectory, so the toolchain walks up until it finds .git. In a worktree, .git is a file pointing at ../.git/worktrees/<name>; if that path was moved, git fails and Go reports exit 128 with no explanation.
git worktree list
git worktree repairCase 4: GIT_DIR inherited from the environment
A shell that exported GIT_DIR for another repository makes every git call in the session look at the wrong place:
env | grep -E '^GIT_(DIR|WORK_TREE|CEILING)'
unset GIT_DIR GIT_WORK_TREEThis is common in scripts that wrapped git --git-dir=... and left the variable behind.
Case 5: Vendored builds and nested modules
A nested module inside a larger repository stamps the parent's revision, which is usually wrong for the artifact. For a module published from a subdirectory, disable stamping or build from a clean checkout of that module only:
cd services/api && go build -buildvcs=false ./...Case 6: Verifying what the binary actually carries
go version -m ./app | grep -E 'vcs\.(revision|time|modified)'If these lines are absent, either stamping was disabled or the build did not happen in a repository. Recording the module version there instead is often more useful than a dirty-worktree flag that is true on every developer machine.
Common Compilation/Runtime Mistakes & Version Gotchas
-buildvcsdoes not affect tests.go testcompiles test binaries and can fail the same way, so putGOFLAGS=-buildvcs=falsein the environment rather than adding flags to one command.go install pkg@versionnever stamps. Building from the module cache has no repository, so this error cannot come from that path; if it appears, the module cache is inside a repository directory, whichGOMODCACHEshould fix.- Git versions before 2.35.2 lack
safe.directory. Very old images fail withdubious ownershipand no way to configure it, so either upgrade git or disable stamping. GOFLAGSfrom the environment overrides the project. A CI variable that sets-buildvcs=trueoverrides ago.envdefault and reintroduces the failure; printgo env GOFLAGSduring the build.- Exit code 128 is not specific. Any fatal git error maps to it, including a full disk while writing the index. Run the same
git statusby hand as the build user to see the real message. - Reproducibility tradeoff.
-trimpath -buildvcs=falseyields identical binaries across machines, which is usually what container builds want; stamping gives provenance at the cost of reproducibility.
When the difference between a working developer build and a failing container build is unclear, export the environment of each with the env file viewer and compare them, paying attention to GOFLAGS, GIT_* and HOME. If the build is driven by a Compose file, the Docker Compose viewer shows the user, volume and working directory that decide ownership. For comparing two Dockerfiles where one fails, the diff tool surfaces the USER or COPY change responsible.
Primary references: go build documentation describes -buildvcs, and git's safe.directory explains the ownership check that produces the underlying exit 128.
Official references
primary documentationThe Go documentation that defines the behaviour this guide explains.
Try matching browser tools
All toolsSmall utilities that fit Go work — they run entirely in this browser tab, so nothing you paste leaves your device.
Related Go guides
All Go guidesGo: x509 certificate signed by unknown authority
Go cannot build a chain to a trusted root: no CA bundle in the image, a private CA, or a missing intermediate. Install roots or set RootCAs.
Go: fatal error: concurrent map writes in goroutines
The runtime catches two writes to one map and stops the process. Guard access with a mutex, use sync.Map for caches, or give one owner.
Go: declared and not used compile error
Go rejects unused local variables and imports on purpose. Delete dead declarations, handle err values and keep imports honest instead of blanking them.
Go: missing go.sum entry for module providing package
Read-only module mode refuses to invent checksums. Run go mod tidy locally, download in CI and keep vendor trees and workspaces consistent.
More in Go
All Go guidesThe guides filed next to this one in Go, in library order.