<?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>DEV Community: Tirth Patel</title>
    <description>The latest articles on DEV Community by Tirth Patel (@tirthnp).</description>
    <link>https://dev.to/tirthnp</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%2F873827%2F68d51fd6-4794-4905-a3ce-b59e343329d8.png</url>
      <title>DEV Community: Tirth Patel</title>
      <link>https://dev.to/tirthnp</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/tirthnp"/>
    <language>en</language>
    <item>
      <title>Using multiple GitHub accounts on one computer</title>
      <dc:creator>Tirth Patel</dc:creator>
      <pubDate>Thu, 09 Jun 2022 18:11:30 +0000</pubDate>
      <link>https://dev.to/tirthnp/using-multiple-github-accounts-on-one-computer-p26</link>
      <guid>https://dev.to/tirthnp/using-multiple-github-accounts-on-one-computer-p26</guid>
      <description>&lt;p&gt;For this article I will use example of two accounts but it could very easily we expanded to multiple accounts.&lt;/p&gt;

&lt;p&gt;First step is to create ssh-keys which could be done by the following commands:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;ssh-keygen -t ed25519 -C "your_email@personal.com"
ssh-keygen -t ed25519 -C "your_email@work.com"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Note that &lt;code&gt;-C "your_email@personal.com"&lt;/code&gt; will just add your email address at the end or the public key. &lt;code&gt;-C&lt;/code&gt; option is to add comment at the end of the public key. So it could be anything you like.&lt;/p&gt;

&lt;p&gt;Also note &lt;code&gt;-t ed25519&lt;/code&gt; is specifying algorithm used to create the public and private keys. &lt;code&gt;ed25519&lt;/code&gt; or Edwards-curve Digital Signature Algorithm is the 4th generation protocol for ssh key generation. If you don't set this option it will use the RSA algorithm by default to generate the key pairs.  &lt;/p&gt;

&lt;p&gt;Now you will be prompted with:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Enter file in which to save the key (/home/{username}/.ssh/id_ed25519):
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;I would recommend you give a personalized name with location like &lt;code&gt;/home/{username}/.ssh/personal&lt;/code&gt; and same goes for work one &lt;code&gt;/home/{username}/.ssh/work&lt;/code&gt;&lt;br&gt;
In your home directory you will see .ssh folder which should have &lt;code&gt;personal.pub&lt;/code&gt; and &lt;code&gt;work.pub&lt;/code&gt; which contains your public key and &lt;code&gt;personal&lt;/code&gt;,&lt;code&gt;work&lt;/code&gt; which contains your private key.&lt;/p&gt;

&lt;p&gt;Now you need to start the ssh-agent using, the following command:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;eval $(ssh-agent)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Note that we do &lt;code&gt;eval&lt;/code&gt; because ssh-agent once outputs bunch of variables that could be set as environment variables. This just makes life easier so its a good practise. You can check this &lt;a href="https://unix.stackexchange.com/questions/351725/why-eval-the-output-of-ssh-agent?newreg=b48571273c4e4abeb5c432b358b60ddc"&gt;link&lt;/a&gt; for more reference.&lt;/p&gt;

&lt;p&gt;Now you could add the private keys to the ssh-agent, so that you don't have to put your passphrase every-time you do anything.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;ssh-add ~/.ssh/personal
ssh-add ~/.ssh/work
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You will be prompted to enter your passphrase for this. You can check if your keys are added by doing&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;ssh-add -l
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now you need to add the public keys to your GitHub accounts. This is a good &lt;a href="https://docs.github.com/en/authentication/connecting-to-github-with-ssh/adding-a-new-ssh-key-to-your-github-account"&gt;link&lt;/a&gt; for reference.&lt;br&gt;
Go to GitHub can click on your profile on top right hand corner and got to settings.&lt;br&gt;
There if you scroll down you will see an option &lt;code&gt;New SSH key&lt;/code&gt;. Click that green button and add the appropriate key. You can copy the key by&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;pbcopy &amp;lt; ~/.ssh/personal.pub
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You have copied the contents of the &lt;code&gt;personal.pub&lt;/code&gt; file. You can paste this to your GitHub account as new ssh-key. You can give it a Title in GitHub to better remember it.&lt;/p&gt;

&lt;p&gt;Now just hit &lt;code&gt;Add SSH key&lt;/code&gt; button and finally you have added the public key to your GitHub. Assuming that you have added both the keys to the respective account let's set up the ssh and git configs to work with both the accounts.&lt;/p&gt;

&lt;p&gt;Go to &lt;code&gt;.ssh&lt;/code&gt; in your home directory. Check if you have an existing &lt;code&gt;config&lt;/code&gt; file. If you don't have one create one. You need to create one that looks like&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# Default github account: work-account
Host github.work.com
   HostName github.work.com
   IdentityFile ~/.ssh/work
   IdentitiesOnly yes

# Other github account: personal-account
Host github.com
   HostName github.com
   IdentityFile ~/.ssh/personal
   IdentitiesOnly yes
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Note that the lines starting with &lt;code&gt;#&lt;/code&gt; are just comments and does not affect the config. The flag used &lt;code&gt;IdentitiesOnly&lt;/code&gt; is used for ssh-agent to point at the specific key provided by &lt;code&gt;IdentityFile&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Now we want to separate our git configs. Here you could choose your style. For me I like to use my work account everywhere except the &lt;code&gt;~/personal&lt;/code&gt; folder. Here I am going to use a minimal &lt;code&gt;.gitconfig&lt;/code&gt; to avoid confusion but go fancy with it.&lt;/p&gt;

&lt;p&gt;In you home dir if you don't have one already create a &lt;code&gt;.gitconfig&lt;/code&gt; file.&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 = your_email@work.com
    name = Your Name


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

&lt;/div&gt;



&lt;p&gt;In this file we are setting your default Name, Email and setting a condition that if you are in &lt;code&gt;~/personal/&lt;/code&gt; it will use load gitconfig from &lt;code&gt;~/personal/.gitconfig-pers&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Now we have to create the &lt;code&gt;~/personal/.gitconfig-pers&lt;/code&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 = your_email@personal.com 
        name = Your Name
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This file will override the email and name when in &lt;code&gt;~/personal&lt;/code&gt;. &lt;br&gt;
Note that the changes will apply to the directories inside the &lt;code&gt;~/personal/&lt;/code&gt;. Which means to check if you config worked create a test folder like so &lt;code&gt;~/personal/test&lt;/code&gt;.&lt;br&gt;
Here if you type&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;git config --get user.email
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You will get your personal email. Except for sub-folders inside &lt;code&gt;~/personal/&lt;/code&gt; you will get your work email when running the same command.&lt;/p&gt;

&lt;p&gt;Enjoy your new setup!&lt;/p&gt;

</description>
    </item>
  </channel>
</rss>
