Setting up two git accounts on local PC

top-view-lock-with-key-copy-space_23-2148578105

The main purpose of setting up two or more accounts on single PC is to establish SSO (Single Sign On) between local git repository and the remote git repository to avoid the need for entering password for each git operation on remote server.

Generating SSH keys on Local PC

List all the SSH keys generated on the local PC;

$ls -la ~/.ssh

By default id_rsa and id_rsa.pub will be the keys used in ssh communication unless it is changed over the ~/.ssh/config file. And the default ssh key-pair can be generated with following command;

$ssh-keygen -t rsa -C "<email>"

t – key type
c – comment

E.g.:-
$ssh-keygen -t rsa -C "amie@mail.com"

When executing the above command following questions will be popped expecting answers or leave blank for defaults.

File name – Enter; to generate the key on the default location which is ~/.ssh
Passphrase – Enter; if additional passcode is not needed on each remote interraction, else enter the passcode needed
Re-enter Passphrase – Enter, if no passcode used, else re-enter the passcode

This will generate public id_rsa.pub and private id_rsa keys.

For the second account, generate another key and define the key file name with -f param;

$ssh-keygen -t rsa -C "<email>" -f  "<file name>"

f – file name to store the keys

E.g.:-
$ssh-keygen -t rsa -C "amie.wills@mail.com" -f "id_rsa_work_account"

Update SSH Config on Local PC

Each ssh key file has to be defined with ~/.ssh/config file in order to get them consumed over respective ssh operations.

To view the current content of the file use the following command.

$vi ~/.ssh/config

And if the file does not exist, create the above file and the file format can be defiled as below;

#Default account
Host <host as a profile>

HostName <exact host name to match>

User <registered user account>

IdentityFile <private key file>



Host
– Remote repository name can use as a profile name
HostName – Remote repository Host name
User – Registered user account name
IdentityFile – SSH private key file

E.g.:-

#Default account, - the default config
Host github.com
HostName github.com

User amie

IdentityFile ~/.ssh/id_rsa

#Second account
Host github.work.com

HostName github.work.com

User amile.wills

IdentityFile ~/.ssh/id_rsa_work_account

Adding SSH keys to the Git accounts

Copy the public key content and update it on the designated SSH Keys section within the respective remote repository.

Git“CPG and SSH Keys”
Bitbucket“SSH Keys”

Register SSH keys with ssh-agent on local PC

Following command register the ssh key with the SSH agent running on the local PC;

$ssh-add <private key file>

E.g.:-
$ssh-add ~/.ssh/id_rsa

$ssh-add ~/.ssh/id_rsa_work_account

Make sure the ssh agent is running. If not start the agent with following command and try adding ssh private keys again;

$eval $(ssh-agent)

Configure commit identity information

To update username and email related to commit specific to a project the following approach can be used;

$git config user.name "amie"
$git config user.email "amie@email.com"

To define the identity information globally applicable for all the repositories configured on the local PC;

$git config --global user.name "amie"
$git config -global user.email "amie@email.com"

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s