<?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: Vutlhari Rikhotso</title>
    <description>The latest articles on DEV Community by Vutlhari Rikhotso (@vutlhari).</description>
    <link>https://dev.to/vutlhari</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%2F350980%2F3fb70fcc-3ccf-49bf-a503-5f3433049665.jpeg</url>
      <title>DEV Community: Vutlhari Rikhotso</title>
      <link>https://dev.to/vutlhari</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/vutlhari"/>
    <language>en</language>
    <item>
      <title>Managing Multiple Git Configs with SSH</title>
      <dc:creator>Vutlhari Rikhotso</dc:creator>
      <pubDate>Tue, 10 Sep 2024 14:47:44 +0000</pubDate>
      <link>https://dev.to/vutlhari/managing-multiple-git-configs-with-ssh-494b</link>
      <guid>https://dev.to/vutlhari/managing-multiple-git-configs-with-ssh-494b</guid>
      <description>&lt;p&gt;I work as a consultant, which means I’m constantly jumping into new projects—sometimes coding full-time, other times just reviewing code or doing a repository deep-dive. Every few months, I’m setting up a fresh Version Control System (VCS) config. Usually it’s GitHub, sometimes Bitbucket, and every now and then, GitLab makes an appearance.&lt;/p&gt;

&lt;p&gt;So, here’s a simple guide to keep things smooth. I know there are plenty of tutorials out there, but I’m terrible at bookmarking, so I need one place I can reliably find. This guide covers managing multiple Git configs on the same machine, without breaking anything.&lt;/p&gt;

&lt;h2&gt;
  
  
  SSH or HTTPS? SSH!
&lt;/h2&gt;

&lt;p&gt;Most VCS platforms offer the option to authenticate via &lt;strong&gt;SSH&lt;/strong&gt; or &lt;strong&gt;HTTPS&lt;/strong&gt;. With HTTPS, Git doesn’t store your credentials by default, meaning you have to enter them every time you interact with a remote repository (clone, push, pull)—not ideal. SSH is more secure and convenient since, once set up, it works seamlessly in the background.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Rule of Thumb: One SSH keypair per Git Config&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Using one SSH keypair per VCS profile is the best practice. It keeps things organized and secure. Let’s get started.&lt;/p&gt;

&lt;h2&gt;
  
  
  Steps
&lt;/h2&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Note&lt;/strong&gt;: You can repeat the following steps for all your VCS accounts to manage multiple profiles effortlessly.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3&gt;
  
  
  1. Generate a New SSH Key
&lt;/h3&gt;

&lt;p&gt;First, create a new SSH key for your profile:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;ssh-keygen &lt;span class="nt"&gt;-t&lt;/span&gt; ed25519 &lt;span class="nt"&gt;-C&lt;/span&gt; &lt;span class="s2"&gt;"email@example.com"&lt;/span&gt; &lt;span class="nt"&gt;-f&lt;/span&gt; ~/.ssh/id_&amp;lt;profile&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;-t ed25519: This generates an Ed25519 key, which is faster and more secure than RSA.&lt;/li&gt;
&lt;li&gt;-C "&lt;a href="mailto:email@example.com"&gt;email@example.com&lt;/a&gt;": The comment to help you remember which email this key is associated with.&lt;/li&gt;
&lt;li&gt;-f ~/.ssh/id_: Specify the file name for the key, so it’s easier to identify later.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;If you prefer to use RSA:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;ssh-keygen &lt;span class="nt"&gt;-t&lt;/span&gt; rsa &lt;span class="nt"&gt;-b&lt;/span&gt; 4096 &lt;span class="nt"&gt;-C&lt;/span&gt; &lt;span class="s2"&gt;"email@example.com"&lt;/span&gt; &lt;span class="nt"&gt;-f&lt;/span&gt; ~/.ssh/id_&amp;lt;profile&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  Passphrase: Add It for Extra Security
&lt;/h4&gt;

&lt;p&gt;During the key generation process, you’ll be prompted to add a passphrase. &lt;strong&gt;It’s highly recommended to use one&lt;/strong&gt; as it adds a layer of security to your private key.&lt;/p&gt;

&lt;p&gt;To check your newly created key files, run:&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;ls&lt;/span&gt; &lt;span class="nt"&gt;-la&lt;/span&gt; ~/.ssh
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You should see both the private key &lt;code&gt;id_&amp;lt;profile&amp;gt;&lt;/code&gt; and the public key &lt;code&gt;id_&amp;lt;profile&amp;gt;.pub&lt;/code&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  2. Add the SSH Key to the SSH Agent
&lt;/h3&gt;

&lt;p&gt;Next, we need to ensure that ssh-agent remembers your passphrase so you don’t have to enter it every time.&lt;/p&gt;

&lt;p&gt;First, ensure ssh-agent is running:&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;eval&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="si"&gt;$(&lt;/span&gt;ssh-agent &lt;span class="nt"&gt;-s&lt;/span&gt;&lt;span class="si"&gt;)&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now, add your key to the agent:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;ssh-add &lt;span class="nt"&gt;--apple-use-keychain&lt;/span&gt; ~/.ssh/id_&amp;lt;profile&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;code&gt;--apple-use-keychain&lt;/code&gt; flag (macOS only) adds the key to your keychain for easier access. If you’re on Linux or Windows, you may omit this option.&lt;/p&gt;

&lt;h3&gt;
  
  
  3. Configure the SSH Settings
&lt;/h3&gt;

&lt;p&gt;Now, let’s create or modify your SSH config file to organize your keys:&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;Open the file and add the following:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;Host &lt;span class="k"&gt;*&lt;/span&gt;
  AddKeysToAgent &lt;span class="nb"&gt;yes
  &lt;/span&gt;UseKeychain &lt;span class="nb"&gt;yes

&lt;/span&gt;Host github-profile_a
  HostName github.com
  User git
  IdentityFile ~/.ssh/id_profile_a
  IdentitiesOnly &lt;span class="nb"&gt;yes

&lt;/span&gt;Host github-profile_b
  HostName github.com
  User git
  IdentityFile ~/.ssh/id_profile_b
  IdentitiesOnly &lt;span class="nb"&gt;yes&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This ensures that your SSH key is automatically added to the agent and remembered.&lt;/p&gt;

&lt;h3&gt;
  
  
  4. Copy the Public SSH Key to Your VCS
&lt;/h3&gt;

&lt;p&gt;Copy your public key to your clipboard:&lt;br&gt;
&lt;/p&gt;

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

&lt;/div&gt;



&lt;p&gt;Now, add this key to your respective VCS platform:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://docs.github.com/en/authentication/connecting-to-github-with-ssh/adding-a-new-ssh-key-to-your-github-account?ref=blog.gitguardian.com" rel="noopener noreferrer"&gt;Add SSH key to Github&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://support.atlassian.com/bitbucket-cloud/docs/set-up-an-ssh-key/?ref=blog.gitguardian.com#Step-3.-Add-the-public-key-to-your-Account-settings" rel="noopener noreferrer"&gt;Add SSH key to BitBucket&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://docs.gitlab.com/ee/ssh/?ref=blog.gitguardian.com#add-an-ssh-key-to-your-gitlab-account" rel="noopener noreferrer"&gt;Add SSH key to Gitlab&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  5. Set Up Git Configurations for Multiple Profiles
&lt;/h3&gt;

&lt;p&gt;To manage multiple VCS profiles (clients, personal, etc.), create separate &lt;code&gt;.gitconfig&lt;/code&gt; files for each profile, ensuring your repositories use the right settings.&lt;/p&gt;

&lt;p&gt;Assume your project directory is structured 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;/code/
    |__client_a/
    |__client_b/
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Create separate Git configuration files for each profile:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;/code/
|__profile_a/
    |_.gitconfig.profile_a
|__profile_b/
    |_.gitconfig.profile_b
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In each &lt;code&gt;.gitconfig.&amp;lt;profile&amp;gt;&lt;/code&gt;, set up the details for the corresponding profile:&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;# ~/code/&amp;lt;profile&amp;gt;/.gitconfig.&amp;lt;profile&amp;gt;&lt;/span&gt;

&lt;span class="o"&gt;[&lt;/span&gt;user]
  email &lt;span class="o"&gt;=&lt;/span&gt; profile_email@example.com
  name &lt;span class="o"&gt;=&lt;/span&gt; Your Name

&lt;span class="o"&gt;[&lt;/span&gt;github]
  user &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"username_for_account"&lt;/span&gt;

&lt;span class="o"&gt;[&lt;/span&gt;core]
  sshCommand &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"ssh -i ~/.ssh/id_&amp;lt;profile&amp;gt; -o IdentitiesOnly=yes"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;sshCommand = "ssh -i ~/.ssh/id_&amp;lt;profile&amp;gt;"&lt;/code&gt;: This command tells Git which SSH key to use for this specific profile.&lt;/p&gt;

&lt;h3&gt;
  
  
  6. Configure the Global Git Settings
&lt;/h3&gt;

&lt;p&gt;Finally, configure your global &lt;code&gt;.gitconfig&lt;/code&gt; to include these individual profile settings based on the directory:&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;# ~/.gitconfig&lt;/span&gt;

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

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

&lt;span class="o"&gt;[&lt;/span&gt;core]
  excludesfile &lt;span class="o"&gt;=&lt;/span&gt; ~/.gitignore &lt;span class="c"&gt;# applies globally&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;code&gt;includeIf&lt;/code&gt; directive tells Git to apply the corresponding &lt;code&gt;.gitconfig&lt;/code&gt; profile settings when you’re working in the specified directory. It ensures each repository is using the correct SSH key and user details.&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;Hopefully, following this guide, you’ll have a streamlined, secure setup for managing multiple Git profiles on the same machine. No more re-entering credentials or worrying about mixing up keys across projects.&lt;/p&gt;

</description>
      <category>git</category>
      <category>github</category>
      <category>bitbucket</category>
      <category>gitlab</category>
    </item>
  </channel>
</rss>
