Adding multiple git account
posted by Anil Kumar Shrestha on 2020-08-25
Generate ssh key
ssh-keygen -t rsa -b 4096 -C "<email address 1>"
// make sure to keep the file name different for different id
// eg: id_rsa_personal
// id_rsa_work
Copy ssh key to respective GitHub/GitLab accounts
cat ~/.ssh/<id file name>.pub
//eg: cat ~/.ssh/id_rsa_personal.pub
Then copy the result and paste to Github -> Settings -> SSH key
Configure
vim ~/.ssh/config
Add the following
Host <host_name1>
HostName github.com
User git
IdentifyFile ~/.ssh/<id file name 1>
Host <host_name2>
HostName github.com
User git
IdentifyFile ~/.ssh/<id file name 2>
Example:
Host github.com
HostName github.com
User git
IdentifyFile ~/.ssh/id_rsa_personal
Host [email protected]
HostName github.com
User git
IdentifyFile ~/.ssh/id_rsa_work
Make gitconfig
vim ~/.gitconfig
Add the following:
[user]
name = <default user>
email = <default email>
[includeIf "gitdir:~/<the folder you will use for work>/"]
path = ~/<the folder you will use for work>/.gitconfig
This basically tells git to use default user if not used grom work folder.
Create gitconfig
vim ~/<the folder you will use for work>/.gitconfig
Then add the following
[user]
name = <work user>
email = <work email>
Add ssh key
ssh-add id_rsa_personal
ssh-add id_rsa_work
Test
ssh -T github.com
ssh -T [email protected]
Now you should be able to make changes from respective accounts in work folder and other folders.