Go .gitignore template
The Go template ignores compiled binaries by file extension, test executables and the vendor directory, and it has a real blind spot on platforms where binaries have no extension.
At a glance
- Category
- Language
- Patterns in this template
- 9 rules
- Directories ignored
- 1 of the 9 rules end in a slash, so they match directories only
- Also needs an OS template
- Yes. Go itself leaves very little in the tree, so most of your noise will be from the editor and the operating system.
- Typical companions
- Docker if you build container images, plus an editor and an OS template.
In detail
Go compiles to a single self contained binary, which is why this template is so short. There is no dependency directory to ignore by default and no intermediate object tree: the module cache lives outside the repository in the user's home directory, and the build cache lives there too.
The extension rules cover Windows and shared library output: exe, exe with a tilde from an interrupted write, dll, so and dylib. The blind spot is obvious once you see it. Running go build in a package on Linux or macOS produces a binary named after the directory with no extension at all, and none of these rules match it. A repository at github.com/you/myapp ends up offering to commit a fifteen megabyte file called myapp.
The fix is a rule for the binary you actually produce, or a convention of always building into a directory such as bin that you ignore wholesale. Naming the binary explicitly is the safer of the two, because it survives someone running a bare go build in the package directory out of habit.
*.test matches the compiled test binary that go test -c writes, and *.out covers coverage profiles and redirected output. go.work is the multi module workspace file, ignored here because it describes one developer's local arrangement of checkouts rather than anything the project asserts.
vendor is a judgement call rather than a rule. Vendoring copies your dependencies into the repository so builds need no network at all, which some organisations require and most projects do not want. If you vendor deliberately, delete this line, because otherwise the directory you are relying on is the one thing git will not carry.
Examples
*.exe
*.exe~
*.dll
*.so
*.dylib
*.test
*.out
go.work
vendor/9 lines exactly as the tool writes them, before any common additions you tick.
Things that catch people out
- None of the rules here match the extensionless binary that go build produces on Linux or macOS. Add a rule for the binary name, or build into an ignored directory.
- *.so also matches shared objects committed by other languages in a mixed repository, and *.out is broad enough to catch a data file named results.out.
- Ignoring go.work is right for a personal workspace, but a team that has agreed on a shared workspace layout should track go.work and ignore only go.work.sum.
Try it: .gitignore Generator
Pick one or more templates, tick the common additions you want, and copy or download the combined .gitignore.
.gitignore Generator
LOCAL · Browser-OnlyGenerate comprehensive .gitignore files by combining templates for languages, frameworks, IDEs, and operating systems
Language
Framework
IDE
OS
Build Tool
Infrastructure
Common Additions
Preview
Select templates or common additions to generate a .gitignore file.
Related .gitignore templates
All .gitignore templates →Docker .gitignore template
A small template with two rules worth questioning: it ignores .dockerignore, and it ignores every compose file except two, which suits few projects.
Rust .gitignore template
The Rust template ignores the target directory and leaves Cargo.lock tracked. Whether that is right depends on whether you ship a binary or a library.
C .gitignore template
What the C .gitignore template covers: object files, static and shared libraries, linker maps, debug symbols and the patterns that swallow a vendored binary.
C++ .gitignore template
The C++ template adds CMake output to the usual compiler artefacts, and its Makefile rule will ignore a hand written Makefile. Here is what each line covers.
C# .gitignore template
Why the C# template writes [Bb]in instead of bin, what the NuGet and Visual Studio rules cover, and which per developer files it keeps out of the repository.
Ruby .gitignore template
The Ruby template anchors most rules to the repository root with a leading slash, and ignores Gemfile.lock, which is right for a gem and wrong for an app.
Tools for this
- .gitignore Generator Generate .gitignore files for any project - 50+ templates for languages, frameworks, IDEs, and OS
- Dockerfile Generator Generate production-ready Dockerfiles for 18 stacks - multi-stage builds, best practices included