Pallab
Pallab

Reputation: 2335

.Gitignore file for Terraform

I am trying to use Terraform and have a local Gitrepo connected to my remote Github account. When i run Terraform plan it creates a .terraform folder which has the providers installed and the size of the file is more than 100MB. So when i try to do Git Push origin master, the push is rejected by Github because of this .terraform folder size which is more than 100 MB So i created a .gitignore file in my local laptop from the template of Terraform .gitignore. So now in the .gitignore file i have my .terraform folder added. I have already did "git add .gitignore" and then tried to push my repo again to remote Github account it was still failing. So then i deleted the Github repository and created a new repository in Github and while creating i got the option to add a .gitignore file and i chose the Terraform .gitignore from the drop down . After i created the .gitignore file from the Github side, i was then able to push my local repo to Github this time by typing " Git push origin master"

So i would like to know why if you create a .gitignore file locally in your local laptop and add the path of the big folders and files that you want to exclude from the push and then try to do Git Push fails and it doesn't work? Why it works if you create from the Github side, any idea plz.. ?

Upvotes: 4

Views: 12100

Answers (2)

mkareshky
mkareshky

Reputation: 212

Although the response is belated, it might prove beneficial for those who have recently encountered this issue.

How to use the .gitignore file with Terraform

To use a .gitignore file with Terraform, simply create a new text file and name it .gitignore — place this file in the root directory of your project.

Step 1 – Go to bash terminal To do this on the command line, go to bash terminal and create a new file using touch .gitignore .

Step 2 – Run git init and terraform init You should also run the git init and terraform init commands to initialize your project.

Step 3 – Create the .gitignore file You can add the sections of code as shown below as needed to form your .gitignore file. Once the file is pushed to your repository, from that point on the files and paths listed in your .gitignore file will be ignored.

Step 4 – Ignore local Terraform directories and files Local Terraform directories and environment-specific files contain runtime information from execution done on the local machine and, therefore, do not need to be committed to source control.

# Local .terraform directories
 **/.terraform/*

#  Ignore variables files
 *.auto.tfvars

#  Ignore override files
 *.tfoverride

#  Ignore environment-specific files
 .envrc

#  Ignore CLI configuration files
 .terraformrc
 terraform.rc

Upvotes: 1

Blaz
Blaz

Reputation: 3618

From your description, it seems git had .terraform already tracked, hence the (seeming) difference of behaviour you observed the second time, with a fresh repository.

You should untrack .terraform with git rm --cached .terraform, using the --cached flag:

   --cached
       Use this option to unstage and remove paths only from the index. Working tree files, whether modified or not, will be left alone.

Upvotes: 3

Related Questions