Git 是分散式版本控制系統。GitHub 只是遠端托管服務。上傳代碼需兩階段:
本地工作區 → (git add) → 暫存區 → (git commit) → 本地倉庫 → (git push) → GitHub 遠端倉庫
git init(新專案)或 git clone https://github.com/username/repo.git(既有專案)。
git config --global user.name "Your Name"git config --global user.email "you@example.com"
git status 查看變更。
git add .(全部)或 git add file1.cs file2.h(指定)。
git commit -m "feat: 實作使用者登入模組"(訊息建議遵循 Conventional Commits)。
git remote add origin https://github.com/username/repo.git
git push -u origin main(首次加 -u 設定上游,後續僅 git push)。
| 現象 | 原因 | 解法 |
|---|---|---|
| push 被拒絕 | 遠端有他人新提交 | git pull --rebase 後再 push |
| 誤提交敏感資訊 | 未加 .gitignore | git filter-repo 重寫歷史並強制推送(謹慎) |
| 合併衝突 | 同檔案雙邊修改 | 手動編輯衝突標記區塊,git add 後 git commit |
專案根目錄建立 .gitignore,參考 github/gitignore 專案對應語言模板(如 VisualStudio.gitignore、Node.gitignore),避免上傳 bin/obj/.vs/ 等建構產物。