<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: mg</title>
    <description>The latest articles on Forem by mg (@mbgeorge48).</description>
    <link>https://forem.com/mbgeorge48</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1155820%2Fa5f8822a-b70d-4845-8522-aed8f4356fbe.jpg</url>
      <title>Forem: mg</title>
      <link>https://forem.com/mbgeorge48</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/mbgeorge48"/>
    <language>en</language>
    <item>
      <title>Managing multiple GitHub SSH logins on a single machine</title>
      <dc:creator>mg</dc:creator>
      <pubDate>Tue, 02 Apr 2024 19:59:04 +0000</pubDate>
      <link>https://forem.com/mbgeorge48/managing-multiple-github-ssh-logins-on-a-single-machine-1k46</link>
      <guid>https://forem.com/mbgeorge48/managing-multiple-github-ssh-logins-on-a-single-machine-1k46</guid>
      <description>&lt;p&gt;Do you have more than one account for GitHub, i.e. one for professional work dev and another for personal side projects?&lt;/p&gt;

&lt;p&gt;If you're using SSH to clone repositories this might help...&lt;/p&gt;

&lt;h3&gt;
  
  
  Setting up your SSH keys
&lt;/h3&gt;

&lt;p&gt;I won't copy the existing &lt;a href="https://docs.github.com/en/authentication/connecting-to-github-with-ssh/generating-a-new-ssh-key-and-adding-it-to-the-ssh-agent"&gt;GitHub docs&lt;/a&gt; for generating keys&lt;/p&gt;




&lt;p&gt;Once you've created the keys you should add them to your GitHub accounts, again &lt;a href="https://docs.github.com/en/authentication/connecting-to-github-with-ssh/adding-a-new-ssh-key-to-your-github-account"&gt;their article&lt;/a&gt; would be better than mine...&lt;/p&gt;




&lt;p&gt;Now that you've got at least 2 SSH keys you need to add some config to &lt;code&gt;~/.ssh/config&lt;/code&gt;, if that file doesn't exist yet you can create it.&lt;/p&gt;

&lt;p&gt;In that file, you need to add an entry like this for each GitHub account&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;Host github.com
    Hostname github.com
    User git
    AddKeysToAgent &lt;span class="nb"&gt;yes
    &lt;/span&gt;IdentitiesOnly &lt;span class="nb"&gt;yes
    &lt;/span&gt;IdentityFile ~/.ssh/id_fizz
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;For each one you need to change the &lt;code&gt;Host&lt;/code&gt; and the &lt;code&gt;IdentityFile&lt;/code&gt;. i.e. if you had a personal and a work account you could set it up like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;Host github.com
    Hostname github.com
    User git
    AddKeysToAgent &lt;span class="nb"&gt;yes
    &lt;/span&gt;IdentitiesOnly &lt;span class="nb"&gt;yes
    &lt;/span&gt;IdentityFile ~/.ssh/id_fizz

Host work-github.com
    Hostname github.com
    User git
    AddKeysToAgent &lt;span class="nb"&gt;yes
    &lt;/span&gt;IdentitiesOnly &lt;span class="nb"&gt;yes
    &lt;/span&gt;IdentityFile ~/.ssh/id_buzz
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now you've done that you should be able to be able to test you're connections like so:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ ssh -T git@github.com
Hi &amp;lt;Personal GitHub Username&amp;gt;! You've successfully authenticated, but GitHub does not provide shell access.

$ ssh -T git@work-github.com
Hi &amp;lt;Work GitHub Username&amp;gt;! You've successfully authenticated, but GitHub does not provide shell access.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now you've done this you can clone repos like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;### personal ###
$ git clone git@github.com:fizz/buzz.git


### work ###
$ git clone git@work-github.com:fizz/buzz.git
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Updating your Git config to manage this for you
&lt;/h3&gt;

&lt;p&gt;You might want to set up you're gitconfig files to handle this for you. There are a few ways of doing this but I do it like this.&lt;br&gt;
In my root .gitconfig file, I have this configured:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="o"&gt;[&lt;/span&gt;includeIf &lt;span class="s2"&gt;"gitdir:~/work/"&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt;
    path &lt;span class="o"&gt;=&lt;/span&gt; ~/work/.gitconfig-work

&lt;span class="o"&gt;[&lt;/span&gt;includeIf &lt;span class="s2"&gt;"gitdir:~/personal/"&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt;
    path &lt;span class="o"&gt;=&lt;/span&gt; ~/personal/.gitconfig-personal
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And then in my &lt;code&gt;~/personal/&lt;/code&gt; and &lt;code&gt;~/work/&lt;/code&gt; directory, I have gitconfig that is specific to each user. For example:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;.gitconfig-personal&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;[user]
  email = buzz.fizz@personal.co
  name = mg
  username = buzz
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;.gitconfig-work&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;[user]
  email = fizz.buzz@work.co
  name = mg
  username = fizz

[url "git@work-github.com:"]
  insteadOf = "git@github.com:"

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the &lt;code&gt;work&lt;/code&gt; based config I have a &lt;a href="https://git-scm.com/docs/git-config#Documentation/git-config.txt-urlltbasegtinsteadOf"&gt;URL remapping&lt;/a&gt;. &lt;br&gt;
So in you're work directory you no longer need to do &lt;code&gt;$ git clone git@work-github.com:fizz/buzz.git&lt;/code&gt; you can do &lt;code&gt;$ git clone git@github.com:fizz/buzz.git&lt;/code&gt; as you would when you normally clone something from GitHub.&lt;/p&gt;

&lt;p&gt;(Thanks to &lt;a class="mentioned-user" href="https://dev.to/ccoveille"&gt;@ccoveille&lt;/a&gt; for making me aware of this!)&lt;/p&gt;




&lt;p&gt;I recently found out how to do it and thought I'd share, there might be better ways of doing it. Please let me know if there is!&lt;/p&gt;

</description>
      <category>github</category>
      <category>git</category>
      <category>ssh</category>
    </item>
  </channel>
</rss>
