The 'Authentication failed' error occurs when Git cannot verify your credentials with the remote repository. This typically happens because GitHub no longer accepts password authentication for HTTPS, requiring you to use a Personal Access Token or switch to SSH authentication.
This error indicates that Git's attempt to authenticate with the remote repository (typically GitHub, GitLab, or Bitbucket) was rejected. The remote server could not verify your identity using the credentials provided. Since August 2021, GitHub no longer accepts password authentication for Git operations over HTTPS. This is the most common cause of this error for GitHub users. Instead of your password, you must use a Personal Access Token (PAT) or authenticate via SSH. The authentication process works as follows: when you push, pull, or clone over HTTPS, Git sends your credentials to the remote server. If the server cannot validate these credentials, it returns this "Authentication failed" error. This can happen for several reasons: 1. **Password authentication is disabled** - Most Git hosting services now require tokens or SSH keys 2. **Incorrect or expired credentials** - Your cached credentials may be outdated 3. **Two-factor authentication** - 2FA requires special tokens rather than passwords 4. **Credential manager issues** - Stored credentials in your OS credential manager may be corrupted or stale
GitHub and many other Git hosting services no longer accept passwords for HTTPS authentication. You need to create a Personal Access Token (PAT):
For GitHub:
1. Go to [github.com/settings/tokens](https://github.com/settings/tokens)
2. Click Generate new token > Generate new token (classic)
3. Give it a descriptive name (e.g., "Development laptop")
4. Set an expiration date (or "No expiration" for convenience, though less secure)
5. Select scopes:
- repo - Full control of private repositories (required for push/pull)
- workflow - If you need to update GitHub Actions workflows
6. Click Generate token and copy it immediately (you won't see it again!)
Use the token as your password:
git push origin main
# When prompted for password, paste your Personal Access TokenFor GitLab: Settings > Access Tokens > Add new token
For Bitbucket: Personal Settings > App passwords > Create app password
Old passwords or tokens may be cached in your operating system's credential manager. Clear them to force Git to prompt for new credentials:
On Windows (Credential Manager):
# Open Credential Manager
control /name Microsoft.CredentialManager
# Or via command line - list credentials
cmdkey /list
# Delete GitHub credentials
cmdkey /delete:git:https://github.comAlternatively:
1. Open Control Panel > Credential Manager > Windows Credentials
2. Find entries starting with git:https://github.com
3. Click the entry and select Remove
On macOS (Keychain):
# Delete Git credentials from Keychain
git credential-osxkeychain erase
host=github.com
protocol=https
# Press Enter twice
# Or use Keychain Access app to find and delete "github.com" entriesOn Linux (credential helper):
# Check what credential helper is configured
git config --global credential.helper
# If using 'store' (plain text file)
rm ~/.git-credentials
# If using 'cache'
git credential-cache exit
# If using GNOME Keyring or libsecret
# Open Seahorse (Passwords and Keys) and remove GitHub entriesAfter clearing, try your Git operation again - you'll be prompted for credentials.
After generating a new token, configure Git to remember it so you don't have to enter it repeatedly:
Using Git Credential Manager (Recommended):
# Install Git Credential Manager (if not already installed)
# On Windows: comes with Git for Windows
# On macOS: brew install git-credential-manager
# On Linux: see https://github.com/git-ecosystem/git-credential-manager
# Configure Git to use it
git config --global credential.helper managerSimple credential caching:
# Cache credentials in memory for 1 hour (3600 seconds)
git config --global credential.helper 'cache --timeout=3600'
# Or store permanently in a plain text file (less secure)
git config --global credential.helper storemacOS Keychain:
git config --global credential.helper osxkeychainNow when you enter your username and Personal Access Token, they'll be saved for future use.
SSH authentication is often more reliable and doesn't require tokens. Here's how to set it up:
Step 1: Check for existing SSH keys:
ls -la ~/.ssh
# Look for id_ed25519.pub or id_rsa.pubStep 2: Generate a new SSH key (if needed):
ssh-keygen -t ed25519 -C "[email protected]"
# Press Enter to accept default location
# Enter a passphrase (optional but recommended)Step 3: Add the SSH key to your SSH agent:
# Start the SSH agent
eval "$(ssh-agent -s)"
# Add your key
ssh-add ~/.ssh/id_ed25519Step 4: Add the public key to GitHub:
# Copy your public key
cat ~/.ssh/id_ed25519.pub
# Copy the output1. Go to [github.com/settings/keys](https://github.com/settings/keys)
2. Click New SSH key
3. Paste your public key and save
Step 5: Change your remote URL to SSH:
# Check current remote
git remote -v
# Change from HTTPS to SSH
git remote set-url origin [email protected]:username/repo.git
# Test the connection
ssh -T [email protected]For CI/CD pipelines or automated scripts, you can embed credentials in the URL (use secrets management in production):
# Set remote with token embedded (for automation only)
git remote set-url origin https://USERNAME:[email protected]/username/repo.git
# Clone with token
git clone https://USERNAME:[email protected]/username/repo.gitImportant security considerations:
- Never commit URLs with tokens to version control
- Use CI/CD secret management (GitHub Actions secrets, GitLab CI variables)
- Tokens in URLs may appear in logs - be careful
Better approach for CI/CD:
# Use environment variables
git config --global credential.helper '!f() { echo "password=$GIT_TOKEN"; }; f'
# Or configure inline
git -c "credential.helper=!f() { echo password=$GIT_TOKEN; }; f" push origin mainAuthentication can fail if you're using the wrong username. Verify you're using the correct one:
For GitHub:
- Use your username, not your email address
- Your username is shown at [github.com/settings/profile](https://github.com/settings/profile)
- Usernames are case-insensitive but stored as lowercase
# Check your configured username
git config --global user.name
# The username for authentication should match your GitHub username
# NOT your display name or emailCheck repository access:
# Ensure you have access to the repository
# Try opening the repository URL in a browser while logged inFor organization repositories:
- Ensure your account has been granted access to the organization
- Check if SSO is required: [github.com/settings/organizations](https://github.com/settings/organizations)
- You may need to authorize your token for SSO
Older versions of Git may have bugs with authentication or lack support for modern credential managers:
Check your Git version:
git --versionUpdate Git:
On Windows:
# Using Git itself
git update-git-for-windows
# Or download from https://git-scm.com/download/winOn macOS:
# Using Homebrew
brew upgrade git
# Or download from https://git-scm.com/download/macOn Ubuntu/Debian:
sudo add-apt-repository ppa:git-core/ppa
sudo apt update
sudo apt install gitOn Fedora/RHEL:
sudo dnf update gitAfter updating, try your Git operation again.
### Two-Factor Authentication (2FA) Considerations
When 2FA is enabled on your Git hosting account, regular passwords never work for HTTPS authentication. You must use:
- Personal Access Tokens (most common)
- SSH keys (recommended for daily development)
- GitHub CLI (gh auth login) which handles authentication automatically
### Git Credential Manager (GCM)
Git Credential Manager is the modern, cross-platform solution for credential storage:
# Check if GCM is installed
git credential-manager --version
# Configure with OAuth (opens browser)
git credential-manager configure
# GCM supports:
# - GitHub (with OAuth device flow)
# - Azure DevOps
# - Bitbucket
# - GitLab### Debugging Authentication
# Enable Git credential debugging
GIT_TRACE=1 git push origin main
# More detailed credential debugging
GIT_CURL_VERBOSE=1 git push origin main
# Check credential helper configuration
git config --list --show-origin | grep credential### Corporate Environments
If you're behind a corporate proxy:
# Configure proxy for Git
git config --global http.proxy http://proxy.company.com:8080
git config --global https.proxy https://proxy.company.com:8080
# If proxy requires authentication
git config --global http.proxy http://username:[email protected]:8080### GitHub CLI Alternative
The GitHub CLI (gh) can handle authentication more seamlessly:
# Install GitHub CLI
# brew install gh (macOS)
# winget install GitHub.cli (Windows)
# Authenticate (opens browser)
gh auth login
# Clone repositories
gh repo clone username/repo
# The CLI configures Git credentials automatically### Fine-Grained Personal Access Tokens
GitHub now offers fine-grained PATs with more granular permissions:
1. Go to [github.com/settings/tokens?type=beta](https://github.com/settings/tokens?type=beta)
2. Select specific repositories
3. Choose only the permissions you need
4. Set an expiration date
These are more secure than classic tokens as they limit potential damage if compromised.
### Troubleshooting Windows Credential Manager Issues
If authentication keeps failing on Windows despite correct credentials:
# Reset Git credential manager
git credential-manager unconfigure
git credential-manager configure
# Or reinstall Git for Windows with GCM### SSH Agent Forwarding
For accessing repositories from remote servers:
# Add to ~/.ssh/config
Host github.com
ForwardAgent yes
# Connect to server with agent forwarding
ssh -A user@serverkex_exchange_identification: Connection closed by remote host
Connection closed by remote host when connecting to Git server
fatal: unable to access: Proxy auto-configuration failed
How to fix 'Proxy auto-configuration failed' in Git
fatal: unable to access: Authentication failed (proxy requires basic auth)
How to fix 'Authentication failed (proxy requires basic auth)' in Git
fatal: unable to access: no_proxy configuration not working
How to fix 'no_proxy configuration not working' in Git
fatal: unable to read tree object in treeless clone
How to fix 'unable to read tree object in treeless clone' in Git