首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> 更多程式語言 >> 更多程式設計知識

Visual Studio 2013/2015/2017/2019/2022 整合 GitHub 完整教程:從項目創建到發布推送

     編輯:更多程式設計知識

Visual Studio 內建 Git 支持(VS 2013 Update 3+),無需安裝插件即可完成克隆、提交、推送、拉取、分支管理、合併請求等全流程操作。本文以 VS 2019/2022 為例,詳述從零開始將本地項目發布到 GitHub 的標準化流程。

前置準備

  • GitHub 賬號:已註冊並驗證郵箱
  • Git 安裝:VS 安裝程序勾選「Git for Windows」組件,或獨立安裝 Git
  • SSH Key(推薦):ssh-keygen -t ed25519 -C "email@example.com" → 公鑰添加到 GitHub Settings → SSH Keys。支持免密推拉、更安全。

流程一:本地現有項目發布到新 GitHub 倉庫

  1. 打開項目:雙擊 .sln 或 File → Open → Project/Solution
  2. 初始化 Git 倉庫:右鍵解決方案資源管理器根節點 → Add to Source Control → Git
    或菜單 Git → Create Git Repository → 選擇本地路徑 → Create
  3. 首次提交:Git Changes 窗口(Ctrl+0, G) → 輸入提交信息「Initial commit」 → Commit All (或 Commit Staged)
  4. 創建 GitHub 倉庫:瀏覽器打開 github.com/new → Repository name: MyProject → Public/Private → 不勾選 README/.gitignore/License(本地已有)→ Create repository
  5. 關聯遠端並推送:複製倉庫 URL(HTTPS: https://github.com/username/MyProject.git 或 SSH: git@github.com:username/MyProject.git)
    在 VS Git Changes 窗口 → Push 鏈接 → 粘貼 URL → Push → 輸入 GitHub 用戶名/密碼(或 PAT/SSH 密鑰) → 完成

流程二:克隆現有 GitHub 倉庫到本地

  1. VS 歡迎頁面 → Clone a repository
  2. Repository location:粘貼 GitHub URL → 選擇本地路徑 → Clone
  3. 若為私有倉庫,輸入憑據(用戶名 + Personal Access Token,不再支持密碼)
  4. 克隆完成自動打開解決方案

日常 Git 操作圖解

進階配置與最佳實踐

1. .gitignore 模板

VS 自帶 VisualStudio.gitignore(創建倉庫時可選),或從 GitHub 官方模板 複製。核心忽略:


# Build results
[Dd]ebug/
[Rr]elease/
x64/
build/
bin/
obj/

# User-specific files
.vs/
*.suo
*.user
*.userosscache
*.sln.docstates

# NuGet
packages/
*.nupkg

# VS Code / Rider / Resharper
.idea/
.vscode/
_ReSharper*/

2. 提交信息規範(Conventional Commits)


feat: 添加用戶導出 Excel 功能
fix: 修復登錄頁驗證碼不刷新問題
docs: 更新 API 文檔 v2.1
refactor: 重構訂單服務領域模型
test: 增加支付模塊單元測試
chore: 升級 Newtonsoft.Json 至 13.0.2

配合 Git Hooks (Husky/.git/hooks/commit-msg) 強制檢查格式。

3. Personal Access Token (PAT) 替代密碼

GitHub 2021 年 8 月起**停止支持賬號密碼認證 HTTPS 操作**。

  1. GitHub → Settings → Developer settings → Personal access tokens → Tokens (classic) → Generate new token
  2. 勾選 repo, workflow, admin:org 等所需權限
  3. 生成的 Token 僅顯示一次,立即複製保存
  4. VS 推送提示輸入密碼時,粘貼 Token(用戶名仍填 GitHub 用戶名)
  5. Windows 憑證管理器會緩存,後續無需重複輸入

4. SSH 免密配置(更安全)


# 1. 生成密鑰對
ssh-keygen -t ed25519 -C "your_email@example.com"
# 2. 啟動 ssh-agent 並添加私鑰
eval "$(ssh-agent -s)"
ssh-add ~/.ssh/id_ed25519
# 3. 複製公鑰內容
cat ~/.ssh/id_ed25519.pub
# 4. GitHub → Settings → SSH Keys → New SSH Key → 粘貼
# 5. VS 克隆/推送使用 SSH URL: git@github.com:user/repo.git

常見問題排查

推送失敗:remote: Permission denied
1. 確認 Token/PAT 權限包含 repo 2. 確認 SSH 公鑰已添加到 GitHub 3. 私有倉庫需 Collaborator/Write 權限
推送失敗:rejected - non-fast-forward
遠端有新提交,先 Pull → 解決衝突 → Commit → Push,或 Force Push (僅限個人分支)
雙因子認證 (2FA) 啟用後無法推送
必須使用 PAT 或 SSH,不再支持密碼
大文件推送失敗(>100MB)
使用 Git LFS:git lfs track "*.psd" → 提交 .gitattributes → 推送
行尾符號警告 (LF/CRLF)
配置 git config --global core.autocrlf true (Windows) / input (Mac/Linux),或項目根目錄 .gitattributes 強制 * text=auto

VS 2022 新增 Git 體驗增強

  • Git Repository 窗口:獨立窗口管理多倉庫、分支圖、提交詳情、遠端比較
  • 行級提交歷史:編輯器右鍵 → View Git Blame (Annotate) / View Commit History
  • 合併請求 (PR) 創建/審查:Git Repository → Pull Requests → New Pull Request,直接在 VS 內完成 Code Review
  • GitHub Issues 集成:提交信息引用 #123 自動鏈接 Issue,Git Changes → Work Items 關聯 Azure Boards/GitHub Projects

掌握 VS 內建 Git 工作流,配合 GitHub 遠端協作,能實現「編碼-提交-推送-審查-合併」無縫一體化體驗,大幅提升團隊協作效率與代碼質量。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved
操作VS 入口快捷鍵對應 Git 命令
查看變更Git Changes 窗口Ctrl+0, Ggit status
暫存/取消暫存文件右鍵 / 拖拽到 Staged/Unstaged 區-git add / git reset
提交Git Changes → Commit AllCtrl+Alt+Fgit commit -m
推送Git Changes → Push / 同步按鈕Ctrl+Shift+Pgit push
拉取Git Changes → Pull / 同步按鈕Ctrl+Shift+Lgit pull
同步Git Changes → Fetch → Pull → Push-git fetch && git pull && git push
創建分支Git Changes → 分支下拉 → New Branch-git checkout -b
切換分支狀態欄分支名 / Git Changes → 分支列表-git checkout
合併分支Git Repository 窗口 → 分支右鍵 → Merge-git merge
查看歷史Git Repository → 右鍵分支 → View History-git log --oneline --graph
解決衝突Git Changes → Conflicts → 雙擊文件 → 合併編輯器-git mergetool