How to Use Multiple GitHub Accounts on the Same Computer
Posted on Oct 23, 2021 by Nate in UncategorizedLike many of you, I have a GitHub account that I use for work, and a separate one for my personal projects. But I had the hardest time getting both of them set up on the same computer. I’m going to point you to all of the articles that helped me, but I’ll also walk you through the process and explain what gave me so much trouble.
By the way, while I use Windows, most of these steps seem applicable across most operating systems.
Intro
With the benefit of hindsight, I now realize that a lot of my problem came from not really understanding some of the details around git. While I use git everyday, I rarely get outside of the clone, add, commit, push,
and pull
commands. I certainly didn’t understand the ins and outs of how git accounts work and all of that. And because articles that deal with this are talking about sensitive data (logins, SSH keys, repo URLs, etc), it can be hard to tell which parts of the examples are literal and which are placeholders.
I’m going to walk you through the steps, and I’ll be as explicit as possible about the parts that you literally need to type verbatim and which parts you can edit for your own needs. And I was only able to figure this out by putting together points from the following resources:
- StackOverflow post: https://stackoverflow.com/questions/3860112/multiple-github-accounts-on-the-same-computer
- TutsPlus Tutorial: https://code.tutsplus.com/tutorials/quick-tip-how-to-work-with-github-and-multiple-accounts–net-22574
- Medium article: https://medium.com/@pinglinh/how-to-have-2-github-accounts-on-one-machine-windows-69b5b4c5b14e
- Free Code Camp article: https://www.freecodecamp.org/news/manage-multiple-github-accounts-the-ssh-way-2dadc30ccaca/
- GitHub doc: https://docs.github.com/en/repositories/creating-and-managing-repositories/troubleshooting-cloning-errors#error-repository-not-found
- GitHub doc: https://docs.github.com/en/get-started/getting-started-with-git/managing-remote-repositories#switching-remote-urls-from-https-to-ssh
Step 1: Understand Your Need
Hopefully it will help you figure out how to solve your specific need by understanding what I was trying to do. I work full time for a company that we’ll call XYZ, and I have a company email address — let’s say it’s nowens@xyzideas.com for the sake of our example. I have a GitHub account tied to that email address and that company, and the username is nate-owens.
I also have a GitHub account for my personal projects and side work. That account is nate-78, and it’s tied to the nathan@owensdev.com email address. I’ve always used SVN to manage my OwensDev projects, but because I use git everyday for work, I’ve been wanting to move my personal stuff over to that as well.
On my computer, I store my work projects at C:\xyz-git. I’d like for my personal projects to be stored at C:\owensdev-git.
Now that you understand what I was trying to accomplish, let’s walk through the steps for how to make it happen.
Step 2: Create SSH Keys
You can use an SSH key to let GitHub know who you are when your computer makes calls against git. Because I wanted to talk to two different accounts, I needed two different SSH keys. If you’re already a git user, it’s likely that you already have an SSH key set up for your main account. I was setting up a new laptop, so I needed to create both keys.
Note: even though I needed two keys, I completed all the steps for one key before starting with the second one. I recommend doing it that way if you need both.
Open a terminal (I used Git Bash) and execute the following:
ssh-keygen -t rsa -C "your-email-address"
I decided to create my work account first, so I used my nowens@xyzideas.com email address.
The following message will show in the terminal:
Generating public/private rsa key pair.
Enter file in which to save the key (/c/Users/your_username/.ssh/id_rsa):
I highly recommend naming your file after the account you’ll use the key for. For instance, I decided to name mine after my email address and company, so I typed in:
/c/Users/Nathan/.ssh/id_rsa_nowens_xyz
You’ll be asked to enter a passphrase, but I recommend leaving this blank. Otherwise, you’ll have to enter that passphrase every time you interact with GitHub. The only reason you might want to use one is if you’re sharing your computer with other people.
Step 3: Attach the Key to GitHub
You now need to let GitHub know about your new key. Log in to your GitHub account, click your profile image in the top right, and select “Settings.” On the following page, click “SSH and GPG keys” in the left sidebar.
You’ll see a list of SSH keys, if you’ve already set any up. Click the “New SSH Key” button, and you’ll see this:
For the title, it’s smart to be machine specific so you can easily recognize it in the future (and delete it if you stop using that computer). I named mine “Nathan’s Dell XPS.”
There are two ways to get the value of the key. One way is by using the terminal:
vim ~/.ssh/<the-name-you-gave-your-file>.pub
For my example, I’d type in:
vim ~/.ssh/id_rsa_nowens_xyz.pub
This will display the value of your key at the top of your console:
Copy that value, including both the “ssh-rsa ” at the beginning and the email address at the end, and paste that into the textarea on GitHub.
If you don’t like vim and don’t want to use the terminal, you can actually navigate to the C:/Users/<your-account>/.ssh folder and open the .pub folder in a text editor. That will also give you the value you need.
Step 4: Tell SSH About the Key
This step was surprising to me, because I would have assumed that SSH on my machine would already be aware of the key I just created, but apparently it doesn’t work that way.
First, make sure the ssh-agent is running by using the following command:
eval "$(ssh-agent -s)"
That command should return an agent ID, which means you’re good to go. Add your new key:
ssh-add ~/.ssh/id_rsa_<your-suffix>
For me, the command was:
ssh-add ~/.ssh/id_rsa_nowens_xyz
Step 5: Repeat Steps if You Need Other Keys
I also needed a key for my personal account, so I repeated the above steps and created a key with the name “id_rsa_owensdev”.
A Quick Aside: How to List Your Current SSH Keys
If you forget exactly what you named certain keys, there’s a handy command that can help you out.
ls -al ~/.ssh
“ls” means “list.” The “a” tells the terminal to list all of the files, even hidden ones. And the “l” says that you want to see them in long format. You’ll get a list like this:
$ ls -al ~/.ssh
total 35
drwxr-xr-x 1 Nathan 197121 0 Oct 23 10:38 ./
drwxr-xr-x 1 Nathan 197121 0 Oct 23 10:38 ../
-rw-r--r-- 1 Nathan 197121 224 Oct 22 18:10 config
-rw-r--r-- 1 Nathan 197121 2610 Oct 22 16:55 id_rsa_owensdev
-rw-r--r-- 1 Nathan 197121 573 Oct 22 16:55 id_rsa_owensdev.pub
-rw-r--r-- 1 Nathan 197121 2602 Oct 23 10:17 id_rsa_nowens_xyz
-rw-r--r-- 1 Nathan 197121 573 Oct 23 10:17 id_rsa_nowens_xyz.pub
-rw-r--r-- 1 Nathan 197121 1446 Oct 22 19:50 known_hosts
-rw-r--r-- 1 Nathan 197121 392 Oct 22 18:14 known_hosts.old
Step 6: Create a Config File
This is where the magic really happens. This is how you tell git on your machine which accounts you’re trying to talk to.
In your /.ssh directory (C:/Users/<your-account>/.ssh), create a config file. No extension — just a file called “config” (you can see it listed in the previous step). This is how my config file looks:
#Default GitHub
Host github.com
HostName github.com
User git
IdentityFile ~/.ssh/id_rsa_nowens_xyz
#Second GitHub
Host github.com-owensdev
HostName github.com
User git
IdentityFile ~/.ssh/id_rda_owensdev
Let’s talk about the different parts of these entries:
- Host
- When talking to a repo, this is the value that goes after the @ symbol. For instance,
git clone https://git@<Host>...
- When talking to a repo, this is the value that goes after the @ symbol. For instance,
- HostName
- This should remain “github.com”
- User
- This is NOT your GitHub username — this should remain “git”
- IdentifyFile
- And this should be the path to your new SSH key
- “~/.ssh” is not being used as a placeholder. You do not need the full actual path here — it should be written out exactly as my example shows.
Maybe You’re Done?
Different people have different setups with GitHub, so it’s possible that you’re already in good shape now.
One of the things you can do to see if the SSH keys are setup correctly is to use this command in your terminal:
ssh -T git@github.com
If everything’s been setup correctly, you should get a message like this:
Hi nate-owens! You've successfully authenticated, but GitHub does not provide shell access.
It’s also good to test your second connection by using the Host
that you specified in your config file. For instance:
ssh -T git@github.com-owensdev
Once you’ve tested these, you can try to work with your repos (step 8) and see if you have success. If you’re having trouble accessing one or both of the repos, try step 7.
Step 7: Update .gitconfig
At this point, my work account was working fine, but I was having trouble with my personal one. I couldn’t clone any of my repos. I now realize that part of my issue was that I didn’t understand how the Host
settings in the config file really worked when working with GitHub URLs. So it’s possible that I didn’t need to make the following changes to my gitconfig, but I did, and maybe you’ll find them helpful.
In my default .gitconfig (C:/Users/<user-name>/.gitconfig), I left my default user information the same (for my work account). But I added a conditional to load a different .gitconfig file when using my owensdev-git directory:
[user]
name = nate-owens
email = nowens@xyzideas.com
[includeIf "gitdir/i:C:/owensdev-git/"]
path = C:/owensdev-git/.gitconfig
Note: While the other examples I read used relative paths, that didn’t work in my case (because of Windows). I needed to use explicit paths. Also, the ‘i:’ part of the includeIf path means case insensitive.
I also added a line to the [core] section that specifies the SSH key I’m using for my default account:
[core]
sshCommand = ssh -i ~/.ssh/id_rsa_nowens_ccg
Then, inside my owensdev-git directory, I created the following .gitconfig file:
[user]
email = nathan@owensdev.com
name = nate-78
[core]
sshCommand = ssh -i ~/.ssh/id_rda_owensdev
In order to get that working, I had to also run git init
in my owensdev-git directory.
Step 8: Working with Repos
Your main repo (in my case, my work repo) should work as it normally would. You should be able to clone a repo from your main account by using a command like :
git clone https://github.com/XYZ-Company/Some-Repo.git
When working with your secondary repo (my personal one, in my case), structure your commands in the same way, and git will automatically use the user credentials and SSH key that you set up in the .gitconfig file for that directory.
git clone https://github.com/Secondary-Account/Some-Repo.git
Conclusion
I hope all of my pain and frustration pays off for you in some way. If you’re experiencing trouble, check out all the links I listed at the top of this post. Specifically, if you can clone repos, but are having trouble working with repos you already had on your machine, you might want to read step 7 on this post.