SSH Keys or Personal Access Tokens for GitHub: Which One, and When
Overview
GitHub gives you two common ways to prove who you are: an SSH key and a personal access token (PAT). Both let a tool clone, pull and push, so they are often treated as interchangeable. They are not. They are built for different callers and they fail in different ways.
What each one is
An SSH key is a pair. The private half stays with you and signs a challenge. GitHub holds the public half and checks the signature. No secret is sent over the network, and nothing is typed.
A personal access token is a string that works like a password. Whoever holds the string can use it, from anywhere, until it expires or is revoked. A fine-grained token can be limited to chosen repositories and permissions.
The difference that matters most: an SSH login produces a signature, while a token is a value that is handed over. A signature can be produced without exposing the key. A token has to be present, in memory or in an environment, wherever it is used.
SSH use cases
SSH suits a person at a workstation.
- Daily code development. Clone, pull and push on your own machine without typing a password or token for each action.
- Biometric approval. A password manager that provides an SSH agent can hold the key and ask for Touch ID or Face ID when a signature is needed, so the key is never a plain file on disk.
- One key per device. Give each laptop or workstation its own key. If a device is lost, delete that one key on GitHub and the others keep working.
- Terminals and IDEs. Any tool that speaks SSH works the same way, with no credential helper to configure and no HTTPS prompt to handle.
SSH is not a fit for a pipeline with no person present, and a key cannot be limited to chosen repositories the way a fine-grained token can, unless it is a per-repository deploy key.
PAT use cases
A PAT suits automation and API calls.
- CI/CD and scripts. A workflow, a build or a deployment job that must clone or push without a person.
- API calls. The GitHub REST and GraphQL APIs take a token, not an SSH signature. Creating an issue, reading a pull request or running
ghfrom a script needs one. - Least privilege. A fine-grained token can be limited to specific repositories and to read-only access, so a leak reaches less than an account-wide key would.
- Time-bound access. A fine-grained token expires after at most a year, which forces periodic replacement. Classic tokens can be created without an expiry, so always set one.
- Strict networks. HTTPS works through most corporate firewalls, and a token authenticates it. GitHub also accepts SSH on port 443 through
ssh.github.com, so a blocked port 22 does not by itself force a token.
A token is a bearer secret. Anyone who reads it can use it elsewhere until it expires or is revoked.
Side by side
| SSH key | Personal access token | |
|---|---|---|
| What is sent | A signature; the private key never moves | The token itself |
| Best for | A person at a machine | Automation, scripts and API calls |
| Scope | Everything your user can reach, unless it is a per-repository deploy key | Chosen repositories and permissions (fine-grained) |
| Expiry | Until you delete it | Set at creation; fine-grained tokens last at most a year |
| Protocol | SSH, port 22 or 443 | HTTPS |
| If copied | Useless without the private key | Usable by anyone, anywhere, until revoked |
| Git operations | Yes | Yes |
| GitHub API calls | No | Yes |
Choosing
Use a signature wherever a person is present, and a token where there is no one to approve.
- Working on your own machine: SSH.
- A pipeline, script or scheduled job: a fine-grained token with an expiry and only the repositories it needs.
- Calling the GitHub API: a token, because SSH cannot.
- A network that blocks port 22: SSH over port 443 first, a token if that is blocked as well.
Common mistakes
- Pasting the wrong value when adding a key. GitHub wants the public key as one line in OpenSSH format, starting with
ssh-ed25519. A fingerprint, a multi-line block or the private key will not work, and the private key must never be pasted anywhere. - Leaving an HTTPS remote in place. An SSH key is ignored when the remote is
https://, and Git asks for a token. Checkgit remote -v. - Using one key for every device. One lost laptop then means replacing the key everywhere.
- Creating a token that never expires. Set an expiry and a repository list every time.