How to Deal with Multiple GitHub Accounts on One Computer


If you have more than one GitHub account then you will face some issues when you start pushing files from your local repository "directory" to remote GitHub repository specially when using SSH URL.

Assuem the following

SSH vs. HTTP URLS

You can define a remote URLs as SSH or HTTP

Read more about changing remote urls


( 1 ) Push to Github Repository Using HTTP URL

  1. Now on your PC open Git Bash change the current path to the local repository e.g. ~/hanimk-git/test
  2. Excute the following git commands
> touch readme.md
> git init
> git add . 
> git commit -m "first commit"
> git remote add origin https://github.com/hanimk/test.git
> git push -u origin master
  1. Pass GitHub username/password
  2. Now check that the readme.md file has been pushed to the remote repository https://github.com/hanimk/test

( 2 ) Push to Github Repository Using SSH URL

GitHub account hmkcode is the default git account which already has an SSH Key genrated and added to github account. Read more about generating ssh key

Now we need to generated a scond SSH key for the new github hanimk account.

1. Create a New SSH Key

ssh-keygen -t rsa -C "second-account-email-address"

2. Attach the New Key

Add SSH key to GitHub

3. Then add your new key to the ssh-agent

> eval '$(ssh-agent)'
# Agent pid 59566
> ssh-add ~/.ssh/id_rsa_hanimk

4. Create a Config File

touch ~/.ssh/config
Host github-hanimk
HostName github.com
User git
IdentityFile ~/.ssh/id_rsa_hanimk

5. Add Remote SSH URL and Push

> git remote add origin-ssh git@github-hanimk:hanimk/test.git
> git add .
> git commit -m "3rd commit"
> git push -u origin-ssh master