How to Work With GitHub and Multiple Accounts
GitHub provides two ways of connecting to git repositories, namely SSH and HTTPS. HTTPS requires you to supply an access token every time you push to a repository. SSH allows you to push code without remembering your username and token every time you push code to a GitHub repository.
So you have a personal GitHub account—everything is working perfectly. But then, you get a new job, and you now need to be able to push and pull to multiple accounts. How do you do that? I'll show you how!
1. Create a New SSH Key
We need to generate a unique SSH key for our second GitHub account.
1 |
ssh-keygen -t rsa -b 4096 -C "your-email-address" |
Replace the placeholder with your email address.
1 |
Generating public/private rsa key pair. |
2 |
Enter file in which to save the key (/home/vaati/.ssh/id_rsa): |
Be careful that you don't overwrite your existing key for your default personal account. Instead, when prompted, save the file as id_rsa_COMPANY. In my case, I've saved the file to ~/.ssh/id_rsa_work.
1 |
Enter file in which to save the key (/home/vaati/.ssh/id_rsa): /home/vaati/.ssh/id_rsa_work |
After you run the command above, you will get a prompt to enter a passphrase.
1 |
Enter passphrase (empty for no passphrase): |
A passphrase should be easy to remember but hard for a bot or computer to guess. You could use a favorite line of poetry, for example, or a line from a movie that you like.
You will be prompted to enter the passphrase again.
1 |
Enter same passphrase again: |
Once you enter the passphrase again, the key is saved in the default location you specified, and two files are created as shown below.
1 |
Your identification has been saved in /home/vaati/.ssh/id_rsa_work
|
2 |
Your public key has been saved in /home/vaati/.ssh/id_rsa_work.pub
|
3 |
The key fingerprint is: |
4 |
SHA256:PLusNPF6nf5e2jr/z57EB1gl5YW43Tw55TPqhTu4z3g dummy8@gmail.com |
5 |
The key's randomart image is:
|
6 |
+---[RSA 4096]----+
|
7 |
| ..o+|
|
8 |
| . .+o|
|
9 |
| o.++|
|
10 |
| . .o.*+|
|
11 |
| . S . + =|
|
12 |
| o o o.o |
|
13 |
| o o. .o +o.|
|
14 |
| . +..o.+E..o|
|
15 |
| ooo..=O*++*|
|
16 |
+----[SHA256]-----+
|
One is a public key, and the other is a private key. You will need the key to make a connection with your GitHub account. To view the contents of the saved key, issue the following command.
1 |
cat .ssh/id_rsa_work.pub
|
Copy and store the key on a text file on your computer, as we will need to add it to your second GitHub account.
2. Attach the New Key
Next, log in to your second GitHub account, click on the drop-down next to the profile picture at the top right, select Settings, and click on SSH and GPG keys.



Next, add the key you copied earlier. Feel free to give it any title you wish.



3. Add the SSH Key to the Agent
Next, because we saved our key with a unique name, we need to tell SSH about it. In the Terminal, type: ssh-add ~/.ssh/id_rsa_work
. If successful, you'll see a response of Identity Added.
1 |
$ ssh-add ~/.ssh/id_rsa_work
|
2 |
Enter passphrase for /home/vaati/.ssh/id_rsa_work:
|
3 |
Identity added: /home/vaati/.ssh/id_rsa_work (dummy85@gmail.com) |
We now have two sets of SSH keys in our machine. They reside in the .ssh directory, and you can view them.
1 |
vaati@vaati-Yoga-9-14ITL5:~$ cd .ssh |
2 |
vaati@vaati-Yoga-9-14ITL5:~/.ssh$ ls |
3 |
config id_rsa id_rsa.pub id_rsa_work id_rsa_work.pub known_hosts known_hosts.old |
4. Create a Config File
We've done the bulk of the workload, but now we need a way to specify when we wish to push to our personal account and when we should instead push to our company account. To do so, let's create a config file.
1 |
touch ~/.ssh/config
|
2 |
vim config |
If you're not comfortable with Vim, feel free to open the file with any editor of your choice. Paste in the following snippet.
1 |
#Default GitHub
|
2 |
Host github.com |
3 |
HostName github.com |
4 |
User git |
5 |
IdentityFile ~/.ssh/id_rsa |
This is the default setup for pushing to our personal GitHub account. Notice that we're able to attach an identity file to the host. Let's add another one for the company account. Directly below the code above, add:
1 |
Host github.com-work |
2 |
HostName github.com |
3 |
User git |
4 |
IdentityFile ~/.ssh/id_rsa_work |
The config file should now look like this:
1 |
#Default GitHub
|
2 |
Host github.com |
3 |
HostName github.com |
4 |
User git |
5 |
IdentityFile ~/.ssh/id_rsa |
6 |
|
7 |
Host github.com-work |
8 |
HostName github.com |
9 |
User git |
10 |
IdentityFile ~/.ssh/id_rsa_work |
This time, rather than setting the host to github.com
, we've named it github.com-work
. The difference is that we're now attaching the new identity file that we created previously: id_rsa_work.
Save the page and exit.
5. Try It Out!
It's time to see if our efforts were successful. Create a test directory, and add a test.txt file.
1 |
mkdir TEST
|
2 |
cd TEST
|
3 |
touch test.txt
|
Initialize git and create the first commit.
1 |
git init |
2 |
git add .
|
3 |
git commit -m "first commit' |
Log in to your company account, create a new repository, and give it the name test_repository. Once the new repository is created, GitHub provides you with some next steps, as shown below.



Since we already have an existing repository on the command line, we only need to add and push changes. This time, rather than pushing to git@github.com
, we're using the custom host we created in the config file: git@github.com-work
.
Use
1 |
git@github.com-work |
Instead of
1 |
git@github.com |
Let's push the changes.
1 |
git remote add origin git@github.com-work:kaththy/Test.git |
2 |
git push origin main |
3 |
Enumerating objects: 3, done. |
4 |
Counting objects: 100% (3/3), done. |
5 |
Delta compression using up to 8 threads |
6 |
Compressing objects: 100% (2/2), done. |
7 |
Writing objects: 100% (3/3), 220 bytes | 220.00 KiB/s, done. |
8 |
Total 3 (delta 0), reused 0 (delta 0), pack-reused 0 |
9 |
To github.com-work:kaththy/Test.git |
10 |
* [new branch] main -> main |
11 |
Branch 'main' set up to track remote branch 'main' from 'origin' |
Return to GitHub, and you should now see your repository. Remember:
- When pushing to your personal account, proceed as you always have.
- For your company account, make sure that you use
git@github.com-COMPANY
as the host.
Conclusion
This guide has covered how to work with multiple accounts using SSH. It has also covered how to push code to various GitHub accounts from the same machine.
This post has been updated with contributions from Esther Vaati. Esther is a software developer and writer for Envato Tuts+.