🔄 完整复盘:Git 仓库初始化、Terraform 文件忽略及上传代码至 GitHub

72 阅读3分钟

目标

  1. 在本地初始化 Git 仓库
  2. 配置 .gitignore,忽略 Terraform 相关文件和密钥文件
  3. 提交必要的代码到 Git
  4. 连接 GitHub 远程仓库
  5. 推送代码到 GitHub

🛠️ 关键步骤及执行命令

🟢 1. 在本地初始化 Git 仓库

执行命令

bash

git init

目的

  • 在当前目录初始化一个新的 Git 仓库,使其能够进行版本控制。

🟢 2. 配置 .gitignore,忽略 Terraform 相关文件和密钥文件

执行命令

bash

echo ".terraform/" >> .gitignore
echo ".terraform.lock.hcl" >> .gitignore
echo "terraform.tfstate" >> .gitignore
echo "terraform.tfstate.backup" >> .gitignore
echo "terraform.tfvars" >> .gitignore
echo "key.pem" >> .gitignore  # 忽略私钥文件

目的

  • .gitignore 作用是告诉 Git 哪些文件或目录不需要被追踪,防止 Terraform 生成的状态文件(terraform.tfstate.terraform/ 目录)以及 密钥文件 key.pem 被上传到 GitHub。
  • 这样可以保护敏感数据,避免泄露。

验证

bash

cat .gitignore

确认 .gitignore 配置是否正确


🟢 3. 确保 Terraform 相关文件未被 Git 跟踪

执行命令

bash
复制代码
git rm -r --cached .terraform/   # 从 Git 缓存中移除 .terraform/ 目录
git rm --cached key.pem          # 从 Git 缓存中移除 key.pem

遇到问题

python
复制代码
fatal: pathspec '.terraform/' did not match any files
fatal: pathspec 'key.pem' did not match any files

问题原因

  • 这说明 .terraform/ 目录 和 key.pem 文件从未被 Git 追踪,所以 Git 不需要移除它们。

解决方案

  • 继续下一步即可,无需执行 git rm,因为 .gitignore 现在已经配置好,Git 以后不会追踪这些文件。

🟢 4. 添加需要跟踪的代码

执行命令

bash
复制代码
git add .gitignore main.tf setup.sh variables.tf

目的

  • .gitignore 和 Terraform 代码文件 (main.tf, setup.sh, variables.tf) 添加到 Git 版本控制

🟢 5. 提交代码

执行命令

bash
复制代码
git commit -m "Added Terraform setup files and updated .gitignore"

目的

  • git add 添加的文件提交到本地 Git 仓库。

结果

bash
复制代码
[master (root-commit) cc8740d] Added Terraform setup files and updated .gitignore
 4 files changed, 435 insertions(+)
 create mode 100644 .gitignore
 create mode 100644 main.tf
 create mode 100644 setup.sh
 create mode 100644 variables.tf

成功提交,Git 现在已经在本地跟踪这些文件!


🟢 6. 连接 GitHub 远程仓库

执行命令

bash
复制代码
git remote add origin https://github.com/babylink1/DevopsProject2.git
git branch -M main  # 如果你的默认分支是 main

目的

  • 绑定远程 GitHub 仓库,以便后续推送代码。

🟢 7. 推送代码到 GitHub

执行命令

bash
复制代码
git push -u origin main

目的

  • 将本地 Git 仓库中的代码推送到 GitHub 远程仓库。

成功推送的标志

bash
复制代码
Enumerating objects: X, done.
Counting objects: X, done.
Delta compression using up to X threads
Compressing objects: 100% (X/X), done.
Writing objects: 100% (X/X), X bytes | X MiB/s, done.
Total X (delta X), reused X (delta X), pack-reused X
To https://github.com/babylink1/DevopsProject2.git
 * [new branch]      main -> main
Branch 'main' set up to track remote branch 'main' from 'origin'.

代码成功上传到 GitHub!


📌 最终状态检查

执行命令

bash
复制代码
git status

正确的输出应该是

bash
复制代码
On branch main
Your branch is up to date with 'origin/main'.

nothing to commit, working tree clean

说明你的本地代码已经成功推送,并且 Terraform 相关文件不会被 Git 追踪!


📝 解决的问题

步骤发生的问题解决方案
git rm -r --cached .terraform/报错 fatal: pathspec '.terraform/' did not match any files说明 .terraform/ 目录从未被 Git 跟踪,不影响提交
git rm --cached key.pem报错 fatal: pathspec 'key.pem' did not match any fileskey.pem 从未被 Git 跟踪,不会上传,已正确忽略
git push可能提示 error: failed to push some refs需要先 git remote add origingit push -u origin main
git status确保 terraform/key.pem 没有出现在 Untracked files:.gitignore 已正确配置,确保它们不会被 Git 追踪

🚀 最终成果

  1. 本地 .terraform/ 目录和 key.pem 被忽略,未上传到 GitHub
  2. Terraform 代码 (main.tf, setup.sh, variables.tf) 已成功提交
  3. 代码已推送至 GitHub
  4. Git 状态已清理,无需额外提交