Terraform .gitignore template
The Terraform template ignores state files, variable files and crash logs because all three can contain plain text secrets, and also ignores the lock file, which should be committed.
At a glance
- Category
- Infrastructure
- Patterns in this template
- 14 rules
- Directories ignored
- None. Every rule matches by filename or extension rather than by directory.
- Also needs an OS template
- Yes. Nothing here covers the operating system or the editor.
- Typical companions
- An editor template and an OS template.
In detail
State files are the reason this template matters more than most. A state file records the real attributes of every resource the tool manages, and those attributes include values marked sensitive: database passwords, generated keys, connection strings. They are stored in plain text with no encryption whatsoever. A committed state file is a credential dump, not a configuration artefact.
The rules cover *.tfstate and *.tfstate.*, the second of which catches the numbered backups written before each apply. The right home for state is a remote backend with encryption and locking, which also solves the problem of two people applying at once. The ignore rule is the local fallback for the period before that backend is configured.
*.tfvars and *.tfvars.json are the variable files, and they are ignored for the same reason: they are where people put the secrets that the configuration reads. That creates a real tension, because a project usually wants a shared, non secret variable file for each environment. The usual resolution is to name those files something that does not end in .tfvars, or to add an explicit negation for the ones you track.
**/.terraform/* is the plugin and module directory, holding downloaded providers, which are large platform specific binaries, and cached copies of remote modules. It is regenerated by an init command and there is no reason to carry it. Provider binaries are tens of megabytes each and are downloaded for the platform running the command, so a copy from a colleague's machine is often the wrong architecture entirely.
The override rules are the interesting design detail. A file named override.tf or ending in _override.tf is merged over the main configuration automatically, which makes it the mechanism for a local change you do not want to share. Ignoring them is what turns that mechanism into a safe one.
Examples
**/.terraform/*
*.tfstate
*.tfstate.*
crash.log
crash.*.log
*.tfvars
*.tfvars.json
override.tf
override.tf.json
*_override.tf
*_override.tf.json
.terraformrc
terraform.rc
.terraform.lock.hcl14 lines exactly as the tool writes them, before any common additions you tick.
Things that catch people out
- .terraform.lock.hcl is ignored by this template, which contradicts the recommended practice. The lock file pins provider versions and their checksums and should be committed so every run uses the same providers.
- A committed state file has already leaked whatever secrets it held. Removing it from the index is not enough: rotate the credentials, because the file is in every clone's history.
- *.tfvars catches the shared environment variable files you meant to track as well as the secret ones. Add a negation for the specific files that belong in the repository.
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.
Vagrant .gitignore template
Two rules worth understanding: the .vagrant directory holds machine identifiers and a generated private key, and box files are multi gigabyte disk images.
Go .gitignore template
The Go template ignores platform binaries by extension, which misses the extensionless binary go build produces on Linux and macOS. Here is what to add.
JavaScript .gitignore template
Every rule in the JavaScript .gitignore template explained: why node_modules is restored rather than committed, and what the old package folders catch.
TypeScript .gitignore template
The TypeScript template ignores compiled output, source maps and build info, but its *.d.ts rule is broader than most projects want. Here is each line.
Python .gitignore template
A line by line read of the Python .gitignore template: bytecode caches, virtual environments, packaging output, test caches and the lib/ rule that bites.
Tools for this
- .gitignore Generator Generate .gitignore files for any project - 50+ templates for languages, frameworks, IDEs, and OS
- .env Editor & Validator Edit and validate .env files online - detect duplicates, mask secrets, generate .env.example