Git Multiple SSH Keys
Situation
Recently I joined a company which uses GitLab. Up until now, my professional and personal projects were being hosted on GitHub. That allowed me to just work with one SSH key for both kind of projects.
I setup 2 accounts and planned to use Git-User-Switch to switch between both users.
Problem
When I tried to push a commit, I got error stating that I don’t have access. I immediately knew that this must has to do with proper ssh key not being used. Before this, I did not know how to see which ssh key git is using.
A quick google gave away an answer. All I had to do is set env variable which will overwrite git ssh default command.
1 |
|
I ran git push command again knowing that it will fail but aiming to look at the logs.
1 |
|
From the logs, I figured out that git was using id_ed25519 (intended for GitLab) key instead of personal (intended for GitHub).
Solution
Next step was to configurer ssh key. I added following content to ~/.ssh/config
file
1 |
|
This config make sure that whenever ssh is trying to connect to github.com, ~/.ssh/personal key will be used.