How to have 2 or more GitHub accounts on one machine (Windows)
Say you have a work and a personal GitHub account. You’re working on one machine and have several projects, some of which you want to push to your personal account or vice versa. This is where you can use SSH keys so that you don’t have to keep re-entering your username and password to identify yourself. What you do is you generate a SSH key. When you do this you get a pair, one being public and the other private. The public one is what you provide to GitHub and the private one stays on your computer. The changes you push will only be allowed when they keys match.
The other day I had such a pain setting this up. I’ve documented these steps so that it’ll be easy for anyone to do it themselves. Note that I did this on a Windows machine lol.
Without further ado, here are the steps to add SSH keys to your GitHub.
1. Navigate to the directory in which you want to push your changes to a different GitHub account.
2. Create a new SSH key in your terminal/command line
For windows I use
ssh-keygen -t rsa -C “your-email-address”
- -t stands for ‘type’ and rsa is type of encryption
- -C is for comment
3. The following will then show:
Generating public/private rsa key pair.
Enter file in which to save the key (/c/Users/your_username/.ssh/id_rsa):
Copy and paste the path followed by an identifiable name for the file:
I named my one test for the sake of this tutorial:
Make sure you don’t override id_rsa as this is your existing key for your original account.
It will then ask you for the following:
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Press ‘Enter’ twice to leave it blank.
You can now type in the following command to see all the SSH keys you have on your local machine:
ls -al ~/.ssh
- ls lists all the files in the current directory
- a is for listing all files including hidden ones
- l is for listing in a long format
You should then get something similar to this:
drwxr-xr-x 1 your_username 1049089 0 Jan 26 10:40 .
drwxr-xr-x 1 your_username 1049089 0 Jan 23 12:12 ..
-rw-r--r-- 1 your_username 1049089 3326 Nov 30 11:21 id_rsa
-rw-r--r-- 1 your_username 1049089 748 Nov 30 11:21 id_rsa.pub
-rw-r--r-- 1 your_username 1049089 1675 Jan 26 10:40 id_rsa_test
-rw-r--r-- 1 your_username 1049089 399 Jan 26 10:40 id_rsa_test.pub
-rw-r--r-- 1 your_username 1049089 803 Nov 30 12:08 known_hosts
You should be able to see your new SSH key file. As you can see in my one I have both id_rsa_test and id_rsa_test.pub.
id_rsa_test is your private key stored on your machine, whilst id_rsa_test.pub is the public key which you will provide GitHub with.
Next you need to copy the SSH key which is stored in id_rsa_test.pub file. You can open this in text editor of your choice. I am currently using atom so I opened the file using the following command:
You will then get something similar to this:
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDEmSbc7ms4SNIf7G0e9EqdrQRTB17VFTqRtCbQ55sSc11xZP5B07UXf9+................a955cf1GUzsNIr60E7VuVxirrr+K2WcleqifnDEg1H/VbyJtEekh4Aav9csBwemTz3 email@example.com
The key is actually longer but I shortened it for the sake of this tutorial.
Copy this and navigate to your GitHub account → Settings → SSH and GPG keys
Click on New SSH key
Give your new key a title and then paste the new generated SSH key you copied before. It is a good idea to name your keys after the machine they’re from. As you can see I have three, Macbook, Windows and Cloud9 IDE.
If you’re on a Mac you should be able to add this key on your terminal as well using the command below:
However because I was using a Windows machine, I had to create a SSH command. You can do this by typing the following directly into the command line:
git config core.sshCommand=ssh -i ~/.ssh/id_rsa_test
or you can do it by opening the git config file in your favourite text editor:
git config -e
Your local git config file should look something like this:
sshCommand = ssh -i ~/.ssh/id_ra_test to the core list.
Note: if you want the command
git config -e to be opening your file in the text editor of your choice, you need to have configured it globally. Mine opened up in Vim as this the default setting. You can override it using the following command:
git config --global core.editor "atom --wait"
Other editor commands can be found here.
3. Set your repository to be pushed to GitHub with the associated account.
You also need to change your user email and name to reflect the GitHub account you want the repo to be associated with. You can change this in the config file whilst you have it open:
Alternatively you can also do this via the command line:
git config user.name "test"git config user.email firstname.lastname@example.org
4. You should now be able to commit and push.
git add .
git commit -m "First commit"
git remote add origin email@example.com:your_username/test.git
git push origin master
Thanks a lot to Samir, James and Tomasz from codebar for helping me with this!
Hope this helps!