GitHub Codespaces git and gh fail after OAuth token expiry while session stays active
0 reputation · 21 Nov 2021, 13:54 UTC
0 reputation · 21 Nov 2021, 13:54 UTC
A GitHub Codespaces session can remain open in the browser while repository Git operations and GitHub CLI API calls fail because the underlying OAuth token expired or was revoked. The goal is to determine whether the codespace automatically refreshes expired credentials for git and gh, or whether each client requires separate re-authentication.
GitHub Codespaces inherits repository access from the signer's existing GitHub permissions and relies on short-lived tokens held by the Git credential helper or GitHub CLI. These tokens can expire independently of session cookies, and documented behavior does not guarantee a single refresh path across the browser editor, VS Code desktop, and the codespace terminal. Least-privilege scopes are also inherited at creation rather than overridden per codespace.
Does an active codespace session automatically refresh expired OAuth tokens for terminal Git and gh commands? If not, which clients require manual re-authentication, and does behavior differ between GitHub Free, Team, and Enterprise Cloud?
37275 reputation · 21 Nov 2021, 17:00 UTC
In GitHub Codespaces, an open browser session does not guarantee that terminal Git or gh commands remain authenticated after an OAuth token expiry. The git credential helper, which caches repository access, can occasionally refresh through Codespace connector events, but it does not always detect or recover from an expired short-lived token. The GitHub CLI (gh) explicitly requires gh auth refresh or a fresh gh auth login once its token expires, regardless of the Codespaces browser state.
Behavior differs by account type. GitHub Free and Team codespaces use the same underlying token mechanism; both exhibit the same token-expiry symptoms, and manual re-authentication is needed when the helper or CLI cache becomes stale. Enterprise Cloud codespaces that rely on SAML or SSO defer token lifecycle management to the identity provider. In those cases, gh auth refresh may report success while actual access is governed by the IdP session, and a codespace restart may be required to reset the git credential helper state.
To verify the cause, run the following scoped commands inside the codespace terminal:
git -C <repository> status – a credential prompt or fatal: unable to access... confirms git token expiry.gh auth status followed by gh auth refresh – successful renewal without re‑login indicates the CLI token is current.If gh auth refresh completes but git still fails, the issue is likely credential-helper state, and restarting the codespace with code spaces restart may reset it.
The single diagnostic detail that would narrow the recommendation is whether the codespace was created under an enterprise account using SAML/SSO. Confirming this helps determine whether the problem resides in the identity provider session or in the local token cache.
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.