GitHub Personal Access Token (PAT) 產生方式
步驟
- 登入 GitHub → 點右上角頭像 → Settings
- 左側選單滾到最底 → Developer settings
- 選擇 Personal access tokens
- 選擇 token 類型:
| 類型 | 說明 |
|---|---|
| Fine-grained tokens(推薦) | 可限定特定 repo、特定權限,更安全 |
| Tokens (classic) | 傳統方式,權限較粗,適合快速測試 |
- 點 Generate new token
- 設定名稱、到期日、權限範圍
- 點 Generate token → 立刻複製保存(離開頁面後就看不到了)
直達連結
- Fine-grained:
https://github.com/settings/tokens?type=beta - Classic:
https://github.com/settings/tokens
權限設定建議(用於自動 push)
Fine-grained token
- Repository access → 選 “Only select repositories” → 指定你的 repo
- Permissions → Contents → Read and write
Classic token
- 勾選
repo(完整 repo 存取權限即可)
使用方式
產生 token 後,用在 HTTPS remote URL 中:
# 方式一:直接設定 remote URL
git remote set-url origin https://你的帳號:ghp_xxxxxxxxxxxx@github.com/你的帳號/repo名稱.git
# 方式二:用 git credential 快取(避免 token 明文存在設定檔)
git config --global credential.helper store
# 下次 push 時輸入帳號和 token,之後就會自動記住
安全提醒
- 不要把 token 寫進程式碼或公開 repo
- 建議設定到期日(30 天或 90 天)
- 用 Fine-grained token 限定最小權限和指定 repo
- 如果只是本機使用,SSH Key 比 PAT 更推薦,不用管 token 過期問題