HOW TO MANAGE MULTIPLE GITHUB ACCOUNTS USING GIT BASH AND SSH
Published: 2024-02-21 20:00:19

**INSTALL GIT ON YOUR SYSTEM**
Go to your web browser and then install git using the link below and follow the process.
On installing it will also install git bash
https://gitforwindows.org/

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0fwupu2fmhmwc00ui3ui.png)
Go to your command for those using Macbook and the windows key for those using windows and type git bash and then open it 

**Generating the SSH Keys**
so before you generate a new SSH keys, you have to check if there is an already available SSH keys which you may have installed a long time ago by running this line of code 

> ls -al ~/.ssh

After running the command it will show the list of available private and public keys as shown in the image below


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/83g4huzwf8ql98xof3hp.png)

if not available, then you can run this command 

> ssh-keygen -t rsa

when asked for the location to save the keys accept the default location by pressing the enter key.

By default the git bash recognizes your personal Github account, you can check that by running the command 

> git config user.name  
> git config user.email , to check the username and email .

After generating the SSH keys for your personal github account, you can do the same for the work account or any other account using the command line below

> ssh-keygen -t rsa -C "email@work_mail.com" -f "id_rsa_work_user1"

so the "email@work_mail.com" is your work email, the one you used to create the work github account or whatever email you used.
and "id_rsa_work_user1" is your username on github, so it distinguishes between the personal and the work SSH keys

So by now we have two different SSH keys
~/.ssh/id_rsa
~/.ssh/id_rsa_work_user1

**Adding the new corresponding SSH key to your Github account
Copy the public key pbcopy < ~/.ssh/id_rsa.pub and then log in to your personal GitHub account:**

To see the key on git bash, enter this command 
> cat ~/.ssh/id_rsa.pub
> cat ~/.ssh/id_rsa_work_user1.pub
N/B: id_rsa.pub is the name for your personal SSH key and >id_rsa_work_user1  is name for your work SSH key or the name you used to save it, if it is your username, that's how it will appear

a. Go to Settings
b. Select SSH and GPG keys from the menu to the left.
c. Click on New SSH key, provide a suitable title, and paste the key in the box below
d. Click Add key — and you’re done!


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wlipfxvx69h1hc6ms7cs.png)

Then do the same thing for your work Github account, if successful github will show a prompt message that you have successfully added your SSH key.

**Registering the new SSH Keys with the ssh-agent**
Ensure the ssh-agent is running by inputting this command
eval "$(ssh-agent -s)", and will show something like this "Agent pid 1280"

Then add the ssh-agent like this : 
ssh-add ~/.ssh/id_rsa
ssh-add ~/.ssh/id_rsa_work_user1

if successfully added it will show something like 
identity added : path to where it was added 

To check the list of SSH keys in your computer, you can run the command 
> ssh-add -l
And it will show the list of keys

**Creating the SSH config File**

$ cd ~/.ssh/
$ touch config           // Creates the file if not exists
$ code config            // Opens the file in VS code, use any editor

so this creates a mark down file in vs code then you input this values 
N/B: make sure that there is no comment else there will be an error

Host personalemail@gmail.com
HostName personalemail@gmail.com
User github username
IdentityFile ~/.ssh/id_rsa

Host workemail@gmail.com
HostName workemail@gmail.com
User github username
IdentityFIle ~/.ssh/id_rsa_work_user1

**One active SSH key in the SSH agent at a time **
so as I mentioned above you can use **ssh-add -l** to see the list of SSH key in your system or computer. Then you can use **ssh-add -D** to delete all the keys, then you can add them one by one by using this command in accordance with the github account you want to use at the moment be it personal or work 
for personal **ssh-add ~/.ssh/id_rsa**
for work **ssh-add ~/.ssh/id_rsa_work_user1**
N/B: "~/.ssh/id_rsa" this is the path it was stored in your computer

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9nsxkpdi4qduakv3hdt5.png)

The ssh-agent now has the key mapped with the personal GitHub account, and we can do a Git push to the personal repository.

To push to your work GitHub account, change the SSH key mapped with the ssh-agent by removing the existing key and adding the SSH key mapped with the GitHub work account.

**Setting the git remote Url for the local repositories**
Once we have local Git repositories cloned /created, ensure the Git config user name and email is exactly what you want. GitHub identifies the author of any commit from the email id attached with the commit description.

To list the config name and email in the local Git directory, do git config user.name and git config user.email. If it’s not found, update accordingly.

> git config user.name "User 1"   // Updates git config user name
> git config user.email "user1@workMail.com"
> git config --globe user.name "User1"
> git config --globe user.email "user1@workMail.com"
N/B: This will add the username and email to the globe

**While Cloning Repositories**
Note: The next step will help, if we have the repository already available on local.

Now that the configurations are in place, we can go ahead and clone the corresponding repositories. On cloning, make a note that we use the host names that we used in the SSH config.

Repositories can be cloned using the clone command Git provides:

git clone git@github.com:personal_account_name/repo_name.git
The work repository will require a change to be made with this command:


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/p9uth7sapztrcuow1xu0.png)

git clone git@github.com-work_user1:work_user1/repo_name.git
This change is made depending on the host name defined in the SSH config. The string between @ and : should match what we have given in the SSH config file.

so for an example I was able to clone the repo of one of my favorite developers **JONAS SCHMEDTMANN** by using the steps stated above 


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/koq5c5yaegghiq938ixk.png)


**For Locally Existing Repositories**
If we have the repository already cloned:

List the Git remote of the repository, git remote -v

Check whether the URL matches our GitHub host to be used, or else update the remote origin URL.

git remote set-url origin git@github.com-worker_user1:worker_user1/repo_name.git
Ensure the string between @ and : matches the Host we have given in the SSH config.

If you are creating a new repository on local:

Initialize Git in the project folder git init.

Create the new repository in the GitHub account and then add it as the Git remote to the local repository.

git remote add origin git@github.com-work_user1:work_user1/repo_name.git 
Ensure the string between @ and : matches the Host we have given in the SSH config.

Push the initial commit to the GitHub repository:

git add .
git commit -m "Initial commit"
git push -u origin master

if this article was helpful please share and comment your thoughts on it.

Reference from **Bivil M Jacob** from his freecodecamp post, and  **Jonas Schmedtmann** from his complete javascript repo