Using SSH Keys with Git Services#

This page explains how to use SSH keys with Git services such as Surrey GitLab, GitHub and GitLab.com.

It is aimed at users who:

  • use Git over SSH rather than HTTPS

  • use more than one Git service, for example Surrey GitLab and GitHub

  • see permission errors even though ssh -T appears to authenticate successfully

  • want to understand the difference between SSH authentication and Git commit identity

For most users, the setup is simple: add your public key to the Git service, make sure your repository uses an SSH remote URL, and set your Git name and email address.

SSH authentication and Git identity are separate#

There are two related but different pieces of configuration:

SSH authentication:

Determines whether you are allowed to fetch from or push to a Git service. This is controlled by your SSH key and ~/.ssh/config.

Git commit identity:

Determines the name and email address recorded in your commits. This is controlled by Git configuration, usually ~/.gitconfig or repository-specific Git config.

For example, SSH might allow you to push to a repository, but your commits could still use the wrong email address if Git itself is not configured correctly.

Configure SSH for Git services#

If you use more than one Git service, it is useful to configure ~/.ssh/config so each service has a clear entry.

Example ~/.ssh/config:

Host surrey-gitlab
    HostName gitlab.surrey.ac.uk
    User git
    IdentityFile ~/.ssh/id_ed25519_surrey_gitlab
    IdentitiesOnly yes

Host github.com
    HostName github.com
    User git
    IdentityFile ~/.ssh/id_ed25519_github
    IdentitiesOnly yes

Host gitlab.com
    HostName gitlab.com
    User git
    IdentityFile ~/.ssh/id_ed25519_gitlab_com
    IdentitiesOnly yes

IdentitiesOnly yes tells SSH to use the key specified by IdentityFile rather than trying several keys from your SSH agent.

Notes for Windows users

Test authentication#

Run the relevant test command:

ssh -T git@surrey-gitlab
ssh -T git@github.com
ssh -T git@gitlab.com

GitHub and GitLab.com may tell you that authentication succeeded but shell access is not provided. That is normal for Git hosting services. They allow Git operations over SSH, but not interactive shell login.

Use SSH remote URLs#

SSH Git remote URLs look like this:

git@surrey-gitlab:group/project.git
git@github.com:organisation/repository.git
git@gitlab.com:group/project.git

HTTPS remote URLs look like this:

https://github.com/organisation/repository.git

If you want to use SSH keys, make sure your repository remote uses an SSH URL.

To check the remote URL for an existing repository, first change into the repository directory, then run:

cd path/to/your/repository
git remote -v

To change the remote URL:

git remote set-url origin git@surrey-gitlab:group/project.git

Set your Git commit identity#

SSH keys determine whether you are allowed to fetch or push. They do not determine the author name and email address in your commits.

Set your identity globally:

git config --global user.name "Your Name"
git config --global user.email "your.name@surrey.ac.uk"

Check your current identity:

git config --get user.name
git config --get user.email

For many users, a single global Git identity is sufficient.

Set identity for one repository only
Advanced: directory-based Git identities
Optional: global ignore file
Advanced: one repository with more than one remote
Troubleshooting