.gitignore 不忽略 tfvars 和 .terraform 目录

lea*_*ner 3 git terraform

看来我的 .gitignore 不符合预期。

$ git status
On branch master

No commits yet

Changes to be committed:
  (use "git rm --cached <file>..." to unstage)

        new file:   .gitignore
        new file:   win_2016_datacentre_vm/.terraform/plugins/linux_amd64/lock.json
        new file:   win_2016_datacentre_vm/.terraform/plugins/linux_amd64/terraform-provider-azurerm_v1.16.0_x4
        new file:   win_2016_datacentre_vm/aos-1.tfvars
        new file:   win_2016_datacentre_vm/aos-1.v1/.terraform/plugins/linux_amd64/lock.json
        new file:   win_2016_datacentre_vm/aos-1.v1/.terraform/plugins/linux_amd64/terraform-provider-azurerm_v1.16.0_x4
        new file:   win_2016_datacentre_vm/aos-1.v1/aos-1.plan
        new file:   win_2016_datacentre_vm/aos-1.v1/aos-1.tf
        new file:   win_2016_datacentre_vm/aos-1.v1/aos-1.tfvars
        new file:   win_2016_datacentre_vm/aos-1.v1/terraform.tfstate
        new file:   win_2016_datacentre_vm/aos-1.v1/terraform.tfstate.backup


Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git checkout -- <file>..." to discard changes in working directory)

        modified:   .gitignore

$ cat .gitignore
# Local terraform directories
docker/*
terraform-getting-started/*
terraform-getting-started.zip
**/.terraform/**
*/**/.tfvars
*.tfstate
Run Code Online (Sandbox Code Playgroud)

让我想知道的是,即使我添加了 .terraform、*.tfvars 和 *.tfstate,为什么要在 git status 中报告它们?

我还应该将 tfstate 提交到存储库吗?

我们将非常感谢您的帮助。

谢谢

tor*_*rek 6

为了稍微扩展zerkms 的评论,这一行:

\n\n
Changes to be committed:\n
Run Code Online (Sandbox Code Playgroud)\n\n

告诉您(和我们)您已经告诉 Git:请包含这些文件

\n\n

文件名.gitignore相当具有误导性。它看起来像是 Git 不应包含的文件列表或文件名模式,但事实并非如此:相反,它是 Git 不应自动添加的文件名列表如果它们还不存在(但它们已经存在),并且不应该建议添加.

\n\n

更长,但很高兴知道

\n\n

Git 的模型是,Git 从 Git 调用的内容(不同的索引、暂存区域缓存)构建新的提交,具体取决于 Git 的谁/哪个部分正在执行调用。据我所知,对索引的最好简短描述是,它是 Git 构建下一个提交(或者在本例中是第一个提交)的地方。

\n\n

当你第一次检查一些现有的提交\xe2\x80\x94时,你还不能\xe2\x80\x94,因为还没有,但在将来的某个时候,当你使用git checkout或为你git clone运行git checkout\xe2\x80\x94时,Git会填充在该提交索引中,然后使用从提交中提取的索引内容来填充工作树。现在索引副本和工作树副本都匹配,并且它们都与从提交中取出的文件匹配。

\n\n

当您的工作树充满文件并且更改了其中一些文件时,您可以运行将git add现有工作树文件重新复制回索引,以使它们为下一次提交做好准备。现在索引副本和工作树副本再次匹配,并且两者都与仍在提交中的文件不同。(或者,如果文件是全新的,那么它现在位于索引中,而以前不在索引中。)

\n\n

当您进行新的提交时,Git 会冻结索引中的副本,并将它们放入提交中。索引中的任何内容现在都在新提交中,即现在的当前提交。所以现在索引与提交匹配,就像您刚刚签出该提交一样。如果您的所有索引文件都与所有工作树文件匹配,则每个文件的所有三个副本都匹配,就像您第一次签出提交时一样。

\n\n

要从索引中删除文件,请使用:这将从索引工作树git rm中删除它。要仅将其从索引中删除,并将其保留在工作树中,请改为使用。请注意:如果该文件位于某些现有提交中,并且您将其从索引中删除,则它不会位于您所做的提交中,但它仍然是现有提交。检查其中之一将\xe2\x80\x94或至少可以\xe2\x80\x94覆盖你故意留下的工作树文件。git rm --cachedgit rm --cached

\n