Setup Multiple GitHub Accounts from a Single Machine

Setting up a second GitHub Account

Assuming that you already a git account setup on a machine and you are looking to set up a second one to help you manage your work and personal git accounts from a single machine.
  • You might be already aware that you git SSH key is stored in a .ssh folder under your home directory. Navigate to this directory and type "ls" to check this.
cd ~/.ssh
ls
  • Next, we need to create a new SSH key using your second git email address and give it a name other than the default "id_rsa", as by this name you will already have your first git account setup. In my case, I am naming it as "id_rsa_personal"
ssh-keygen -t rsa -C "automationbynishant@gmail.com" -f "id_rsa_personal"
  • Again type "ls" and check if the new RSA public and the private key are created.
ls


  • Run eval on ssh-agent which will check if ssh-agent is running and will start it if its not already running. Next, add this new SSH key to your ssh-agent. 

eval "$(ssh-agent -s)"
ssh-add ~/.ssh/id_rsa_personal

  • Get the public SSH key from the generated .pub file. You can use "cat" to get it and copy it to your clipboard\text file. Now we need to add this new SSH key to your GitHub account.
cat ~/.ssh/id_rsa_personal.pub
  • Open github.com in a browser and log in. Click on the profile photo on the right and navigate to Settings -> New SSH Key -> Add SSH Key  
  • In the Title field give any name and paste the SSH key you copied earlier in the Key textbox

  • Click Add SSH Key to save this new key to your GitHub account.
  • Now we need to create a config file which will tell Git how to differentiate between the two Git Hub accounts and map them to the corresponding RSA file. We will need to give a personalized HostName for the new Git account. In my case, I will go with personal.github.com, you can choose as you like, but remember this as you need it for all actions on your second account.
 sudo nano ~/.ssh/config


Here is the content for you to copy paste.
# Work account, - the default config
Host github.com
   HostName github.com
   User git
   IdentityFile ~/.ssh/id_rsa

# Personal account
Host personal.github.com 
   HostName github.com
   User git
   IdentityFile ~/.ssh/id_rsa_personal
  • Click Ctrl+X followed by "Y" to save the config file. I am using nano to edit the file, you may choose the editor you are comfortable with.
  • Now that the setup is pretty much done, you might want to clone your personal repository to your local. For this, go to the folder you wish to clone your git repo and run the git clone command, but now instead of using the usual git@github.com you need to use git@your-customized-hostname which in my case was personal.github.com 
git clone --recursive git@personal.github.com:git-account-name/git-repo-name.git
  • If you remember, you would have set your global user.name and user.email while setting up your first Git account using git config --global user.name "YOUR GIT USERNAME" and git config --global user.email "YOUR EMAIL ADDRESS”. But for this new repo, it has to be your second git repo username and email and so we will set up a git repo specific user.name and user.email. For this, cd to the git repo folder and set your repo specific user.name and user.email by using the below commands. 
git config user.name "YOUR SECOND GIT USERNAME"
git config user.email "YOUR SECOND GIT EMAIL ADDRESS"
  • Optional: If you need to add a remote you need to again use git@your-customized-hostname instead ofgit@github.com
git remote add your-name git@personal.github.com:git-account-name/git-repo-name.git

  • Viola!!! Your setup is done. Now you can start committing to both your git accounts.











Comments

  1. Thank you for documenting these steps, this is a very useful content.

    ReplyDelete

Post a Comment

Popular posts from this blog

Get rid of "Timed out receiving message from renderer"

Setup Selenium Grid using Kubernetes

Launching Selenium Grid and Registering a Web Node