DEV Community

1suleyman
1suleyman

Posted on

๐Ÿ’ป What Is .gitignore in Terraform? (And Why You Should Care Before You Push to Git)

Hey everyone ๐Ÿ‘‹

If you're working with Terraform and using Git to version your infrastructure code (which you absolutely should!), there's one simple file that can save you from major headaches:

โžก๏ธ .gitignore

When I first started with Terraform, I was focused on writing .tf files and deploying resources. But I quickly learned that if you donโ€™t handle .gitignore properly, you can accidentally leak sensitive info โ€” and clutter your repo with auto-generated noise.

Let me walk you through it the way I wish someone had done for me ๐Ÿ‘‡


๐Ÿงน Think of It Like a โ€œDo Not Packโ€ List

Youโ€™re moving houses. You label boxes you want to move โ€” clothes, books, tech.

But you donโ€™t pack everything. You leave out garbage, pizza boxes, and broken cables.

.gitignore is your "Do Not Pack" list for Git โ€” telling it what not to commit.


๐Ÿ’ฃ What Happens If You Donโ€™t Use It?

Terraform creates a lot of behind-the-scenes files โ€” like plugin folders, state files, and even crash logs.

If you push them to Git:

  • Your repo gets bloated ๐Ÿ’พ
  • Secrets might be exposed ๐Ÿ˜ฑ
  • Other devs will see things they shouldnโ€™t ๐Ÿ‘€

โš ๏ธ Common Terraform Files You Should Ignore

๐Ÿ“‚ File or Folder โš ๏ธ Why It Should Be Ignored
.terraform/ Contains downloaded provider binaries and plugin data โ€” recreated with terraform init
terraform.tfstate Stores your actual infrastructure state โ€” including cleartext secrets
terraform.tfstate.backup A backup copy of your state โ€” same risks as above
*.tfvars May include environment-specific variables like passwords, API keys, etc.
crash.log Only created on errors โ€” not useful for version control

โœ๏ธ Example .gitignore File for Terraform

# Ignore Terraform system files
.terraform/
*.tfstate
*.tfstate.backup
*.tfvars
crash.log
Enter fullscreen mode Exit fullscreen mode

๐Ÿ’ก Pro tip: You can grab a prebuilt Terraform .gitignore from GitHubโ€™s github/gitignore repo.


๐Ÿ”ฌ Quick Lab Recap: Setting It Up

In the demo I followed:

  1. Cloned a sample Terraform repo containing demo.tf
  2. Created an example.tfvars file with this content:
   username = "admin"
   password = "password"
Enter fullscreen mode Exit fullscreen mode
  1. Ran terraform init and terraform apply
  2. Noticed these files appeared:
  • .terraform/
  • terraform.tfstate
  • example.tfvars

Then created a .gitignore file with the entries listed above.

After that, running git status showed:
โœ… Git ignored all the sensitive/noisy files
โœ… Only .gitignore was being staged


๐Ÿง  Why This Matters (Especially for Teams)

Without .gitignore, you might:

  • Accidentally leak credentials in tfstate files
  • Share huge plugin binaries no one needs
  • Cause version conflicts from files that change every run

๐Ÿ’ก Analogy: Committing Terraform system files is like sending your dirty laundry to your boss instead of just the project folder.


โœ… Final Checklist

โœ… Good Practice โŒ Bad Practice
Use .gitignore to keep your repo clean Commit .tfstate or .tfvars files
Store secrets securely (not in Git) Hardcode passwords in .tf files
Push only the code โ€” not the mess Include local .terraform/ or crash logs

๐Ÿงฉ Final Thoughts

Using .gitignore isnโ€™t just about being tidy โ€” itโ€™s about:

  • Keeping your infrastructure secure
  • Making your repo easier to work with
  • Protecting your team from accidentally leaking secrets

Itโ€™s a small file with a big job. And once you understand it, it becomes second nature in every Terraform project.

If youโ€™re working with Terraform, Iโ€™d love to hear your .gitignore setup or lessons youโ€™ve learned. Drop a comment or connect with me on LinkedIn โ€” letโ€™s keep building clean, secure infrastructure together โ˜๏ธ๐Ÿง 

Top comments (0)