Working with different Github account on the same PC
STEP 1: Generating a new SSH key
Generate the SSH key by following this link.
STEP 2: Adding your SSH key to the ssh-agent
Add the generated SSH key to the ssh-agent by following the GitHub document
STEP 3: Adding a new SSH key to your GitHub account
To do that, follow this doc
Adding the second account SSH keys
By following the steps below again, set up a second account on the same PC
- Generate the second SSH key in your PC by following STEP 1 and save the file name different from the previous one
- Add SSH key to the ssh-agent by following the STEP 2
- Add the new SSH key to your account by following STEP 3
Configuring the SSH for two different accounts
Run the below comments in the terminal if you're using Linux based system. If you are using Windows, run it in Git base.
- Navigate to the ssh folder by running the below command
cd ~/.ssh ls
2. list the files available in the ssh folder by the below command
ls
let assume that it has two key pairs like below,
id_encryption_personal
id_encryption_personal.pub
id_encryption_work
id_encryption_work.pub
3. Create the config
file by running the below command or you can create it from GUI
touch config
open it in your editor and add the following line to it
Host github.com
HostName github.com
User git
PreferredAuthentications publickey
IdentityFile ~/.ssh/id_encryption_personalHost alias-for-your-second-account
HostName github.com
User git
PreferredAuthentications publickey
IdentityFile ~/.ssh/id_encryption_work
In the above config, the Host is up to you. you can create it on your own’s.
Testing your SSH connection
Test the above configuration from your Terminal/Git Bash by the following steps
ssh -T git@github.com
I am using the host as github.com here because I am keeping it as github.com in the config file seen above. Make it as you created
Testing the second account,
ssh -T git@alias-for-your-second account
Here I am mentioning the host as alias-for-your-second account
because i have created like this in the config file seen above
Cloning repo from the remote
cloning it from the first account
Go to your repository and copy the SSH URL to clone the repo. It will look like below
git@github.com:user_name/repo_name.git
To clone the repo run the below command from the directory where you have to clone the repo
git clone git#github.com:user_name/repo_name.git
Here, I am not done any changes in the URL copied from the git, Because I am having the Host for the first account is github.com.
If you have changed the Host in the config for the first account. Replace the github.com as you mentioned the host in the config file
Cloning it from the second account
Go to your second account’s repository and copy the SSH URL to clone the repo. It will look like below
git@github.com:user_name/repo_name.git
To clone the repo run the below command from the directory where you have to clone the repo
git clone git#alias-for-your-second-account:user_name/repo_name.git
Here, I have changed the URL
URL copied from the git is
git@github.com:user_name/repo_name.git
URL used to clone is
git#alias-for-your-second-account:user_name/repo_name.git
Because I have changed the host in the config file as alias-for-your-second-account. like below
Host alias-for-your-second-account
HostName github.com
User git
PreferredAuthentications publickey
IdentityFile ~/.ssh/id_encryption_work
Cloning without changing the URL
you can clone the repo without changing the URL. For that should have the config as below
Host github.com
HostName github.com
User git
IdentitiesOnly yes
Match Host github.com exec "pwd | grep '/organization' > /dev/null"
IdentityFile ~/.ssh/id_encryption_work
Match Host github.com !exec "pwd | grep '/organization' > /dev/null"
IdentityFile ~/.ssh/id_encryption_personal
Above config is,
If you are cloning the repo to the directory organization. It will use the Identity file as ~/.ssh/id_encryption_work
If you are cloning the repo to the directory other than the organization folder. It will use Identify file as ~/.ssh/id_encryption_personal