“”
How to use multiple GitHub accounts on the same computer

Using multiple GitHub accounts article header image

View more techs

How to use multiple GitHub accounts on the same computer

 

Matías Mannarino

Matías Mannarino

July 25, 2022

Looking to use multiple GitHub accounts on the same computer?

Look no further: In this post we will provide a step-by-step tutorial on how to do just that.

Let's get started!

SSH Key for every account

1 - First, you need to make sure you have an SSH key for every account. You can follow the steps from this official GitHub tutorial to achieve this: https://docs.github.com/en/authentication/connecting-to-github-with-ssh/adding-a-new-ssh-key-to-your-github-account

2 - Store the keys of every account in your ~/.ssh directory: 

Store account keys in ssh directory 

3 - Ensure the files have the right permissions. You don't want to have insecure keys :)

Issue the following command for each private key:

chmod 400 github_mainAccount
chmod 400 github_secondaryAccount

A gitconfig file to rule them all

1 - Next, you will need to add a general gitconfig file:

touch ~/.gitconfig

2 - We will add these lines to the .gitconfig file to use our mainAccount globally, and our secondaryAccount only in the specificFolder. Of course, you can add as many GitHub accounts and folders as you like:

[include]
   path = ~/.git-mainAccount.conf 
[includeIf "gitdir:~/specificFolder/"]
   path = ~/.git-secondaryAccount  

A gitconfig file for every account

1 - Create a git config file for the main account: 

touch ~/.git-mainAccount.conf

2 - Add these lines to the file:

[user]
   name = yourName
   email = mainAccountEmail@something.com
[core]
 sshCommand = "ssh -i  ~/.ssh/github_mainAccount"

3 - Create a git config file for the secondary account:

touch ~/.git-secondaryAccount.conf

4 - Add these lines to the file:

[user]
   name = yourName
   email = secondaryAccountEmail@something.com
[core]
 sshCommand = "ssh -i  ~/.ssh/github_secondaryAccount"

Add SSH keys to your computer

So, the SSH Agent can take care of the authentication for you, and you don’t have to type in passwords at the terminal. We want to run these lines every time we start a terminal, so we will add them to the startup file.

1 - First, remove old keys:

ssh-add -D

2 - If you're on a Mac, add these to your ~/.zshrc (if you are using zsh) or to your ~/.bashrc (if you are using bash):

ssh-add --apple-use-keychain -q ~/.ssh/github_mainAccount
ssh-add --apple-use-keychain -q ~/.ssh/github_secondaryAccount

Or, if you're using Linux, add:

ssh-add -q ~/.sshgithub_mainAccount
ssh-add -q ~/.sshgithub_secondaryAccount

3 - To check if the ssh keys were added, you can list them:

ssh-add -l

And that’s all!

You can go now to your specificFolder and run:

git config user.name

It will show the name you set up in your secondary account.

Also, you can go to any other folder of your system, run the same command and it will show you the name you set up in your main account.

We hope you've found this article useful. Thanks for reading!

 

Matías Mannarino

Matías Mannarino

Matías is a software engineer with more than seven years of industry experience, currently specializing in the latest trending technologies like ReactJS, React Native, and Node.js. Based in Argentina, Mati is also a big fan of music and records his own songs in his free time.

Contact us

Ready to get started? Use the form below or give us a call to meet our team and discuss your project and business goals.
We can’t wait to meet you!


Follow Us
See our client reviews