Skip to content
Laviyx Learn

Try

Git and GitHub course menu

GitHub SSH Keys and Personal Access Tokens Explained

Sign in to GitHub from the command line. Use the GitHub CLI or Git Credential Manager, create a personal access token, set up an SSH key, and fix authentication errors.

Beginner4 min readUpdated
On this page

GitHub needs to know who you are before it accepts a push, but it no longer accepts your account password for Git commands. There are three common ways to sign in. Pick one. You only need to set it up once on each computer.

MethodBest for
Git Credential ManagerWindows users. It opens a browser sign-in window.
GitHub CLI (gh)Quick setup on any system
SSH keyDevelopers who want a permanent setup without tokens

Option 1: Git Credential Manager#

Git for Windows includes it. The first time you push to an https:// address, a window opens, you sign in through your browser, and your details are stored safely. There is nothing else to do.

Option 2: GitHub CLI#

Install the GitHub CLI, then run:

Terminal
gh auth login

Choose GitHub.com, HTTPS, and sign in with your browser. It configures Git for you. The same tool can also create repositories and pull requests from the terminal.

Option 3: SSH keys#

An SSH key is a pair of files: a private key that stays on your computer, and a public key that you give to GitHub. GitHub checks that you hold the private key without it ever being sent.

Create a key, using your own email:

Terminal
ssh-keygen -t ed25519 -C "you@example.com"

Press Enter to accept the default location. A passphrase is recommended, because it protects the key if someone copies the file.

Print the public key (ending in .pub) and copy it:

Terminal
cat ~/.ssh/id_ed25519.pub

On GitHub, go to Settings, then SSH and GPG keys, then New SSH key, and paste it. Test the connection:

Terminal
ssh -T git@github.com

On the first connection, answer yes to the question about the host. A successful test greets you by username, and says GitHub does not provide shell access. That message is normal.

Now use the SSH form of a repository's address:

Terminal
git clone git@github.com:your-name/notes.git

For a repository that already uses an HTTPS address, switch it:

Terminal
git remote set-url origin git@github.com:your-name/notes.git

Personal access tokens#

A personal access token (PAT) is a password made for a specific purpose, with an expiry date and limited permissions. You need one for scripts, or if a tool asks for a password over HTTPS.

  1. Go to Settings, then Developer settings, then Personal access tokens.
  2. Create a fine-grained token, choose an expiry date, and select only the repositories it needs.
  3. Copy it at once, because GitHub shows it only one time.

When Git asks for a password over HTTPS, paste the token instead.

HTTPS or SSH?#

HTTPSSSH
Addresshttps://github.com/your-name/notes.gitgit@github.com:your-name/notes.git
Signs in withBrowser, tokenKey file
Works through strict firewallsUsuallySometimes blocked

Both are equally good choices. Git behaves the same once you have signed in.

Common errors#

Key takeaway#

Next: Clone, Fetch and Pull.

Finished this lesson?