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 address

Or:

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 gh

Ubuntu/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 gh

macOS:

brew install gh

Windows:

winget install GitHub.cli

2. Authenticate with GitHub

gh auth login --web

This 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-username

3. 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 main

That's it! No more authentication headaches.

Why This Approach is Better

Security Benefits

  1. Encrypted Storage: GitHub CLI stores your authentication token securely in your system's credential store, not in plain text.
  2. Token-Based Authentication: Uses GitHub's Personal Access Tokens (PATs) instead of your actual password, with scoped permissions.
  3. Automatic Token Management: Handles token refresh automatically without manual intervention.
  4. Revocable Access: You can revoke the token anytime from your GitHub settings.

Convenience Benefits

  1. Browser Authentication: No need to generate SSH keys or manage personal access tokens manually.
  2. Global Configuration: Works for all your Git repositories automatically.
  3. Cross-Platform: Works on Linux, macOS, and Windows.
  4. No Password Prompts: Never get stuck on password prompts in non-interactive environments.

Managing Your Authentication

Check Authentication Status

gh auth status

Log Out (if needed)

gh auth logout

Re-authenticate

gh auth login --web

Alternative 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.git

Personal 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 prompted

Troubleshooting

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

  1. Check your GitHub CLI authentication:
  2. Re-authenticate if needed:
  3. Verify your remote URL:

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.