Git vs GitHub、平台功能、社群價值、帳號設定
GitHub 是一個程式碼託管與協作平台(code hosting & collaboration platform)——它託管你用 Git 管理的專案,並在之上提供協作、追蹤、自動化與部署服務。2008 年上線,如今超過一億開發者在上面工作。
| 對比 | Git | GitHub |
|---|---|---|
| 本質 | 版本控制工具 | 雲端託管平台 |
| 運行位置 | 你的電腦(本機) | 雲端伺服器 |
| 發明 | Linus Torvalds(2005) | GitHub, Inc.(2008) |
| 少了它會怎樣 | GitHub 無法運作 | Git 照樣能用(只是單機) |
很多人把兩者混為一談,其實它們是分層合作的:
git init git add . git commit -m "first"
git push -u origin main # 之後:Issues、PR、 # Pages、Actions…
GitHub 不是「一個放程式碼的地方」,而是一整套開發工作流。五大核心服務:
| 功能 | 用途 | 單元 |
|---|---|---|
| Repo 託管 | 程式碼雲端備份、版本歷史、分支 | 單元 2 |
| Pull Request | 程式碼審查與合併流程 | 單元 4 |
| Issues / Projects | bug 追蹤、任務管理、看板 | 單元 5 |
| GitHub Pages | 靜態網站免費部署 | 單元 6 |
| GitHub Actions | CI/CD 自動化 | 單元 7 |
註冊後,你的 repo 網址形如 https://github.com/你的帳號/repo名。連上 GitHub 有三種認證方式:
| 方式 | 原理 | 適合 |
|---|---|---|
| HTTPS + PAT | Personal Access Token 當密碼 | 最常見、容易設定 |
| SSH key | 一對公私鑰,免密碼 | 長期使用、最安全 |
| gh CLI | 命令列登入,同時管理認證 | 重度命令列使用者 |
git filter-repo 清理(見單元 8)。1. 到 github.com 註冊帳號(選 username) 2. Settings → Developer settings → PAT → 產生並複製(只顯示一次!) 3. 本機設定身份 git config --global user.name "你的名字" git config --global user.email "你的email" 4. 建立測試 repo 並 push(詳見單元 2)
✅ 能登入 github.com
✅ 已產生 PAT 或 SSH key
✅ git config --list 看得到身份
✅ 成功 push 過一次
✅ 知道 Token 該存在哪裡(密碼管理員)
實作上,gh auth login(單元 8)一次就搞定 PAT + 身份設定,新手最推薦。
進入任何 repo 頁面,先認得這幾個區塊——以後每個單元都會用到:
┌─────────────────────────────────────┐ │ owner/repo ⭐ ★ | Code Issues PRs │ ├─────────────────────────────────────┤ │ main ▼ add file ▼ commit 歷史 │ │ ├── README.md ← 專案門面 │ │ ├── src/ │ │ └── .github/ │ └─────────────────────────────────────┘ 標籤頁:Code / Issues / Pull requests / Actions / Projects / Wiki
git --version;若沒安裝,到 git-scm.com 下載。然後設定你的 user.name / user.email,為單元 2 做準備。你是新創團隊 TechCo 的第一位工程師, 負責建立 GitHub Organization, 設定 SAML SSO、Branch Protection、 Secret Scanning,並用 gh CLI 批次建立 三個 repo(frontend / backend / infra)。
# 1. gh CLI 登入並設定身份 gh auth login gh auth setup-git # 2. 用 API 建立 Organization gh api --method POST /orgs \ -f login=TechCo \ -f name="TechCo Inc." \ -f billing_email="admin@techco.dev" # 3. 批次建立三個 repo for svc in frontend backend infra; do gh repo create "TechCo/$svc" --private \ --description "$svc service" done # 4. 為每個 repo 開啟 Secret Scanning for svc in frontend backend infra; do gh api --method PUT \ "/repos/TechCo/$svc" \ -f security_and_analysis='{"secret_scanning":{"status":"enabled"}}' done # 5. 設定 Branch Protection(main) gh api --method PUT \ "/repos/TechCo/backend/branches/main/protection" \ -f required_status_checks='{"strict":true,"contexts":["test"]}' \ -f enforce_admins=true \ -f required_pull_request_reviews='{"required_approving_review_count":2}'
Git(本機)──HTTPS/SSH──▶ GitHub.com(Git 服務)
│
┌───────────────┼───────────────┐
▼ ▼ ▼
Issues / PR Actions Pages
(MySQL 資料庫) (Runner VM) (CDN + S3)
│
▼
GitHub API v3 / GraphQL v4
(REST + GraphQL 雙介面)
Settings → SSH keys。gh auth login 背後觸發 OAuth device flow:CLI 跳出瀏覽器 → 授權 → 取得 access token → 存在本機 credential store。gh api /rate_limit 可查剩餘。repo(讀寫 repo)、workflow(改 Actions)、admin:org(管理 org)。權限太少會在 API 呼叫時收到 403。ed25519 與 RSA 兩種,ed25519 更安全且更快——用 ssh-keygen -t ed25519 產生。| 症狀 | 可能原因 | 解決方案 |
|---|---|---|
git push 回 HTTP 401 | Token 過期或權限不足 | 到 Settings → Developer settings → PAT 檢查;用 gh auth status 查目前身份 |
Permission denied (publickey) | SSH key 未上傳或 agent 未載入 | ssh-add -l 確認;未上傳就 copy id 公鑰貼到 GitHub |
gh: not found | gh CLI 未安裝或不在 PATH | brew install gh(macOS)或 sudo apt install gh(Ubuntu) |
| API 回 403 Rate Limited | 超過 API 呼叫配額 | gh api /rate_limit 確認;加 token 認證或等待重設 |
fatal: remote origin already exists | 已設定過 origin | git remote set-url origin <url> 改指向 |
ssh -T git@github.com 顯示「Hi username!」。然後再產生一把 key 加到你的 Organization,測試兩把 key 在不同 repo 的行為差異。把本單元的知識(平台、認證、Organization、安全治理)放大到整家公司的規模:所有設定都寫成程式碼(Infrastructure as Code),放進一個 techco-infra repo,讓「公司怎麼用 GitHub」也可以審查、版本化、重現——這是單元 1.9「單一 repo 治理」之上的整站層級。
techco-infra/ ├── .github/ │ ├── CODEOWNERS # 設定變更需 DevOps 審查 │ └── workflows/ │ └── audit.yml # 每晚稽核「設定有沒有漂移」 ├── scripts/ │ ├── bootstrap-org.sh # 一次性初始化(延伸 1.9) │ └── apply-policies.sh # 把策略套到所有 repo ├── policies/ │ ├── org.yml # org 層級規則 │ └── branch-protection.json# 標準保護分支設定 └── README.md
# policies/branch-protection.json(片段) { "required_status_checks": { "strict": true, "contexts": ["ci"] }, "required_pull_request_reviews": { "required_approving_review_count": 2 }, "enforce_admins": true } # scripts/apply-policies.sh(核心迴圈) for repo in $(gh repo list TechCo \ --json name --jq '.[].name'); do gh api --method PUT \ "/repos/TechCo/$repo/branches/main/protection" \ --input policies/branch-protection.json done
# 1. 驗證所有 repo 的保護分支都生效 gh api /repos/TechCo/backend/branches/main/protection \ --jq '.required_pull_request_reviews.required_approving_review_count' # 輸出 → 2 # 2. 稽核成員 2FA 狀態(org 稽核 log) gh api "/orgs/TechCo/audit-log" --paginate \ --jq '.[] | select(.action=="org.two_factor_requirement_enabled")' | head -1 # 輸出 → {"action":"org.two_factor_requirement_enabled", ...} # 3. 全公司 repo 清單與可見性 gh repo list TechCo --limit 100 --json name,visibility # 輸出 → [{"name":"backend","visibility":"PRIVATE"}, ...]
# 傳輸與 API 面 - git clone 用 SSH 比 HTTPS 少一層認證往返 - API rate limit:認證 5,000 次/小時,批次優先 - 大 repo 用 --filter=blob:none 只拉需要的物件 - LFS 檔案走獨立 CDN,不卡 Git 傳輸 # 回報與儀表板 - github.com/status 查平台可用性 - gh api /rate_limit 檢查剩餘額度
平台層品質由 GitHub 保證(SLA、Status Page), 你這一層的品質是「設定品質」: - 每條規則都有人負責(CODEOWNERS) - 設定進 repo:可 diff、可 review - 稽核 workflow 每天抓「設定漂移」 - 新 repo 用預設範本(repo template) 自動帶上安全設定,避免「開局就裸奔」
安全由上到下三層: 1. 身份層:SAML SSO + 強制 2FA 2. 存取層:最小權限 + 保護分支 3. 內容層:Secret Scanning + push protection 紅線:PAT 只用最小 scope、不放進 repo、 失效立刻撤銷;金鑰一律存密碼管理員。
| 面向 | github | gitlab | markdown | yaml | json |
|---|---|---|---|---|---|
| 定位 | 雲端程式碼託管平台 | 雲端 / 自架 DevOps 平台 | 輕量排版語法 | 人類可讀設定格式 | 資料交換格式 |
| 認證 | PAT / SSH / OAuth | PAT / OAuth / SAML | —(不涉及) | —(不涉及) | —(不涉及) |
| 組織治理 | Organization + Teams | Group + Subgroups(權限更細) | — | — | — |
| 與其他欄的關係 | 全站以 Markdown 寫文件、YAML 寫 Actions、JSON 做 API 資料 | 同左(CI 也用 YAML) | GitHub README / Issue 的載體 | GitHub Actions workflow 的載體 | GitHub REST API 的回應格式 |