This guide addresses the GitHub CLI (gh) error gh auth git-credential: "erase" operation not supported
and the related remote: Invalid username or token
error when using Git with HTTPS.
Why This Happens
- The GitHub CLI (
gh
) is set as your Git credential helper but doesn't support the "erase" operation Git sometimes uses. - GitHub no longer allows password authentication for Git operations (since August 2021). You need a personal access token (PAT) or SSH.
- Stale or invalid credentials (e.g., expired PAT) cause the "invalid username or token" error.
Step 1: Check Your Setup
Run these commands to diagnose:
gh auth status
git config --get credential.helper
git remote -v
-
gh auth status
: Shows your GitHub CLI login status and protocol (HTTPS/SSH). -
git config --get credential.helper
: If it shows!gh auth git-credential
, that's likely causing the "erase" issue. -
git remote -v
: Confirms if you're using HTTPS (https://github.com/...
) or SSH (git@github.com:...
).
Fix 1: Refresh GitHub CLI Authentication (For HTTPS)
- Refresh your auth:
gh auth refresh -h github.com
Follow the browser prompts to re-authenticate.
- If that fails, log out and back in:
gh auth logout
gh auth login
- Choose GitHub.com and HTTPS.
- Authenticate via browser or token.
- Agree to set up the Git credential helper.
- Set up Git credential helper explicitly:
gh auth setup-git
- Test with:
git push
Tip: Update GitHub CLI (gh version
) to 2.40+ if issues persist. Reinstall from cli.github.com.
Fix 2: Use a Personal Access Token (PAT) Directly (For HTTPS)
-
Generate a PAT:
- Go to GitHub > Settings > Developer settings > Personal access tokens > Tokens (classic) > Generate new token.
- Name it (e.g., "Git Access").
- Select scopes:
repo
(required),workflow
(for Actions). - Copy the token.
Update your remote URL:
git remote set-url origin https://<YOUR_TOKEN>@github.com/YOUR_USERNAME/YOUR_REPO.git
-
Or cache the PAT:
-
Windows: Credential Manager > Windows Credentials > Add
git:https://github.com
with username and PAT. -
macOS: Keychain Access > Add/update
github.com
password. -
Linux: Run
git config --global credential.helper store
, thengit push
and enter username/PAT once.
-
Windows: Credential Manager > Windows Credentials > Add
Test with
git push
.
Fix 3: Switch to SSH (Recommended for Long-Term)
- Generate an SSH key:
ssh-keygen -t ed25519 -C "your_email@example.com"
Press Enter for defaults; add a passphrase (optional).
- Start SSH agent and add key:
eval "$(ssh-agent -s)"
ssh-add ~/.ssh/id_ed25519
-
Add public key to GitHub:
- Copy:
cat ~/.ssh/id_ed25519.pub
- GitHub > Settings > SSH and GPG keys > New SSH key > Paste and save.
- Copy:
Update remote to SSH:
git remote set-url origin git@github.com:YOUR_USERNAME/YOUR_REPO.git
- Test SSH:
ssh -T git@github.com
Should say "Hi YOUR_USERNAME! You've successfully authenticated..."
- Test with
git push
.
Troubleshooting
- Expired PAT? Check GitHub > Settings > Developer settings > Personal access tokens. Regenerate if needed.
- 2FA Enabled? Passwords won't work—use PAT or SSH.
-
Wrong Remote? Verify with
git remote -v
. Fix withgit remote set-url
. -
Clear Cached Creds:
- Windows: Credential Manager > Remove GitHub entries.
- macOS: Keychain Access > Delete
github.com
entries. - Linux:
git credential-cache exit
or edit~/.git-credentials
.
-
Enterprise GitHub? Use
--hostname yourcompany.com
withgh
commands. -
Debug: Run
GIT_TRACE=1 git push
for detailed logs.
Top comments (0)