How to use multiple GitHub accounts on one machine
Tags: githubdevops
Published: 2023-04-22 19:29:13

---
title: How to use multiple GitHub accounts on one machine
published: true
date: 2022-07-23 00:00:00 UTC
tags: githubdevops
canonical_url: https://bstefanski.com/use-multiple-github-accounts-one-machine
---

I needed to create a new GitHub account for work, but I didn't want to switch my credentials every time I pushed, cloned, or pulled from my private repositories.

I quickly googled how to achieve this without much hassle, and here it is:

  
  
<section><h3 id="1-generate-the-ssh-keys-for-both-your-work-and-private-emails">1. Generate the SSH keys for both your work and private emails.</h3>
<p>It will ask you to enter a unique file name and passphrase. I liked the file name pattern from the other <a href="https://www.section.io/engineering-education/using-multiple-ssh-keys-for-multiple-github-accounts/">blog post</a>, so I borrowed (stole) it - <code>id_rsa_personal</code> and <code>id_rsa_work</code><br>
<!-- -->Don't forget to save the passphrase somewhere safe! For example in <a href="https://1password.com/">1password</a>. </p>
<div>
<div></div>
<pre><div><span><span>$ </span><span>cd</span><span> .ssh/ </span></span></div><div><span><span> </span></span></div><div><span><span>$ ssh-keygen -t rsa -C </span><span>"your-private-email@email.com"</span><span> </span></span></div><div><span><span>$ ssh-keygen -t rsa -C </span><span>"your-work-email@email.com"</span></span></div></pre>
</div>
<p>I am not sure if you should keep the commands' output, but I did! Just in case! </p></section><section><h3 id="2-copy-the-ssh-keys-and-paste-them-into-the-appropriate-github-account">2. Copy the SSH keys and paste them into the appropriate GitHub account</h3>
<div>
<div></div>
<pre><div><span><span>$ </span><span>ls</span><span> </span></span></div><div><span><span> </span></span></div><div><span><span>$ </span><span>cat</span><span> id_rsa_personal.pub </span><span>|</span><span> pbcopy </span></span></div><div><span><span>$ </span><span>cat</span><span> id_rsa_work.pub </span><span>|</span><span> pbcopy</span></span></div></pre>
</div> <p>Log in to the appropriate GitHub account, go to settings. Click on the <code>SSH and GPG keys</code>, and paste one of the ssh keys.</p></section><section><h3 id="3-setup-ssh-config">3. Setup SSH config</h3>
<p>You need to create an SSH config with both hosts configured.</p>
<div>
<div></div>
<pre><div><span><span>$ </span><span>vim</span><span> ~/.ssh/config </span></span></div><div><span><span> </span></span></div><div><span><span></span><span># replace IdentityFile with your own file name and :wq</span><span></span></span></div><div><span><span> </span></span></div><div><span><span>Host me.github.com </span></span></div><div><span><span> HostName github.com </span></span></div><div><span><span> User </span><span>git</span><span> </span></span></div><div><span><span> IdentityFile ~/.ssh/id_rsa_personal </span></span></div><div><span><span> </span></span></div><div><span><span>Host work.github.com </span></span></div><div><span><span> HostName github.com </span></span></div><div><span><span> User </span><span>git</span><span> </span></span></div><div><span><span> IdentityFile ~/.ssh/id_rsa_work</span></span></div></pre>
</div></section><section><h3 id="4-add-the-ssh-keys-to-the-ssh-agent">4. Add the SSH keys to the SSH Agent</h3>
<div>
<div></div>
<pre><div><span><span>$ ssh-add ~/.ssh/id_rsa_personal </span></span></div><div><span><span>$ ssh-add ~/.ssh/id_rsa_work</span></span></div></pre>
</div></section><section><h3 id="5-clone-the-repository-to-test-things-out">5. Clone the repository to test things out</h3>
<div>
<div></div>
<pre><div><span><span>$ </span><span>git</span><span> clone git@work.github.com:orgName/repoName.git</span></span></div></pre>
</div>
<br><br><br><p>Source: <a href="https://www.section.io/engineering-education/using-multiple-ssh-keys-for-multiple-github-accounts/">https://www.section.io/engineering-education/using-multiple-ssh-keys-for-multiple-github-accounts/</a></p></section>