go build fails with error obtaining VCS status, exit status 128
Fix it fast
Most common cause, 1 of 2go build runs as root or a CI account that does not own the checkout
Build as the user who owns the checkout.
# build without sudo
go build -o app .
# or, inside the container or CI job that builds as another user
git config --global --add safe.directory /src
go build -o app .Short answer
error obtaining VCS status: exit status 128 means git status, which go build runs to stamp the binary, failed. Usually another user owns the repository (sudo, a container, CI), or .git points to a missing gitdir. go build -v shows git's real message; fix that, or pass -buildvcs=false. Applies to Go 1.26-1.27.
Which cause do you have?
Go hides git's own message unless you ask for verbose output. Build again with -v in the same shell, user and container, and read the line that starts with fatal.
go build -v ./... 2>&1 | grep -iE "^# cd|fatal"Then find the line you see in this list and open its fix:
- 1go build runs as root or a CI account that does not own the checkout
The fatal line says the repository is owned by someone else, and the build runs under sudo, as root in a container, or as a CI user - 2A .git file points to a gitdir that is missing from this copy
Ownership is fine, .git is a small file that starts with gitdir:, and the path after it does not exist where the build runs
What does the error look like?
error obtaining VCS status: exit status 128Use -buildvcs=false to disable VCS stamping.
Cause 1 of 2
go build runs as root or a CI account that does not own the checkout
You seeThe fatal line says the repository is owned by someone else, and the build runs under sudo, as root in a container, or as a CI user
Check
go build reports exit status 128 here because Git refuses to read a repository owned by someone else, and Go treats that refusal as a failed VCS lookup. The Go issue tracker has the classic case: a plain build succeeds while sudo go build in the same folder fails. A container that bind-mounts your checkout and builds as root hits the same check.
ls -ld .git
id -unFix
Build as the user who owns the checkout. When that is not possible, trust the exact path in the global or system Git config of the user that builds; Git ignores the setting inside the repository's own config.
# build without sudo
go build -o app .
# or, inside the container or CI job that builds as another user
git config --global --add safe.directory /src
go build -o app .Verify
git status works for the building user, and the binary carries vcs.revision in its build info.
go version -m ./app | grep vcsConfirmed by github.com
Cause 2 of 2
A .git file points to a gitdir that is missing from this copy
You seeOwnership is fine, .git is a small file that starts with gitdir:, and the path after it does not exist where the build runs
Check
Submodules and git worktree checkouts usually hold a gitfile: a plain .git text file naming the real repository directory elsewhere. Copy such a folder into a Docker build context or another machine without its parent repository and git cannot open it, so go build fails the stamp. An invalid .git in a parent directory has the same effect, as reported in the Go tracker for toolchain builds.
cat .git
git rev-parse --git-dirFix
Build from a full clone, or leave .git out of the copied tree. With no repository found, Go's default auto mode skips stamping instead of failing. Use -buildvcs=false when you want a build that never depends on Git state.
# .dockerignore
.git
# or keep the files and opt out of stamping
go build -buildvcs=false -o app .
export GOFLAGS=-buildvcs=false # for every go command in this jobVerify
The build completes; go version -m shows no vcs lines when stamping was skipped or disabled.
go version -m ./appConfirmed by github.com
How do I stop it coming back?
- Run go build in CI and containers as the same user that checked out the code, or add one safe.directory line for the checkout path in the image.
- Decide whether release binaries need vcs.revision. If they do, build from a full clone; if not, set -buildvcs=false in the Makefile so the choice is explicit.
- Keep .git out of Docker build contexts that copy only part of a repository, such as a single submodule.
Sources
How was this page made?
I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.