GitHub Personal Access Token (PAT) 產生方式

GitHub Personal Access Token (PAT) 產生方式

步驟

  1. 登入 GitHub → 點右上角頭像Settings
  2. 左側選單滾到最底 → Developer settings
  3. 選擇 Personal access tokens
  4. 選擇 token 類型:
類型 說明
Fine-grained tokens(推薦) 可限定特定 repo、特定權限,更安全
Tokens (classic) 傳統方式,權限較粗,適合快速測試
  1. Generate new token
  2. 設定名稱、到期日、權限範圍
  3. Generate token立刻複製保存(離開頁面後就看不到了)

直達連結

  • Fine-grainedhttps://github.com/settings/tokens?type=beta
  • Classichttps://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 過期問題