<?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: אוריאל גימאני</title>
    <description>The latest articles on DEV Community by אוריאל גימאני (@__97fb9697a93).</description>
    <link>https://dev.to/__97fb9697a93</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%2F1973118%2F04fd3981-01a6-42ac-9fff-d805ef2f2742.png</url>
      <title>DEV Community: אוריאל גימאני</title>
      <link>https://dev.to/__97fb9697a93</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/__97fb9697a93"/>
    <language>en</language>
    <item>
      <title>Setting up multiple ssh keys and git repositories on a single machine</title>
      <dc:creator>אוריאל גימאני</dc:creator>
      <pubDate>Sat, 24 Aug 2024 22:42:29 +0000</pubDate>
      <link>https://dev.to/__97fb9697a93/setting-up-multiple-ssh-keys-and-git-repositories-on-a-single-machine-bbo</link>
      <guid>https://dev.to/__97fb9697a93/setting-up-multiple-ssh-keys-and-git-repositories-on-a-single-machine-bbo</guid>
      <description>&lt;p&gt;In this short guide ill walk you through setting up multiple GitHub accounts with ssh on the same machine so that different repositories will be automatically related to the right ssh key and account.&lt;/p&gt;

&lt;p&gt;Explaining the problem:&lt;br&gt;
Lets say you have two GitHub accounts — one for work, the other for personal usage.&lt;br&gt;
Each of these accounts has one or more repositories on your machine that you want to associate with said account.&lt;/p&gt;
&lt;h3&gt;
  
  
  Step 1: Setting up ssh
&lt;/h3&gt;

&lt;p&gt;Create an ssh key for each account and add them to your GitHub accounts, You can use GitHub’s guide on how to do that here.&lt;/p&gt;
&lt;h3&gt;
  
  
  Step 2: Setting up ssh config hosts.
&lt;/h3&gt;

&lt;p&gt;create an ssh config file in your .ssh folder. This file is responsible for telling your ssh agent what key to use for each domain:&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="nb"&gt;touch&lt;/span&gt; ~/.ssh/config
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now add the following to the config file:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Host github.com-personal-account # github.com-{name-with-hyphen}
  HostName github.com
  AddKeysToAgent yes
  UseKeychain yes # only if you used a keychain when creating the ssh key
  IdentityFile ~/.ssh/private_key_1

Host github.com-work-account
  HostName github.com 
  AddKeysToAgent yes
  UseKeychain yes # only if you used a keychain when creating the ssh key
  IdentityFile ~/.ssh/private_key_2
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You should be able test the success / failure of this part by using the following command:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;ssh &lt;span class="nt"&gt;-T&lt;/span&gt; git@github.com-personal-account

&lt;span class="c"&gt;# If everything is working you should see this message: &lt;/span&gt;
Hi &lt;span class="o"&gt;{&lt;/span&gt;github account name&lt;span class="o"&gt;}!&lt;/span&gt; Youve successfully authenticated, but GitHub does not provide shell access.

&lt;span class="c"&gt;# if not - troubleshoot the previous steps before moving on :)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Step 3: Associate repositories with the corresponding ssh keys.
&lt;/h3&gt;

&lt;p&gt;There are two different cases we may encounter here;&lt;br&gt;
One is cloning a new repository and the second is attaching an already cloned repository.&lt;br&gt;
Both cases are pretty straightforward so let me explain each of them.&lt;/p&gt;
&lt;h3&gt;
  
  
  Cloning a new repository:
&lt;/h3&gt;

&lt;p&gt;Go to GitHub and press on the “&amp;lt;&amp;gt;code” button, choose ssh and copy the link.&lt;br&gt;
You’ll receive something like the following: (please follow the example)&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;git@github.com:&lt;span class="o"&gt;{&lt;/span&gt;repo-owner-name&lt;span class="o"&gt;}&lt;/span&gt;/&lt;span class="o"&gt;{&lt;/span&gt;repo-name&lt;span class="o"&gt;}&lt;/span&gt;.git

&lt;span class="c"&gt;# change it to:&lt;/span&gt;
git@github.com-&lt;span class="o"&gt;{&lt;/span&gt;username-from-host-in-config&lt;span class="o"&gt;}&lt;/span&gt;:&lt;span class="o"&gt;{&lt;/span&gt;repo-owner-name&lt;span class="o"&gt;}&lt;/span&gt;/&lt;span class="o"&gt;{&lt;/span&gt;repo-name&lt;span class="o"&gt;}&lt;/span&gt;.git

&lt;span class="c"&gt;# example:&lt;/span&gt;
git@github.com-personal-account:&lt;span class="o"&gt;{&lt;/span&gt;repo-owner-name&lt;span class="o"&gt;}&lt;/span&gt;/&lt;span class="o"&gt;{&lt;/span&gt;repo-name&lt;span class="o"&gt;}&lt;/span&gt;.git

&lt;span class="c"&gt;#now use it to run git clone:&lt;/span&gt;
git clone git@github.com-personal-account:&lt;span class="o"&gt;{&lt;/span&gt;repo-owner-name&lt;span class="o"&gt;}&lt;/span&gt;/&lt;span class="o"&gt;{&lt;/span&gt;repo-name&lt;span class="o"&gt;}&lt;/span&gt;.git
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The commands above will clone the repository and make sure that each git command involving this repository will be directed through the corresponding ssh key.&lt;/p&gt;

&lt;p&gt;(Don’t forget to set the &lt;code&gt;user.name&lt;/code&gt; and &lt;code&gt;user.email&lt;/code&gt; properties for your commits metadata).&lt;/p&gt;

&lt;h3&gt;
  
  
  Linking an existing local repository:
&lt;/h3&gt;

&lt;p&gt;In case you already have a local repository you can set it easily to work with your newly set ssh. Go to the repository base directory and follow these commands:&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="c"&gt;#this command shows the current origin of the repo&lt;/span&gt;
git remote &lt;span class="nt"&gt;-v&lt;/span&gt;

&lt;span class="c"&gt;#you will recieve something like this:&lt;/span&gt;
origin git@github.com:&lt;span class="o"&gt;{&lt;/span&gt;repo-owner&lt;span class="o"&gt;}&lt;/span&gt;/&lt;span class="o"&gt;{&lt;/span&gt;repo-name&lt;span class="o"&gt;}&lt;/span&gt;.git &lt;span class="o"&gt;(&lt;/span&gt;fetch&lt;span class="o"&gt;)&lt;/span&gt;
origin git@github.com:&lt;span class="o"&gt;{&lt;/span&gt;repo-owner&lt;span class="o"&gt;}&lt;/span&gt;/&lt;span class="o"&gt;{&lt;/span&gt;repo-name&lt;span class="o"&gt;}&lt;/span&gt;.git &lt;span class="o"&gt;(&lt;/span&gt;push&lt;span class="o"&gt;)&lt;/span&gt;

&lt;span class="c"&gt;# copy the origin and add to it as follows:&lt;/span&gt;
git@github.com-&lt;span class="o"&gt;{&lt;/span&gt;profile-name&lt;span class="o"&gt;}&lt;/span&gt;:&lt;span class="o"&gt;{&lt;/span&gt;repo-owner&lt;span class="o"&gt;}&lt;/span&gt;/&lt;span class="o"&gt;{&lt;/span&gt;repo-name&lt;span class="o"&gt;}&lt;/span&gt;.git

&lt;span class="c"&gt;# example:&lt;/span&gt;
git@github.com-personal-account:&lt;span class="o"&gt;{&lt;/span&gt;repo-owner&lt;span class="o"&gt;}&lt;/span&gt;/&lt;span class="o"&gt;{&lt;/span&gt;repo-name&lt;span class="o"&gt;}&lt;/span&gt;.git

&lt;span class="c"&gt;# now use the origin you wrote to set the repo new origin &lt;/span&gt;
git remote set-url origin git@github.com-personal-account:&lt;span class="o"&gt;{&lt;/span&gt;repo-owner&lt;span class="o"&gt;}&lt;/span&gt;/&lt;span class="o"&gt;{&lt;/span&gt;repo-name&lt;span class="o"&gt;}&lt;/span&gt;.git
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  And thats it, you're done.
&lt;/h3&gt;

&lt;p&gt;You can make sure it works by running git pull.&lt;br&gt;
(Don’t forget to set the &lt;code&gt;user.name&lt;/code&gt; and &lt;code&gt;user.email&lt;/code&gt; properties for your commits metadata).&lt;/p&gt;

&lt;p&gt;I hope you find this guide helpful , For any questions / corrections / additions feel free to comment below.&lt;br&gt;
Thanks a lot for reading!&lt;/p&gt;

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