Git Credentials Expired Mid-Session — Fix (2026)
The Error
remote: Invalid username or password.
fatal: Authentication failed for 'https://github.com/user/repo.git'
Git credential token expired during Claude Code session.
Push/fetch operations will fail until credentials are refreshed.
This appears when your GitHub personal access token (PAT) or OAuth token expires while Claude Code is running, causing all remote git operations to fail.
The Fix
gh auth refresh
- Refresh your GitHub CLI authentication, which also updates the git credential helper.
- Verify the new token works:
gh auth status. - Resume your Claude Code session — push and fetch will work again.
Why This Happens
GitHub tokens (PATs, OAuth tokens, fine-grained tokens) have expiration dates. If a token expires during a long Claude Code session, all git push, git fetch, and git pull commands fail. Claude Code does not detect the authentication failure as a token expiry — it sees it as a generic push rejection and may retry repeatedly. This is especially common with fine-grained PATs that have short (7-day) expiration windows.
If That Doesn’t Work
Re-authenticate with the GitHub CLI:
gh auth login
Update a stored personal access token:
git credential reject <<EOF
protocol=https
host=github.com
EOF
gh auth login --with-token < ~/.github-token
Switch to SSH authentication to avoid token expiry:
git remote set-url origin git@github.com:user/repo.git
ssh-add ~/.ssh/id_ed25519
Prevention
# CLAUDE.md rule
Use SSH keys instead of HTTPS tokens for git authentication — SSH keys do not expire. If using PATs, set expiration to 90 days minimum. Run 'gh auth status' at the start of each session to verify credentials.
Related Error Messages
This fix also applies if you see these related error messages:
fatal: not a git repositoryerror: failed to push some refsfatal: refusing to merge unrelated historiesTokenLimitExceeded: max tokens reachedError: output truncated at max_tokens
Frequently Asked Questions
Why does Claude Code require git?
Claude Code uses git for several core operations: tracking file changes, creating commits, reading blame information, searching history with git log, and managing branches. Without git, these operations fail and Claude Code falls back to less efficient alternatives.
Can Claude Code work in a non-git directory?
Yes, but with reduced functionality. File search and editing work normally, but version control operations (commit, diff, blame) are unavailable. Claude Code displays a warning when opened in a directory without git initialization.
How do I prevent Claude Code from making unwanted git operations?
Add rules to your CLAUDE.md: Do not create commits automatically. Do not run git push. Always ask before any git operation that modifies history. Claude Code respects these constraints and asks for confirmation before proceeding.
What causes token count mismatches?
Token counts are estimated before sending a request and precisely calculated on the server. The estimation uses a fast local tokenizer that may differ slightly from the server’s tokenizer. Small discrepancies (1-3%) are normal and do not affect functionality.
Related Guides
Try it: Paste your error into our Error Diagnostic for an instant fix.
- Terminal Emulator Rendering Artifacts — Fix (2026)
- How to Use Thirdweb SDK Workflow (2026)
- Python Virtualenv Not Activated — Fix (2026)
- Claude Code Offline Mode Setup (2026)
Git Operations in Claude Code: Safety Checklist
Claude Code can execute git commands, which makes safety guardrails important:
Before any destructive operation: Always check git status and git stash list to confirm there are no uncommitted changes that could be lost.
Branch management: Claude Code should create feature branches for non-trivial changes rather than committing directly to main. Use the pattern git checkout -b claude/feature-name to clearly identify AI-generated branches.
Commit message conventions: Configure your preferred commit format in CLAUDE.md. Claude Code follows the format you specify. Common formats: Conventional Commits (feat: add user search), Angular style, or simple descriptive messages.
Common Git Mistakes Claude Code Makes
-
Amending the wrong commit. If a pre-commit hook fails, Claude Code sometimes uses
--amendon the next attempt, which modifies the previous (successful) commit instead of creating a new one. Configure CLAUDE.md with: “Never use git commit –amend. Always create new commits.” -
Force pushing to shared branches. Claude Code may suggest
git push --forceto resolve push rejections. AddBash(git push --force*)to your deny list in settings.json. -
Committing generated files. Without guidance, Claude Code may commit
dist/,node_modules/, or.envfiles. Ensure your.gitignoreis complete and add a pre-commit hook that checks for these.