A Go .gitignore ignores built binaries (*.exe, *.dll, *.so, *.dylib), test binaries from go test -c (*.test), coverage and CPU profiles (*.out, *.prof), the vendor/ folder at the root, the workspace files go.work and go.work.sum, and .env. Those are the key lines of the tool’s Go template, which has 12.
What it ignores, and what it keeps
Commit go.mod and go.sum. The Go modules FAQ says go.sum should typically be committed along with go.mod: it holds the checksums that make every build fetch the same code. go.work and go.work.sum describe your local multi-module setup, so they stay out, and one developer’s workspace never changes everyone’s build.
In a scratch repository with this file, git check-ignore -v -n --no-index names the line that matched each path, or prints :: when none did:
:: go.mod
:: go.sum
.gitignore:12:/vendor/ vendor/modules.txt
:: web/static/vendor/htmx.min.js
:: myapp
.gitignore:2:*.exe myapp.exe
.gitignore:10:*.out coverage.out
.gitignore:8:*.test pkg/api/api.test
.gitignore:13:go.work go.work
.gitignore:16:.env .env
The binary go build writes
On Linux and macOS, go build in the module root writes an executable named after the last part of the module path, with no extension, so myapp above isn’t ignored; on Windows it’s myapp.exe, which *.exe catches. Add it by name, anchored, as /myapp, or build into a folder (go build -o bin/) and ignore /bin/.
vendor/, at the root only
Go only reads the vendor directory at the module root, so the template anchors it: web/static/vendor/ full of front-end scripts stays tracked. If you vendor on purpose, drop the line and commit the folder: when go.mod says go 1.14 or later and vendor/ exists, the go command builds from it by default, so an ignored local copy can drift from what CI builds.
Re-including a file
*.out also matches golden files such as testdata/expr.out. A later ! line brings them back, because testdata/ itself isn’t excluded:
.gitignore:19:!**/testdata/*.out parser/testdata/expr.out
Inside an excluded directory that doesn’t work: Git never looks into vendor/, so !vendor/modules.txt leaves the file reported against /vendor/ on line 12.
Already committed a binary?
.gitignore only affects untracked files. Untrack the file, keeping it on disk, and commit:
git rm --cached myapp
git commit -m "Stop tracking the binary"
Editor and OS files
GoLand’s .idea/, .vscode/ and .DS_Store come from your machine. Pick the JetBrains or VS Code template, or keep them in a global ignore file:
git config --global core.excludesFile ~/.gitignore_global