GitHub SSH 金鑰設定

GitHub SSH Setting

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12

    cd ~/
    mkdir .ssh 
    ssh-keygen -t ed25519 -C "your_email@example.com"
    
    (是否要用預設檔名) enter >> (密碼) enter >> (密碼確認) enter

    cat .ssh/id_ed25519.pub
    >> 跑出公鑰 貼到 github 上

    ssh -T git@github.com
    >> 測試連線 >> "Hi xinqilin! You've successfully authenticated, but GitHub does not provide shell access."

重設 remote url => https 改 ssh

1
2
    git remote rm origin
    git remote add origin [url]

應用程式要用的 token

Settings → Developer settings → Personal access tokens → Generate new token

Personal Access Token (PAT) 類型

Classic PAT vs Fine-grained PAT

Classic PATFine-grained PAT(推薦)
存取範圍帳號層級,可存取全部 repo指定 repo 或 org
過期時間可設定,可永不過期最長 1 年
細粒度權限粗粒度(read:repo、write:packages)細粒度(如只有 contents:read)
推薦場景舊系統整合新開發,CI/CD

建立 Fine-grained PAT

  1. GitHub → Settings → Developer settings → Personal access tokens → Fine-grained tokens
  2. 選擇 Resource owner(個人或 org)
  3. 選擇 Repository access(Only select repositories)
  4. 配置最小 Permissions(原則:最小權限)
  5. 設定過期日期(建議 90 天,配合 CI 定期 rotate)

SSH Key vs HTTPS PAT

SSH KeyHTTPS + PAT
設定複雜度略高(需管理 key pair)低
CI/CD需安全存放私鑰Token 存 secret,較方便
互動操作推薦(無需每次輸入)需搭配 credential helper
Fine-grained 控制無(key 綁定整個帳號)Fine-grained PAT 支援

參考資料

留言討論