usage

GitHub CLI 2.103.0

工具與目錄
在這頁

新功能 3 項

01

gh 可改用會自動續期的登入憑證(預覽)

一般用 gh 登入 GitHub,拿到的 token(證明你身分的授權令牌)發下來就不會過期。現在可以改選會過期的 OAuth 憑證:gh 會存一把短效的 access token,另配一把會輪替的 refresh token(專門用來換新令牌的憑證)。access token 需要時會自動換新,你不必重新登入。⟦公司門禁卡改成每天自動換發的臨時卡,以前一張卡用到離職,現在卡片天天換新,你照樣每天刷卡進門。⟧這是預覽功能,要自己選擇開啟。只想這一次登入用它,打:

```shell gh auth login --expiring ```

想以後每次登入都優先用它,打:

```shell gh config set expiring_credentials enabled ```

官方原文

## Expiring, auto-refreshing OAuth tokens (preview)

gh can now sign in with expiring OAuth credentials that it refreshes automatically. When you opt in, gh stores a short-lived access token alongside a rotating refresh token and refreshes the access token whenever it is needed, so you stay signed in without having to authenticate again.

To opt in for a single sign-in, run:

```shell

gh auth login --expiring

```

To prefer expiring credentials for every sign-in, run:

```shell

gh config set expiring_credentials enabled

```

02

用會過期的 gh 憑證時,查狀態、接 git 和寫腳本要注意的事

開了自動續期後,想知道憑證還剩多久,就打 gh auth status:它會顯示憑證能不能續期、access token 何時到期。它和 gh auth token 都會在需要時自動續期。github.com 和 GHE.com 支援這種 token,其他主機會退回一般不過期的 token。git 走 HTTPS 的話,建議用 git 2.46 以上,並執行 gh auth setup-git,讓 gh 當 git 的 credential helper(替 git 提供登入資料的程式)。這樣 git 不會把憑證留到過期之後。⟦跟鮮奶一樣,以前能囤一箱慢慢喝,現在得隨喝隨買。⟧腳本和擴充別把 gh auth token 的輸出存起來,每次要用都重新執行它;透過 GH_TOKEN 或 GITHUB_TOKEN 給的 token 則永遠不會續期。

官方原文

gh auth status shows whether a credential is refreshable and when the current access token expires. Both gh auth status and gh auth token automatically refresh the access token when needed. Expiring tokens are available on github.com and GHE.com; on hosts that do not issue them, gh falls back to a regular non-expiring token.

If you use HTTPS as your git protocol, we advise using git 2.46 or newer and letting gh act as your git credential helper by running gh auth setup-git. This way gh serves and refreshes the expiring credential for git, and git does not cache it past its lifetime.

> [!IMPORTANT]

> Expiring access tokens should not be cached. Scripts and extensions that store the output of gh auth token will eventually hold an expired token, so run gh auth token again each time a token is needed. Tokens supplied through GH_TOKEN or GITHUB_TOKEN are never refreshed.

For more information, see [Authenticating with GitHub CLI](https://docs.github.com/github-cli/github-cli/authenticating-with-github-cli#using-expiring-refreshable-tokens).

03

新指令 gh issue artifact,在終端機管理 issue 的附件文件(私人預覽)

規劃一個新功能時,你可能想把規劃書、流程圖或網頁原型直接掛在 GitHub issue(記錄待辦與問題的討論串)上。gh issue artifact 把 issue artifacts 帶到命令列,提供 list、view、create、edit、download、delete 六個指令,例如:

```shell # Create a Markdown document with an attached image gh issue artifact create 142 signin-plan.md --attach ./signin-flow.png

# Create an HTML document gh issue artifact create 142 menu-prototype.html

# List or view artifacts gh issue artifact list 142 gh issue artifact view 142 2

# Update a document or restore an earlier version gh issue artifact edit 142 2 --body-file signin-plan-v2.md gh issue artifact edit 142 2 --restore-version 5 ```

⟦替每張工單配一個專屬資料夾,規劃書和原型都收在裡面,還記得每一版,想退回第 5 版也行。⟧artifacts 也能用 GitHub 網址指定,Markdown 文件可以用 --attach 附上圖片和影片。這是私人預覽,只有加入預覽的使用者和組織能用,而且只對加入預覽的儲存庫有效;申請方式在官方的預覽公告討論串。

官方原文

## Manage issue artifacts with gh issue artifact (private preview)

gh issue artifact brings issue artifacts to the command line. Users and organizations enrolled in the private preview can use the list, view, create, edit, download, and delete commands to manage issue artifacts.

```shell

# Create a Markdown document with an attached image

gh issue artifact create 142 signin-plan.md --attach ./signin-flow.png

# Create an HTML document

gh issue artifact create 142 menu-prototype.html

# List or view artifacts

gh issue artifact list 142

gh issue artifact view 142 2

# Update a document or restore an earlier version

gh issue artifact edit 142 2 --body-file signin-plan-v2.md

gh issue artifact edit 142 2 --restore-version 5

```

Artifacts can also be addressed by their GitHub URLs, and Markdown documents support image and video attachments with --attach.

> [!IMPORTANT]

> Issue artifacts are in private preview. These commands only work for repositories enrolled in the preview. See the [Issue Artifacts private preview announcement and feedback discussion](https://github.com/orgs/community/discussions/205881) for access instructions and to share feedback.