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.

1. Generate an SSH key (if you don't already have one):

```bash
ssh-keygen -t ed25519 -C "your_email@example.com"
```

2. Add the key to your SSH agent:

```bash
eval "$(ssh-agent -s)"
ssh-add ~/.ssh/id_ed25519
```

3. Copy the public key and add it to your GitHub account under **Settings > SSH and GPG keys**:

```bash
cat ~/.ssh/id_ed25519.pub
```

4. Switch your remote URL to SSH:

```bash
git remote set-url origin git@github.com:username/repo.git
```

For a full walkthrough, see [GitHub's SSH key guide](https://docs.github.com/en/authentication/connecting-to-github-with-ssh).

## 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:

```bash
$ 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](https://cli.github.com/) handles authentication for you and works with both HTTPS and SSH:

```bash
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):

```bash
git config --global credential.helper 'cache --timeout=3600'
```

**Permanent storage on disk** (plaintext --- only use on machines you trust):

```bash
git config --global credential.helper store
```

**macOS Keychain** (encrypted, recommended on Mac):

```bash
git config --global credential.helper osxkeychain
```

**Windows Credential Manager** (built into Git for Windows):

```bash
git config --global credential.helper manager
```

After configuring a helper, the next time you enter your token during a [push](/git/publishing-local-changes) or fetch, it will be saved automatically.

## Related

- [Publishing local changes](/git/publishing-local-changes) --- pushing commits to a remote
- [`git push` command reference](/git/publishing-local-changes)
- [`git remote` command reference](/git/commands/git-remote)
- [`git config` command reference](/git/commands/git-config)

---

[DeployHQ](https://www.deployhq.com) 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](https://www.deployhq.com/signup).
