看来我的 .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
让我感到奇怪的是,即使我添加了 .terraform、*.tfvars 和 *.tfstate 为什么在 git status 中报告它们?
我还应该将 tfstate 提交到存储库吗?
非常感谢您的帮助。
谢谢
最佳答案
展开zerkms's comment有点,这一行:
Changes to be committed:
告诉您(和我们)您已经告诉 Git:请包含这些文件。
文件名 .gitignore
具有误导性。它似乎是 Git 不应包含的文件列表或文件名模式,但它不是:相反,它是 Git 不应自动添加的文件名列表,如果它们还没有已经存在(但它们已经存在),并且不应该建议您应该添加。
更长,但很高兴知道
Git 的模型是 Git 从 Git 调用的东西构建新的提交,不同的是,索引,或暂存区,或缓存,取决于 Git 的谁/哪个部分在进行调用。我所知道的对索引的最好的简短描述是 Git 构建下一个要进行的提交(或者在这种情况下,是第一个要进行的提交)的地方。
当你第一次检查一些现有的提交时——你还不能,因为没有,但在未来的某个时候,当你使用 git checkout
或 git clone
为您运行 git checkout
——Git 将填充该提交的索引,然后使用从提交中提取的索引内容来填充工作树也是如此。现在索引副本和工作树副本都匹配,并且它们都匹配从提交中取出的文件。
当你有一个充满文件的工作树并且更改了其中的一些文件时,你运行 git add
将现有的工作树文件重新复制回索引,使它们准备就绪用于下一个 提交。现在索引副本和工作树副本再次匹配,并且都与仍在提交中的文件不同。 (或者,如果该文件是全新的,它现在在索引中,而以前没有。)
当您进行新 提交时,Git 会卡住索引中的副本,并将它们放入提交中。索引中的任何内容现在都在新提交中,现在是当前提交。所以现在索引与提交匹配,就好像您刚刚 checkout 该提交一样。如果所有索引文件都与所有工作树文件匹配,则每个文件的所有三个副本都匹配,就像您第一次 checkout 提交时一样。
要从索引中删除一个文件,使用git rm
:这将从索引和 工作树。要仅将其从索引中删除,将其保留在工作树中,请改用 git rm --cached
。注意:如果文件在一些现有的提交中,并且您将其从索引中删除,它不会在您所做的新提交中,但它仍然是现有的提交.检查其中一个将(或者至少可以)覆盖您使用 git rm --cached
故意留下的工作树文件。
关于git - .gitignore 不忽略 tfvars 和 .terraform 目录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53093911/