TechEarl

How to Add an SSH Key to GitHub and GitLab

Paste your SSH public key into GitHub or GitLab, then confirm it works with ssh -T. The whole flow, with no password or token prompts on git push after.

Ishan Karunaratne⏱️ 4 min readUpdated
Share thisCopied
Add your SSH public key to GitHub or GitLab and verify it with ssh -T so git clone, pull and push stop asking for credentials.

Adding an SSH key to GitHub or GitLab is the same idea as adding it to a server: the public half goes on their side, the private half stays on yours. The only difference is you paste it into a settings page instead of running ssh-copy-id. If you are still getting started with Git, this is the one-time setup that stops every clone, pull, and push from asking for credentials.

Copy your public key

You need an SSH key first. Copy the public half to the clipboard:

Try it with your own values

Pick your OS to get the right clipboard command.

bash· Linux (GNU)
xclip -sel clip < ~/.ssh/id_ed25519.pub   # or: wl-copy < ~/.ssh/id_ed25519.pub

If those tools are not installed, just cat ~/.ssh/id_ed25519.pub and copy the single line by hand. It starts with ssh-ed25519 and ends with your comment.

Paste it into the web UI

  • GitHub: Settings, then SSH and GPG keys, then New SSH key. Give it a title (the machine name is useful), leave the type as Authentication Key, paste, and save.
  • GitLab: Preferences, then SSH Keys, then Add new key. Paste, optionally set an expiry, and save.

You are pasting the contents of the .pub file. Never paste the private key (the file with no extension).

Test it from the terminal

This is the step people skip and then wonder why git push still fails:

bash
# GitHub
ssh -T git@github.com

# GitLab
ssh -T git@gitlab.com
Pasting an SSH public key into GitHub's New SSH key form
GitHub Settings, SSH and GPG keys, New SSH key: give it a title and paste the public key.
An SSH key listed in a GitHub account with its fingerprint
Once added, the key shows its fingerprint and access level. (This demo key has been deleted.)

GitHub replies Hi USERNAME! You've successfully authenticated, but GitHub does not provide shell access. That sentence is success, not an error. GitLab says Welcome to GitLab, @USERNAME!. The first time you connect you will be asked to trust the host key; type yes. If instead ssh -T fails to authenticate, work through Permission denied (publickey) before touching anything else.

Use SSH for the remote, not HTTPS

A key only helps if the repo uses the SSH remote URL (git@...), not HTTPS. If you are weighing using an SSH remote instead of HTTPS, the short version is that SSH leans on the key you just added so nothing prompts you again:

bash
# Check the current remote
git remote -v

# Switch an existing repo from HTTPS to SSH
git remote set-url origin git@github.com:owner/repo.git

Clone with the SSH URL from the start (the green Code button, SSH tab) and git clone, pull, and push use the key automatically with no prompts.

FAQ

See also

Sources

Authoritative references this article was fact-checked against.

TagsSSHGitHubGitLabgitssh-keygenDevOps

Found this useful? Pass it on.

Copied

Ishan Karunaratne

Software Systems Architect · Senior Software Engineer · Engineering Leadership

Software systems architect and senior software engineer with more than two decades designing, building, and running production software, Linux systems, and DevOps infrastructure, and lately working AI into the stack. Now a CTO, though what I write here is drawn from the full arc of that work, across architecture, engineering, and operations, not any single job.

Keep reading

Related posts

Create an EBS volume with aws ec2 create-volume, attach it to a running EC2 instance, format with mkfs.ext4 or mkfs.xfs, mount it, and persist across reboots with a UUID-based /etc/fstab entry. Console, AWS CLI, and Terraform walkthroughs.

How to Add an EBS Volume to an EC2 Instance

Create an EBS volume, attach it to a running EC2 instance, format and mount it, and survive reboots with a UUID-based fstab entry. Console, AWS CLI, and Terraform walkthroughs plus the Nitro device-naming gotcha that trips everyone.

Generate an SSH key with ssh-keygen: pick Ed25519 over RSA, decide on a passphrase, and set the ~/.ssh permissions SSH needs. Linux, macOS and Windows.

How to Create an SSH Key in 2026

Create an SSH key in one ssh-keygen command. Which key type to pick in 2026 (Ed25519 vs RSA), whether to set a passphrase, and the file permissions SSH needs, on Linux, macOS and Windows.