Visual Studio 內建 Git 支持(VS 2013 Update 3+),無需安裝插件即可完成克隆、提交、推送、拉取、分支管理、合併請求等全流程操作。本文以 VS 2019/2022 為例,詳述從零開始將本地項目發布到 GitHub 的標準化流程。
| 操作 | VS 入口 | 快捷鍵 | 對應 Git 命令 |
| 查看變更 | Git Changes 窗口 | Ctrl+0, G | git status |
| 暫存/取消暫存 | 文件右鍵 / 拖拽到 Staged/Unstaged 區 | - | git add / git reset |
| 提交 | Git Changes → Commit All | Ctrl+Alt+F | git commit -m |
| 推送 | Git Changes → Push / 同步按鈕 | Ctrl+Shift+P | git push |
| 拉取 | Git Changes → Pull / 同步按鈕 | Ctrl+Shift+L | git 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 |
進階配置與最佳實踐
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 操作**。
- GitHub → Settings → Developer settings → Personal access tokens → Tokens (classic) → Generate new token
- 勾選
repo, workflow, admin:org 等所需權限
- 生成的 Token 僅顯示一次,立即複製保存
- VS 推送提示輸入密碼時,粘貼 Token(用戶名仍填 GitHub 用戶名)
- 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 遠端協作,能實現「編碼-提交-推送-審查-合併」無縫一體化體驗,大幅提升團隊協作效率與代碼質量。
- 上一篇文章: Visual Studio 2012/2013/2015/2017/2019/2022 中 Ctrl+C 複製空行行為解析與關閉方法
- 下一篇文章: PL/SQL Developer 導出數據庫對象完整指南:表、視圖、存儲過程、觸發器一鍵導出 SQL 腳本
Copyright ©
程式碼之家 All Rights Reserved