test

Multiple git account on same machine

How to setup and use multiple git account on a the same machine

How To Work With Multiple Github Accounts on a single Machine

Intro

Before starting out, this tutorial is a copy of Rahul Pandey tutorial. It was so useful that i could not risk to loose it and decided to archive it on my own website.

Let suppose I have two github accounts, https://github.com/goyo-office and https://github.com/goyo-personal. Now i want to setup my mac to easily talk to both the github accounts.

NOTE: This logic can be extended to more than two accounts also. :)

The setup can be done in 5 easy steps:

Steps:


Step 1

Create SSH keys for all accounts

First make sure your current directory is your .ssh folder. Or create it

     $ cd ~/.ssh

Syntax for generating unique ssh key for ann account is:

     ssh-keygen -t rsa -C "your-email-address" -f "github-username"

here,

-C stands for comment to help identify your ssh key

-f stands for the file name where your ssh key get saved

After entering the command the terminal will ask for passphrase, leave it empty and proceed.

Now after adding keys , in your .ssh folder, a public key and a private will get generated.

The public key will have an extention .pub and private key will be there without any extention both having same name which you have passed after -f option in the above command. (in my case github-goyo-office and github-goyo-personal)


Step 2 - MAC OS/LINUX ONLY

Add SSH keys to SSH Agent

Now we have the keys but it cannot be used until we add them to the SSH Agent.

     ssh-add -K ~/.ssh/github-goyo-office
     ssh-add -K ~/.ssh/github-goyo-personal

You can read more about adding keys to SSH Agent here.


Step 3

Add SSH public key to the Github

For the next step we need to add our public key (that we have generated in our previous step) and add it to corresponding github accounts.

For doing this we need to:

1. Copy the public key

 We can copy the public key either by opening the github-goyo-office.pub file in vim and then copying the content of it.
     vim ~/.ssh/github-goyo-office.pub
     vim ~/.ssh/github-goyo-personal.pub

OR

We can directly copy the content of the public key file in the clipboard.

     pbcopy < ~/.ssh/github-goyo-office.pub
     pbcopy < ~/.ssh/github-goyo-personal.pub

2. Paste the public key on Github

OR


Step 4

Create a Config File and Make Host Entries

The ~/.ssh/config file allows us specify many config options for SSH.

If config file not already exists then create one (make sure you are in ~/.ssh directory)

     touch config

The commands below opens config in your default editor…Likely TextEdit, VS Code.

     open config

Now we need to add these lines to the file, each block corresponding to each account we created earlier.

     #goyo-office account
     Host github.com-office
          HostName github.com
          User git
          IdentityFile ~/.ssh/github-goyo-office

     #goyo-personal account
     Host github.com-goyo
          HostName github.com
          User git
          IdentityFile ~/.ssh/github-goyo-personal

SSO

If you are using a SSO, configue it with the dedicated section in the same page.

Step 5

Cloning GitHub repositories using different accounts

So we are done with our setups and now its time to see it in action. We will clone a repository using one of the account we have added.

Make a new project folder where you want to clone your repository and go to that directory from your terminal.

You can directly copy the provided link by the hosting service : Usually you would get something generic like this : ssh_demo You will just need to modify the first part of the link with your costomised alias :

git clone git@github.com-{your-account}:GoyoStach/Hub.git

[e.g.] git@github.com-goyo:GoyoStach/Hub.git

Finally

From now on, to ensure that our commits and pushes from each repository on the system uses the correct GitHub user — we will have to configure user.email and user.name in every repository freshly cloned or existing before.

To do this use the following commands.

     git config user.email "my_office_email@gmail.com"
     git config user.name "goyo Office"
     
     git config user.email "my-personal-email@gmail.com"
     git config user.name "goyo Perso"

Pick the correct pair for your repository accordingly.

Now you can use:

     git push
     
     git pull

References

https://gist.github.com/goyoarity/86da20fe3858e6b311de068201d279e3 https://www.theserverside.com/blog/Coffee-Talk-Java-News-Stories-and-Opinions/GitHub-SSH-Windows-Example Tags : #Code #Windows