目标
- 在本地初始化 Git 仓库
- 配置
.gitignore,忽略 Terraform 相关文件和密钥文件 - 提交必要的代码到 Git
- 连接 GitHub 远程仓库
- 推送代码到 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 files | key.pem 从未被 Git 跟踪,不会上传,已正确忽略 |
git push | 可能提示 error: failed to push some refs | 需要先 git remote add origin 再 git push -u origin main |
git status | 确保 terraform/ 和 key.pem 没有出现在 Untracked files: | .gitignore 已正确配置,确保它们不会被 Git 追踪 |
🚀 最终成果
- 本地
.terraform/目录和key.pem被忽略,未上传到 GitHub - Terraform 代码 (
main.tf,setup.sh,variables.tf) 已成功提交 - 代码已推送至 GitHub
- Git 状态已清理,无需额外提交