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.
- Change directory on your machine into the new git repository that you want to commit to the new GitLab account.
- 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.
- 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.