DEV Community

Nutan Mishra
Nutan Mishra

Posted on

Fixing GitHub Authentication Error: "erase operation not supported" and "Invalid username or token"

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
Enter fullscreen mode Exit fullscreen mode
  • 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)

  1. Refresh your auth:
   gh auth refresh -h github.com
Enter fullscreen mode Exit fullscreen mode

Follow the browser prompts to re-authenticate.

  1. If that fails, log out and back in:
   gh auth logout
   gh auth login
Enter fullscreen mode Exit fullscreen mode
  • Choose GitHub.com and HTTPS.
  • Authenticate via browser or token.
  • Agree to set up the Git credential helper.
  1. Set up Git credential helper explicitly:
   gh auth setup-git
Enter fullscreen mode Exit fullscreen mode
  1. Test with:
   git push
Enter fullscreen mode Exit fullscreen mode

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)

  1. 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.
  2. Update your remote URL:

   git remote set-url origin https://<YOUR_TOKEN>@github.com/YOUR_USERNAME/YOUR_REPO.git
Enter fullscreen mode Exit fullscreen mode
  1. 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, then git push and enter username/PAT once.
  2. Test with git push.

Fix 3: Switch to SSH (Recommended for Long-Term)

  1. Generate an SSH key:
   ssh-keygen -t ed25519 -C "your_email@example.com"
Enter fullscreen mode Exit fullscreen mode

Press Enter for defaults; add a passphrase (optional).

  1. Start SSH agent and add key:
   eval "$(ssh-agent -s)"
   ssh-add ~/.ssh/id_ed25519
Enter fullscreen mode Exit fullscreen mode
  1. Add public key to GitHub:

    • Copy: cat ~/.ssh/id_ed25519.pub
    • GitHub > Settings > SSH and GPG keys > New SSH key > Paste and save.
  2. Update remote to SSH:

   git remote set-url origin git@github.com:YOUR_USERNAME/YOUR_REPO.git
Enter fullscreen mode Exit fullscreen mode
  1. Test SSH:
   ssh -T git@github.com
Enter fullscreen mode Exit fullscreen mode

Should say "Hi YOUR_USERNAME! You've successfully authenticated..."

  1. 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 with git 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 with gh commands.
  • Debug: Run GIT_TRACE=1 git push for detailed logs.

Top comments (0)