Git Authentication with GitHub CLI: The Easy and Secure Way
If you've ever tried to push to GitHub and encountered errors like:
fatal: could not read Password for '<https://username@github.com>': No such device or addressOr:
Host key verification failed.
fatal: Could not read from remote repository.You're not alone. Git authentication with GitHub can be frustrating, especially when working in environments where you can't interactively enter passwords.
GitHub CLI Credential Helper
GitHub CLI provides a seamless way to authenticate with GitHub through your browser and automatically handle Git authentication. Here's how to set it up:
Step-by-Step Setup
1. Install GitHub CLI
Ubuntu (Snap - Recommended):
sudo snap install ghUbuntu/Debian (APT):
curl -fsSL <https://cli.github.com/packages/githubcli-archive-keyring.gpg> | sudo dd of=/usr/share/keyrings/githubcli-archive-keyring.gpg
echo "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/githubcli-archive-keyring.gpg] <https://cli.github.com/packages> stable main" | sudo tee /etc/apt/sources.list.d/github-cli.list > /dev/null
sudo apt update
sudo apt install ghmacOS:
brew install ghWindows:
winget install GitHub.cli2. Authenticate with GitHub
gh auth login --webThis command will:
- Generate a one-time code
- Open your browser to GitHub's device authentication page
- Automatically complete the authentication process
Example output:
! First copy your one-time code: 1CB2-66AE
Open this URL to continue in your web browser: <https://github.com/login/device>
✓ Authentication complete.
✓ Logged in as your-username3. Configure Git to Use GitHub CLI
git config --global credential.helper "!gh auth git-credential"This tells Git to use GitHub CLI for all authentication needs.
4. Test Your Setup
git push origin mainThat's it! No more authentication headaches.
Why This Approach is Better
Security Benefits
- Encrypted Storage: GitHub CLI stores your authentication token securely in your system's credential store, not in plain text.
- Token-Based Authentication: Uses GitHub's Personal Access Tokens (PATs) instead of your actual password, with scoped permissions.
- Automatic Token Management: Handles token refresh automatically without manual intervention.
- Revocable Access: You can revoke the token anytime from your GitHub settings.
Convenience Benefits
- Browser Authentication: No need to generate SSH keys or manage personal access tokens manually.
- Global Configuration: Works for all your Git repositories automatically.
- Cross-Platform: Works on Linux, macOS, and Windows.
- No Password Prompts: Never get stuck on password prompts in non-interactive environments.
Managing Your Authentication
Check Authentication Status
gh auth statusLog Out (if needed)
gh auth logoutRe-authenticate
gh auth login --webAlternative Methods (For Reference)
SSH Keys (More Complex)
# Generate SSH key
ssh-keygen -t ed25519 -C "your-email@example.com"
# Add to SSH agent
ssh-add ~/.ssh/id_ed25519
# Add public key to GitHub
cat ~/.ssh/id_ed25519.pub
# Copy output to GitHub → Settings → SSH and GPG keys
# Change remote URL to SSH
git remote set-url origin git@github.com:username/repo.gitPersonal Access Token (Manual)
# Generate token on GitHub → Settings → Developer settings → Personal access tokens
git config credential.helper store
git push origin main
# Enter username and token when promptedTroubleshooting
If You Get "credential-gh is not a git command"
Make sure you're using the correct credential helper format:
git config --global credential.helper "!gh auth git-credential"If Authentication Still Fails
- Check your GitHub CLI authentication:
``bash gh auth status
- Re-authenticate if needed:
``bash gh auth login --web
- Verify your remote URL:
``bash git remote -v
Conclusion
The GitHub CLI credential helper approach is the most user-friendly and secure way to handle Git authentication with GitHub. It eliminates the complexity of SSH key management while providing better security than password-based authentication.
Once set up, you'll never have to worry about Git authentication again. Just push, pull, and work with your repositories seamlessly!
---
_This guide covers the most common Git authentication issues and provides a modern, secure solution that works across all platforms._