OpenTofu .gitignore: Which Files to Keep Out of Git

Every OpenTofu project creates files that should never go into Git: downloaded providers, state files and saved plans. This guide gives you the OpenTofu .gitignore to use, shows what each line keeps out, and names the one OpenTofu file you should commit.

It’s a companion to Part 2 of the OpenTofu Tutorial, which adds a first .gitignore to the Bookshop project.

Cost: free. Everything here runs on your own computer.

Every command and output on this page is real, run on OpenTofu 1.13 and Git on macOS. The commands are the same in Windows PowerShell and on Linux.

Prerequisites

  • OpenTofu installed: How to Install OpenTofu on macOS.
  • Git installed, from git-scm.com. git --version should print a version number.
  • An OpenTofu project folder where you’ve run tofu init and tofu apply, such as the Bookshop project from Part 2. This one also has a saved plan, bookshop.tfplan.

Step 1: See What Git Would Pick Up

If the folder isn’t a Git repository yet, run git init in it first. Then ask Git what it sees:

git status
On branch master

No commits yet

Untracked files:
  (use "git add <file>..." to include in what will be committed)
	.terraform.lock.hcl
	.terraform/
	bookshop.tfplan
	index.html
	main.tf
	terraform.tfstate

nothing added to commit but untracked files present (use "git add" to track)

Your branch may be called main instead of master; that depends on your Git settings. Three of these files must not be committed:

  • .terraform/ holds the providers tofu init downloaded. It’s large, built for your own operating system, and tofu init can always download it again.
  • terraform.tfstate is OpenTofu’s state. OpenTofu’s documentation warns that state can contain sensitive data, such as passwords.
  • bookshop.tfplan is a saved plan. The documentation warns that a plan file can hold sensitive values in plain text.

Step 2: Add a .gitignore

In your editor, create a file named .gitignore in the project folder with these four lines:

.terraform/
*.tfstate
*.tfstate.*
*.tfplan
LineKeeps out
.terraform/The downloaded providers and modules
*.tfstateterraform.tfstate
*.tfstate.*terraform.tfstate.backup, the copy of the previous state that OpenTofu keeps
*.tfplanSaved plans with a .tfplan ending

Step 3: Check Again

git status
A Terminal window running git status in an OpenTofu project with a .gitignore: only .gitignore, .terraform.lock.hcl, index.html and main.tf are listed as untracked, and the state, plan file and .terraform folder are left out.
With the .gitignore in place, Git sees only the files that belong in it.
On branch master

No commits yet

Untracked files:
  (use "git add <file>..." to include in what will be committed)
	.gitignore
	.terraform.lock.hcl
	index.html
	main.tf

nothing added to commit but untracked files present (use "git add" to track)

The providers, the state and the plan are gone from the list. What’s left is safe to commit: your configuration, the .gitignore itself, the lock file, and index.html, the page this project builds.

Keep the Lock File

Don’t ignore .terraform.lock.hcl. It records the exact provider versions, and both tofu init and OpenTofu’s documentation ask you to commit it, so everyone who uses the project gets the same versions.

Common Mistakes

MistakeWhat happensDo this instead
Writing .terraform* as a shortcutIt also matches .terraform.lock.hcl, so the lock file never reaches Git.Write .terraform/ with the slash, as above.
Adding the .gitignore after committing the stateGit keeps tracking the state file, because .gitignore doesn’t affect files Git already tracks.Stop tracking it with git rm --cached terraform.tfstate. The file stays on your disk.
Naming plan files without .tfplanA plan saved as tfplan isn’t matched by *.tfplan.End plan file names with .tfplan, or add their names to the .gitignore.

FAQ

I already pushed a state file. What now?

Stop tracking it with git rm --cached terraform.tfstate, add the .gitignore, and commit. Older commits still contain the file, so change any password or key that was in it.

Where does the state go if it isn’t in Git?

For a project on your own computer, it stays in the folder. Teams keep state in a shared, locked backend instead, which later parts of the OpenTofu Tutorial set up.

Conclusion

Four lines keep an OpenTofu project’s private files out of Git: .terraform/, *.tfstate, *.tfstate.* and *.tfplan. Commit your .tf files, the .gitignore and .terraform.lock.hcl, and check git status before your first commit.

Related

Additional Resources

OpenTofu® is a registered trademark of LF Projects, LLC. Other names mentioned are trademarks of their respective owners. JD Bots isn’t affiliated with or endorsed by the OpenTofu project.

Leave a Reply

Up ↑