目标
把本地的 devops-platform 项目(Kubernetes 平台组件的安装脚本、Helm values、文档等)变成一个 Git 仓库,并推送到 GitHub 上已经创建好的空白仓库 lglgl/devops-platform。
前提
- 本地已经有项目文件夹:
/Users/igwanglyang/Documents/GitHub/devops-platform - GitHub 上已经创建了空白仓库(没有 README、没有 .gitignore、没有 LICENSE,这样首次推送不会冲突)
- 本地密钥/认证已经配置好,能直接向 GitHub 推送
过程概览
| 阶段 | 内容 | 结果 |
|---|---|---|
| 一 | 提交前先写好 .gitignore,排除密钥、本地环境、日志和构建产物 |
.gitignore 就绪 |
| 二 | git init -b main → git add . → git status → git commit |
本地仓库建立,首个提交 61af971 |
| 三 | 添加远程 origin,确认分支名,git push -u origin main |
推送成功,建立上游跟踪 |
| 四 | git status / git remote -v / git log -1 复核 |
工作区干净,本地与远程一致 |
验证结果
- 推送输出为
[new branch] main -> main,并提示branch 'main' set up to track 'origin/main'. git status显示Your branch is up to date with 'origin/main'.与nothing to commit, working tree cleangit log -1 --oneline显示61af971 (HEAD -> main, origin/main) Initial commit: devops platform,HEAD与origin/main指向同一个提交
环境信息(来自日志)
- 宿主机:macOS(提示符
(base) igwanglyang@MacbookProdahouzicn) - 项目目录:
/Users/igwanglyang/Documents/GitHub/devops-platform - 远程仓库:
https://github.com/lglgl/devops-platform.git - 首个提交:78 个文件,8843 行新增
一、先写 .gitignore
在第一次 git add 之前先创建 .gitignore,避免密钥、本地环境文件被提交进历史(一旦提交并推送,再清理会很麻烦)。
bash
(base) igwanglyang@MacbookProdahouzicn devops-platform % cat > .gitignore <<'EOF'
# Secrets and environment
.env
.env.*
!.env.example
*.pem
*.key
credentials
secrets/
kubeconfig
.kube/
# Local environment
.DS_Store
.idea/
.vscode/
.venv/
venv/
__pycache__/
*.py[cod]
# Logs and generated files
*.log
coverage/
dist/
build/
EOF
这份规则分三组:
- 密钥与环境配置 :
.env、.env.*(但用!.env.example保留示例文件)、*.pem、*.key、credentials、secrets/、kubeconfig、.kube/。对 DevOps 项目尤其重要,kubeconfig 和证书一旦泄露等于集群大门敞开。 - 本地环境 :
.DS_Store(macOS)、.idea/、.vscode/、虚拟环境.venv/venv/、Python 缓存。 - 日志与生成物 :
*.log、coverage/、dist/、build/。
二、初始化本地仓库并完成首次提交
用 git init -b main 直接指定默认分支名为 main(与 GitHub 现在的默认分支名一致),然后暂存、查看状态、提交。下面四条命令是一起粘贴执行的:
bash
(base) igwanglyang@MacbookProdahouzicn devops-platform % git init -b main
git add .
git status
git commit -m "Initial commit: devops platform"
Initialized empty Git repository in /Users/igwanglyang/Documents/GitHub/devops-platform/.git/
On branch main
No commits yet
Changes to be committed:
(use "git rm --cached <file>..." to unstage)
new file: .gitignore
new file: .workbuddy-ai/memory/2026-10-10.md
new file: .workbuddy-ai/memory/2026-10-11.md
new file: README.md
new file: application/README.md
new file: application/deployment.yaml
new file: application/ingress.yaml
new file: application/install.sh
new file: application/namespace.yaml
new file: application/service.yaml
new file: application/uninstall.sh
new file: cicd/README.md
...
new file: registry/README.md
new file: registry/deployment.yaml
new file: registry/ingress.yaml
new file: registry/install.sh
new file: registry/namespace.yaml
new file: registry/service.yaml
new file: registry/uninstall.sh
[main (root-commit) 61af971] Initial commit: devops platform
78 files changed, 8843 insertions(+)
create mode 100644 .gitignore
create mode 100644 .workbuddy-ai/memory/2026-10-10.md
create mode 100644 .workbuddy-ai/memory/2026-10-11.md
create mode 100644 README.md
create mode 100644 application/README.md
...
create mode 100644 registry/service.yaml
create mode 100644 registry/uninstall.sh
要点:
Initialized empty Git repository表示.git目录创建在项目目录内。No commits yet说明这是全新的仓库,git status里只有 78 个new file,没有无关的改动。[main (root-commit) 61af971]:root-commit表示这是仓库的第一个提交;78 files changed, 8843 insertions(+)是首次提交的规模。- 项目按组件分目录:
application/、cicd/(Argo CD、Jenkins)、gitlab/、ingress-nginx/、observability/(Loki、Alloy、kube-prometheus-stack、Tempo)、registry/,以及docs/文档。
三、关联 GitHub 远程仓库并推送
3.1 添加并确认远程
bash
(base) igwanglyang@MacbookProdahouzicn devops-platform % git remote add origin https://github.com/lglgl/devops-platform.git
(base) igwanglyang@MacbookProdahouzicn devops-platform % git remote -v
origin https://github.com/lglgl/devops-platform.git (fetch)
origin https://github.com/lglgl/devops-platform.git (push)
3.2 确认当前分支名
推送前确认本地分支确实是 main,避免本地叫 master、远程叫 main 造成混乱:
bash
(base) igwanglyang@MacbookProdahouzicn devops-platform % git branch --show-current
main
3.3 推送并建立上游跟踪
-u(--set-upstream)会让本地 main 记住它对应 origin/main,以后直接 git push / git pull 即可:
bash
(base) igwanglyang@MacbookProdahouzicn devops-platform % git push -u origin main
Enumerating objects: 99, done.
Counting objects: 100% (99/99), done.
Delta compression using up to 10 threads
Compressing objects: 100% (96/96), done.
Writing objects: 100% (99/99), 85.79 KiB | 12.26 MiB/s, done.
Total 99 (delta 8), reused 0 (delta 0), pack-reused 0 (from 0)
remote: Resolving deltas: 100% (8/8), done.
To https://github.com/lglgl/devops-platform.git
* [new branch] main -> main
branch 'main' set up to track 'origin/main'.
* [new branch] main -> main 说明远程原本没有 main,这次推送创建了它------这也印证了"远程是空白仓库"的前提;如果远程仓库初始化时带了 README,这里很可能会被拒绝(rejected / non-fast-forward)。
四、验证同步结果
bash
(base) igwanglyang@MacbookProdahouzicn devops-platform % git status
git remote -v
git log -1 --oneline
On branch main
Your branch is up to date with 'origin/main'.
nothing to commit, working tree clean
origin https://github.com/lglgl/devops-platform.git (fetch)
origin https://github.com/lglgl/devops-platform.git (push)
61af971 (HEAD -> main, origin/main) Initial commit: devops platform
三个信号都对:
Your branch is up to date with 'origin/main':本地与远程跟踪分支一致。nothing to commit, working tree clean:没有遗漏的改动。(HEAD -> main, origin/main):HEAD、本地main、远程origin/main全部指向61af971。
五、补充建议
以下均为补充内容,日志里没有执行,请按你的实际情况判断。
5.1 检查已推送的内容里有没有敏感信息
这个项目包含 Jenkins、GitLab、Argo CD、Registry 等组件的 values.yaml 和安装脚本。.gitignore 只挡得住文件名模式(.env、*.key、kubeconfig 等),挡不住写在 values.yaml 里的明文密码或 token。推送完成后建议扫一遍:
bash
git grep -nEi 'password|passwd|token|secret|apikey|api_key' -- '*.yaml' '*.sh' '*.md'
如果发现真实凭据,需要先轮换(作废并重新生成)凭据,再考虑清理历史,因为推送后它已经存在于 GitHub 上。
5.2 认证方式:前提是密钥,日志里用的是 HTTPS 地址
前提中说"已经通过了本地密钥配置",但 git remote add 用的是 HTTPS 地址。推送成功说明当前认证(例如 macOS 钥匙串里保存的凭据)可用。如果想真正走 SSH 密钥,可以把远程改成 SSH 地址:
bash
git remote set-url origin git@github.com:lglgl/devops-platform.git
ssh -T git@github.com # 验证 SSH 认证
git remote -v
5.3 .workbuddy-ai/memory/ 要不要提交
首次提交里带上了 .workbuddy-ai/memory/2026-10-10.md 和 2026-10-11.md,看起来是 AI 工具生成的本地记忆文件。如果它们属于个人工作痕迹、不希望公开,可以加入 .gitignore 并从版本控制中移除(保留本地文件):
bash
echo ".workbuddy-ai/" >> .gitignore
git rm -r --cached .workbuddy-ai
git add .gitignore
git commit -m "Ignore local AI memory files"
git push
总结
把"已有本地文件夹"同步到"空白 GitHub 仓库",完整命令如下(在项目目录内执行):
bash
# 1. 提交前先写好 .gitignore
# 2. 初始化仓库,默认分支 main,并完成首次提交
git init -b main
git add .
git status
git commit -m "Initial commit: devops platform"
# 3. 关联远程并推送
git remote add origin https://github.com/lglgl/devops-platform.git
git remote -v
git branch --show-current
git push -u origin main
# 4. 复核
git status
git remote -v
git log -1 --oneline