Sometimes developer facing problem on using multiple git account. In this article I will cover how we can use multiple git and gitlab account on a single mac.
Like you have 3 users one is “niamul” and 2nd one is “cssniam” at gitlab.com and 3rd one is at github.com with the following name “niamulrox”.
Now first generate SSK key with the following instruction : https://gitlab.com/help/ssh/README
Make sure when you will generate the SSK key then for diffrent diffrent user you have to store key at different different file like
IdentityFile /Users/Niamul/.ssh/id_rsa IdentityFile /Users/Niamul/.ssh/id_rsa2 IdentityFile /Users/Niamul/.ssh/id_rsa3
After that create a config file at ~/.ssh/ with the following command
cd ~/.ssh/ touch config nano config
And in config just add the following settings
Host niamul.gitlab.com User git Hostname gitlab.com PreferredAuthentications publickey IdentityFile /Users/Niamul/.ssh/id_rsa Host cssniam.gitlab.com User git Hostname gitlab.com PreferredAuthentications publickey IdentityFile /Users/Niamul/.ssh/id_rsa2 Host niamulrox.github.com User git Hostname github.com PreferredAuthentications publickey IdentityFile /Users/Niamul/.ssh/id_rsa3
Now you have to delete cached keys with the following command
Next, you can check that your keys were added:
ssh -T email@example.com ssh -T firstname.lastname@example.org ssh -T email@example.com
f you get success message then you are done with the multiple account SSH Key authentication
Now for every project you have to define your username and user mail with the following command
cd ~/project_location git config user.name "niamul" git config user.email "firstname.lastname@example.org"
Adding Remote path
When you will add your remote path from any project eigther github or gitlab in your local git project then you must have to follow a tricks to make it works with the different different user like If you get your remore repository url like this
Then for the first user of gitlab you have to replace it with its hostname define in config like
So actually domain will replace with the host like for the example I had replace “gitlab.com” with my first user host name “niamul.gitlab.com”. Make sure to use your own username. As demo I have used my own user name but you must need to replace with your own user name. Hope it will help you to use Use multiple git account on your mac.