How do I authenticate with GitHub when pushing commits?
Since August 2021, GitHub no longer accepts passwords for Git operations over HTTPS. If you try to push using a password, you'll get an Authentication failed error. Here are the modern ways to authenticate with GitHub.
Using SSH keys (recommended)
SSH key authentication is the most common approach for developers who work with GitHub regularly. Once set up, you won't need to enter credentials at all.
- Generate an SSH key (if you don't already have one):
ssh-keygen -t ed25519 -C "your_email@example.com"
- Add the key to your SSH agent:
eval "$(ssh-agent -s)"
ssh-add ~/.ssh/id_ed25519
- Copy the public key and add it to your GitHub account under Settings > SSH and GPG keys:
cat ~/.ssh/id_ed25519.pub
- Switch your remote URL to SSH:
git remote set-url origin git@github.com:username/repo.git
For a full walkthrough, see GitHub's SSH key guide.
Using personal access tokens (PATs)
If you prefer HTTPS URLs, you can use a personal access token instead of a password. Generate one in GitHub > Settings > Developer settings > Personal access tokens > Tokens (classic), and give it repo scope.
Then use it as your password when Git prompts you:
$ git push origin main
Username: your-github-username
Password: ghp_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
The token works anywhere a password used to. You can combine it with a credential helper (see below) so you don't have to paste it every time.
Using GitHub CLI
The GitHub CLI handles authentication for you and works with both HTTPS and SSH:
gh auth login
Follow the interactive prompts to authenticate via browser. Once done, Git operations through HTTPS are automatically authenticated --- no tokens to manage manually.
Credential helpers
If you're using HTTPS with a personal access token, credential helpers save your token so you only enter it once.
Temporary cache (stores in memory, clears after timeout):
git config --global credential.helper 'cache --timeout=3600'
Permanent storage on disk (plaintext --- only use on machines you trust):
git config --global credential.helper store
macOS Keychain (encrypted, recommended on Mac):
git config --global credential.helper osxkeychain
Windows Credential Manager (built into Git for Windows):
git config --global credential.helper manager
After configuring a helper, the next time you enter your token during a push or fetch, it will be saved automatically.
Related
- Publishing local changes --- pushing commits to a remote
git pushcommand referencegit remotecommand referencegit configcommand reference
DeployHQ connects directly to your GitHub repositories and deploys automatically when you push. You can set up SSH key or token-based authentication once and let DeployHQ handle the rest --- try it free.