How to use different SSH Keys with different GitLab repositories [SOLVED]

Last modified date

Comments: 0

Contents

Problem

Recently I created a new GitLab account for a new project I was working on.

I created a new set of SSH keys and added them to the new GitLab repository. For each separate GitLab account, you need a unique set of keys for each account.

However when I went to got and commit and push my new project to GitLab, I got an access denied error.

The reason for this is that GitLab was using the wrong set of SSH keys to connect to the remote GitLab repository.

Since I already had a default set of SSH keys on my Mac that I use to connect to my first GitLab repository, Git was trying to use that default set of SSH keys instead of the newly generated keys for my new GitLab project.

This is a problem, but there is a very easy way to tell git which set of SSH keys to use depending on which repository you want to commit code to.

Solution

  1. Change directory on your machine into the new git repository that you want to commit to the new GitLab account.
  2. Once you are in the new git repo, run this command, replacing <<NEW PRIVATE KEY FILE NAME>> with the file name of your private SSH key.
git config core.sshCommand "ssh -o IdentitiesOnly=yes -i ~/.ssh/<<NEW PRIVATE KEY FILE NAME>> -F /dev/null"

This command tells git which SSH key it should use for this repository.

  1. Then push your changes to the remote repository and you will see that git is able to successfully push the changes to the correct remote repository.

JakTech

Leave a Reply

Your email address will not be published. Required fields are marked *

Post comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.