BackgroundHaving 2 different gitlab accounts one is for company and the other one is personal, I would like both work independently on the machine at home, pull source code separately with their corresponding username and repository.
Problems
Very often I encountered a situation with permission denied in my company account while listing the remote repository of do anything remote (like git pull), but I can only make my personal account work without success on my company account. Turns out git client is wrongly picking the my personal account ssh information to login to my working account causing the "permission right" error.
Solutions
1. Refer to ~/.ssh/config and update the config file, add 1 more ssh entry to my company gitlab account as follows
Host anywhere.gitlab.com
PubkeyAcceptedKeyTypes +ssh-rsa
HostName gitlab.com
IdentitiesOnly yes
IdentityFile ~/.ssh/id_rsa2
User ihmcjacky
Host maxwellhk.gitlab.com
PubkeyAcceptedKeyTypes +ssh-rsa
HostName gitlab.com
IdentitiesOnly yes
IdentityFile ~/.ssh/id_rsa
User maxwellhk
Here, we have 2 users, 1 is ihmcjacky for my company usage, another is for my private usage, HostName parameter both specified as gitlab.com, but the Host which is very important, it acts as an alias for git client to identify which configuration it should refer to when trying to login to gitlab. Name anything familiar to you. In my case anywhere.gitlab.com and maxwellhk.gitlab.com.
2. Go to the desired project (in my case, company repository), navigate to .git folder and open config, configure the username and user email manually so as to let git client not to use globally configured settings for log in. Also in remote-origin url settings, configure as follows
url = git@anywhere.gitlab.com:p2Firmware/p2_controller.git
The anywhere.gitlab.com here is important, it is the alias we specified in ~/.ssh/config.
For safety, we can follow References > Multiple SSH keys for different accounts on Github or Gitlab, go through the processes, delete the cached keys and add back the ssh entries and check the ssh connection
3. Finally, add back the new origin URL to the project by using git remote set-url origin new.git.url/here
References