首页 / 资讯中心 / 文章详情

Windows 下使用 GitHub Private Repository 管理私人 Visual Studio/C++ 项目

Windows 下使用 GitHub Private Repository 管理私人 Visual Studio/C++ 项目 ★ FEATURED ARTICLE
如果我们有一个已经开发了一段时间的 Visual Studio/C 项目希望使用 Git 做版本管理同时又不希望源代码被其他人看到那么一个比较简单的方案就是Git GitHub Private Repository。本文以一个已经存在的 Windows C 项目为例从零开始介绍如何创建 GitHub 私有仓库、初始化本地 Git、配置 .gitignore、完成第一次 commit 和 push以及最后如何让 Codex 在完成代码修改后自动 commit 和 push。一、整体流程Windows 本地项目 │ ▼ git init │ ▼ 配置 .gitignore │ ▼ git add │ ▼ git commit │ ▼ 建立 GitHub Private Repository │ ▼ 配置 origin │ ▼ git push │ ▼ GitHub 私有云端代码仓库以后继续修改代码时只需要修改代码 → git status / git diff → git add → git commit → git push二、创建 GitHub Private Repository一进入创建仓库页面登录 GitHub 后点击右上角“”选择New repository二填写仓库信息选项建议设置Owner自己的 GitHub 账号Repository name项目名称例如 razehackDescription可选VisibilityPrivateAdd READMEOffAdd .gitignoreNo .gitignoreLicenseNo license最重要的是 Visibility 必须选择 Private。如果选择 Public项目源代码将公开。已有本地项目时建议创建空仓库最后点击Create repository。创建完仓库后如下图所示三、在 Windows 本地初始化 Git一PowerShell 和 CMD 选哪个Git 在两者中都能使用本文统一使用 PowerShell。二进入项目目录cd C:\你的项目路径\razehack git --version三初始化 Git 仓库git init git status项目目录中会出现隐藏的.git/用于保存版本历史、分支和对象等信息四、为 Visual Studio/C 项目配置 .gitignore一为什么不能直接 git add .Visual Studio/C 会产生编译产物、中间文件、缓存、运行日志和用户配置因此应先配置忽略规则。二创建 .gitignorenotepad .gitignore# Visual Studio .vs/ .vscode/ *.suo *.user *.userosscache *.sln.docstates # C/C build output Debug/ Release/ x64/ x86/ Win32/ [Bb]uild/ [Bb]in/ [Oo]bj/ [Ll]ib/ *.obj *.o *.pch *.pdb *.idb *.ilk *.ipch *.tlog *.lastbuildstate *.exe *.dll *.lib *.exp # Visual Studio generated ipch/ *.VC.db *.VC.VC.opendb *.opensdf *.sdf # Temporary/cache *.tmp *.temp *.log *.cache *.bak Thumbs.db Desktop.ini # Runtime logs log/ # Images *.png *.jpg *.jpeg *.gif *.bmp *.webp *.tif *.tiff # Videos *.mp4 *.avi *.mov *.mkv *.wmv *.webm *.flv # Project-required DirectX import library !RZ/SDK/DirectX/d3dx9.lib三项目必需的 .lib 文件怎么办*.lib会排除所有 .lib但!RZ/SDK/DirectX/d3dx9.lib可将项目必需的库重新纳入 Git。四编译输出目录直接排除如果RZ/DLL/全部是编译生成的 DLL/PDB可在 .gitignore 中加入RZ/DLL/。五、检查 .gitignore 是否生效一查看未跟踪文件git status --short二查看被忽略文件git status --ignored三检查某个文件为什么被忽略git check-ignore -v Pic/1.png git check-ignore -v Pic/1.mp4六、已经 git add 以后才修改 .gitignore 怎么办单纯修改 .gitignore 不会自动移除已进入暂存区的文件。可执行git rm -r --cached -f . git add .--cached 不会删除硬盘上的项目文件。它只是重新建立 Git 暂存区。一检查图片和视频git status --short | Select-String \.(png|jpg|jpeg|gif|bmp|webp|tif|tiff|mp4|avi|mov|mkv|wmv|webm|flv)$二确认必须保留的 d3dx9.libgit status --short | Select-String d3dx9.lib应该看到A RZ/SDK/DirectX/d3dx9.lib。七、配置 Git Commit 作者身份一第一次 commit 的 Author identity unknownGit 需要知道 commit 作者。user.name和user.email是提交作者身份不是 GitHub 登录密码。二推荐使用 GitHub noreply 邮箱在 GitHub Settings → Emails 中找到 noreply 邮箱然后git config --global user.name 你的GitHub用户名 git config --global user.email 你的GitHub noreply邮箱 git config --global user.name git config --global user.email八、完成第一次 Commitgit commit -m Initial commit git status此时版本已经保存在本地 Git 中但还没有上传 GitHub。九、将 master 修改为 maingit branch -M main git status十、把本地仓库连接到 GitHub一添加远程仓库git remote add origin https://github.com/用户名/项目名.git二检查 origingit remote -v十一、第一次 git pushgit push -u origin main第一次 HTTPS push 可能通过 Git Credential Manager 打开浏览器进行 GitHub 授权。成功后本地 main 会跟踪 origin/main。十二、解决 TLS certificate verification has been disabled 警告一检查配置来源git config --show-origin --get http.sslVerify二重新启用 TLS 验证git config --global http.sslVerify true git config --global --get http.sslVerify git fetch如果 fetch 正常且不再出现安全警告说明已经恢复。十三、以后日常修改代码怎么使用 Git一基本工作流git status git diff git add . git commit -m 描述本次修改 git push二git push 还需要每次输入密码吗通常不需要。第一次通过 Git Credential Manager/浏览器完成授权后凭据通常由 Windows 保存。凭据过期、被删除、授权撤销或更换电脑时可能需要重新认证。十四、如何理解 Git 的几个关键阶段① 工作区正在修改 │ git add ▼ ② 暂存区准备提交 │ git commit ▼ ③ 本地 Git Repository形成版本 │ git push ▼ ④ GitHub Remote Repository云端 Private 仓库命令作用git status查看当前变化git diff查看具体修改git add加入暂存区git commit在本地保存一个版本git push上传本地 commit 到 GitHubgit fetch获取远程提交信息但不直接合并十五、让 Codex 自动 Commit 和 Push一为什么使用 AGENTS.md可以在项目根目录 AGENTS.md 中加入项目级 Git 工作规则避免每次都重复提醒 Codex。二推荐的 Codex Git 工作流## Git workflow After completing any requested code change: 1. Review the changes with git diff and ensure that only files related to the current task are included. 2. Run the relevant build/tests when practical. 3. Check git status. 4. Stage the files related to the current task. 5. Create a Git commit with a concise message describing the change. 6. Push the commit to the current remote branch with git push. 7. Report the commit hash and whether the push succeeded. Unless I explicitly say otherwise, completing a coding task includes committing and pushing the completed changes. Do not: - commit unrelated changes made by me or another tool; - use git add . blindly when unrelated working-tree changes exist; - use git push --force or git push --force-with-lease; - rewrite, amend, rebase, or delete existing Git history unless I explicitly request it; - discard local changes to make the working tree clean; - commit credentials, API keys, passwords, logs, build artifacts, images, or videos that are excluded by .gitignore. If git push fails because of authentication, conflicts, branch protection, network errors, or other repository issues, stop and report the error instead of using a destructive workaround.三为什么不是每修改一个文件就 push更合理的粒度是“每完成一个完整开发任务再 commit push”而不是每保存一个文件都提交。四Codex 是否需要重新输入密码如果 Codex 在同一台 Windows 电脑、同一用户环境中调用本机 Git并能访问 Git Credential Manager 保存的凭据通常可以直接 push。独立云端或隔离环境则需要单独授权。十六、一些容易踩的坑一Private 不代表可以提交密码不要提交 .env、密码、API Key、Access Token、SSH 私钥、证书私钥和数据库密码。二不要盲目 git add .有自己和 Codex 并行修改时先用git status、git diff检查再加入当前任务相关文件。三不要让自动化工具随便 force push没有明确授权时不要使用git push --force或git push --force-with-lease。四.gitignore 不能删除已经提交的历史文件已经 commit 后再写进 .gitignore 不会让旧 commit 中的文件自动消失。十七、最终推荐的日常开发流程自己开发修改代码 ↓ git status git diff ↓ git add 相关文件 git commit -m 本次修改说明 git push使用 Codex给 Codex 一个完整开发任务 ↓ Codex 修改代码 ↓ 检查 git diff ↓ 编译 / 测试 ↓ 提交当前任务相关文件 ↓ git commit ↓ git push ↓ 报告 commit hash 和 push 结果十八、总结对于个人 Windows Visual Studio/C 私人项目一套实用方案就是Visual Studio / Codex │ ▼ 本地 Git Repository │ commit ▼ 本地版本历史 │ push ▼ GitHub Private Repository第一次配置完成以后日常核心操作就是git status、git add、git commit和git push。配合 AGENTS.md还可以让 Codex 在完成开发任务后自动检查、测试、commit 和 push。
阅读完成 · 觉得有帮助?
咨询建站