Sometimes developers are facing problems with using multiple git accounts. This article will cover how we can use multiple git and GitLab accounts 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 the different users you have to store key at the different files 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
ssh-add -D
Next, you can check that your keys were added:
ssh -T [email protected] ssh -T [email protected] ssh -T [email protected]
If you get a 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 "[email protected]"
Adding Remote path
When you will add your remote path from any project either GitHub or GitLab in your local git project then you must have to follow tricks to make it works with the different user like If you get your remote repository URL like this
[email protected]:niamul/main-site-master.git
Then for the first user of GitLab, you have to replace it with its hostname define in config like
[email protected]:niamul/main-site-master.git
So actually domain will replace with the host like for example, I had replaced “gitlab.com” with my first user hostname “niamul.gitlab.com”. Make sure to use your own username. As a demo, I have used my own user name but you just need to replace it with your own user name. Hope it will help you to use the Use multiple git accounts on your Mac.
Wrapping Up
We hope this article will help you. You can also see our other articles to learn the Easy way to install the Tomcat server on Ubuntu for JAVA Spring.
Create a virtual environment and Install Django
If you like this article please like our Facebook Page to stay connected.