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
export GIT_SSH_COMMAND="ssh -v"

I ran git push command again knowing that it will fail but aiming to look at the logs.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
debug1: /etc/ssh/ssh_config line 47: Applying options for \*

# other lines

debug1: Will attempt key: /Users/nirav.patel/.ssh/id_ed25519 ED25519

# other lines

debug1: No more authentication methods to try.
[email protected]: Permission denied (publickey).
fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.

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
2
3
Host github.com
  HostName github.com
  IdentityFile ~/.ssh/personal

This config make sure that whenever ssh is trying to connect to github.com, ~/.ssh/personal key will be used.