Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
June 9, 2022 06:11 pm GMT

Using multiple GitHub accounts on one computer

For this article I will use example of two accounts but it could very easily we expanded to multiple accounts.

First step is to create ssh-keys which could be done by the following commands:

ssh-keygen -t ed25519 -C "[email protected]"ssh-keygen -t ed25519 -C "[email protected]"

Note that -C "[email protected]" will just add your email address at the end or the public key. -C option is to add comment at the end of the public key. So it could be anything you like.

Also note -t ed25519 is specifying algorithm used to create the public and private keys. ed25519 or Edwards-curve Digital Signature Algorithm is the 4th generation protocol for ssh key generation. If you don't set this option it will use the RSA algorithm by default to generate the key pairs.

Now you will be prompted with:

Enter file in which to save the key (/home/{username}/.ssh/id_ed25519):

I would recommend you give a personalized name with location like /home/{username}/.ssh/personal and same goes for work one /home/{username}/.ssh/work
In your home directory you will see .ssh folder which should have personal.pub and work.pub which contains your public key and personal,work which contains your private key.

Now you need to start the ssh-agent using, the following command:

eval $(ssh-agent)

Note that we do eval because ssh-agent once outputs bunch of variables that could be set as environment variables. This just makes life easier so its a good practise. You can check this link for more reference.

Now you could add the private keys to the ssh-agent, so that you don't have to put your passphrase every-time you do anything.

ssh-add ~/.ssh/personalssh-add ~/.ssh/work

You will be prompted to enter your passphrase for this. You can check if your keys are added by doing

ssh-add -l

Now you need to add the public keys to your GitHub accounts. This is a good link for reference.
Go to GitHub can click on your profile on top right hand corner and got to settings.
There if you scroll down you will see an option New SSH key. Click that green button and add the appropriate key. You can copy the key by

pbcopy < ~/.ssh/personal.pub

You have copied the contents of the personal.pub file. You can paste this to your GitHub account as new ssh-key. You can give it a Title in GitHub to better remember it.

Now just hit Add SSH key button and finally you have added the public key to your GitHub. Assuming that you have added both the keys to the respective account let's set up the ssh and git configs to work with both the accounts.

Go to .ssh in your home directory. Check if you have an existing config file. If you don't have one create one. You need to create one that looks like

# Default github account: work-accountHost github.work.com   HostName github.work.com   IdentityFile ~/.ssh/work   IdentitiesOnly yes# Other github account: personal-accountHost github.com   HostName github.com   IdentityFile ~/.ssh/personal   IdentitiesOnly yes

Note that the lines starting with # are just comments and does not affect the config. The flag used IdentitiesOnly is used for ssh-agent to point at the specific key provided by IdentityFile.

Now we want to separate our git configs. Here you could choose your style. For me I like to use my work account everywhere except the ~/personal folder. Here I am going to use a minimal .gitconfig to avoid confusion but go fancy with it.

In you home dir if you don't have one already create a .gitconfig file.

[user]    email = [email protected]    name = Your Name[includeIf "gitdir:~/personal/"]    path = ~/personal/.gitconfig-pers

In this file we are setting your default Name, Email and setting a condition that if you are in ~/personal/ it will use load gitconfig from ~/personal/.gitconfig-pers.

Now we have to create the ~/personal/.gitconfig-pers.

[user]    email = [email protected]         name = Your Name

This file will override the email and name when in ~/personal.
Note that the changes will apply to the directories inside the ~/personal/. Which means to check if you config worked create a test folder like so ~/personal/test.
Here if you type

git config --get user.email

You will get your personal email. Except for sub-folders inside ~/personal/ you will get your work email when running the same command.

Enjoy your new setup!


Original Link: https://dev.to/tirthnp/using-multiple-github-accounts-on-one-computer-p26

Share this article:    Share on Facebook
View Full Article

Dev To

An online community for sharing and discovering great ideas, having debates, and making friends

More About this Source Visit Dev To