<?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: Danillo Gomes</title>
    <description>The latest articles on DEV Community by Danillo Gomes (@danillog).</description>
    <link>https://dev.to/danillog</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%2F451076%2F4e805169-a4cb-45fa-b4b8-d3531ac28a22.jpeg</url>
      <title>DEV Community: Danillo Gomes</title>
      <link>https://dev.to/danillog</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/danillog"/>
    <language>en</language>
    <item>
      <title>Git: Managing Multiple Accounts</title>
      <dc:creator>Danillo Gomes</dc:creator>
      <pubDate>Sat, 13 Dec 2025 12:44:16 +0000</pubDate>
      <link>https://dev.to/danillog/git-managing-multiple-accounts-3mib</link>
      <guid>https://dev.to/danillog/git-managing-multiple-accounts-3mib</guid>
      <description>&lt;h1&gt;
  
  
  Managing Multiple Git Accounts: SSH Keys &amp;amp; Conditional Includes
&lt;/h1&gt;

&lt;p&gt;Every developer who has ever dealt with a personal project, a job, and perhaps a freelance project on the same machine knows the pain of accidentally performing a &lt;code&gt;git commit&lt;/code&gt; with the wrong email. Git allows you to manage multiple accounts using only native features.&lt;/p&gt;

&lt;p&gt;This guide shows how to configure Linux and Windows environments to keep personal and work projects organized, making Git automatically use the corresponding accounts and the correct SSH key for each project—eliminating the need for &lt;code&gt;git config --local user.email&lt;/code&gt; with every new clone.&lt;/p&gt;

&lt;p&gt;The configuration relies on two features:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Multiple SSH Keys:&lt;/strong&gt; A unique key for each Git provider (GitHub, GitLab, etc.) for authentication.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Git Conditional Inclusion:&lt;/strong&gt; The &lt;code&gt;includeIf&lt;/code&gt; directive in your &lt;code&gt;.gitconfig&lt;/code&gt; file loads specific settings (name and email) based on the repository path.&lt;/li&gt;
&lt;/ol&gt;




&lt;h2&gt;
  
  
  Step 1: Create One SSH Key for Each Identity
&lt;/h2&gt;

&lt;p&gt;First, create a unique SSH key for each account. &lt;strong&gt;Do not reuse old SSH keys.&lt;/strong&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  1. Generate the Personal Key
&lt;/h3&gt;

&lt;p&gt;Open your terminal and run the following command (replacing the placeholder with your personal email):&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;"your-personal-email@example.com"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;When asked where to save the key, &lt;strong&gt;do not accept the default&lt;/strong&gt;. Use a descriptive name:&lt;br&gt;
&lt;code&gt;Enter file in which to save the key: /home/user/.ssh/id_ed25519_pessoal&lt;/code&gt;&lt;/p&gt;
&lt;h3&gt;
  
  
  2. Generate the Work Key
&lt;/h3&gt;

&lt;p&gt;Repeat the process for your work account:&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;"your-work-email@example.com"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Save it as: &lt;code&gt;/home/user/.ssh/id_ed25519_trabalho&lt;/code&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  3. Add Keys to the SSH Agent (Recommended)
&lt;/h3&gt;

&lt;p&gt;To avoid typing your password every time, add them 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;&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;
ssh-add ~/.ssh/id_ed25519_pessoal
ssh-add ~/.ssh/id_ed25519_trabalho
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  Step 2: Configure the SSH Config File
&lt;/h2&gt;

&lt;p&gt;Tell SSH which key to use for each server by editing the &lt;code&gt;~/.ssh/config&lt;/code&gt; file. Add an entry for each account:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# Personal Account (GitHub)
Host github.com
    HostName github.com
    User git
    PreferredAuthentications publickey
    IdentityFile ~/.ssh/id_ed25519_personal

# Work Account (GitLab)
Host gitlab.company.com
    HostName gitlab.company.com
    User git
    PreferredAuthentications publickey
    IdentityFile ~/.ssh/id_ed25519_work

# Freelance Example (Bitbucket)
Host bitbucket.org-freelance
    HostName bitbucket.org
    User git
    PreferredAuthentications publickey
    IdentityFile ~/.ssh/id_ed25519_freelance
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;  &lt;strong&gt;Host:&lt;/strong&gt; An alias for the connection.&lt;/li&gt;
&lt;li&gt;  &lt;strong&gt;HostName:&lt;/strong&gt; The actual server address.&lt;/li&gt;
&lt;li&gt;  &lt;strong&gt;IdentityFile:&lt;/strong&gt; The path to the &lt;strong&gt;private key&lt;/strong&gt;.&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  Step 3: Add Public Keys to Git Providers
&lt;/h2&gt;

&lt;p&gt;Upload the &lt;strong&gt;public&lt;/strong&gt; part of each key (&lt;code&gt;.pub&lt;/code&gt;) to your Git provider (GitHub, GitLab, etc.):&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Copy the public key content:
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;   &lt;span class="nb"&gt;cat&lt;/span&gt; ~/.ssh/id_ed25519_pessoal.pub
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;Paste it into the "SSH and GPG keys" section of your account settings on the provider's website.&lt;/li&gt;
&lt;/ol&gt;




&lt;h2&gt;
  
  
  Step 4: Automate Account Switching with &lt;code&gt;includeIf&lt;/code&gt;
&lt;/h2&gt;

&lt;p&gt;Edit your global Git configuration file (&lt;code&gt;~/.gitconfig&lt;/code&gt; on Linux or &lt;code&gt;C:\Users\Username\.gitconfig&lt;/code&gt; on Windows). Use the &lt;code&gt;includeIf&lt;/code&gt; block to load specific settings based on the directory:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ini"&gt;&lt;code&gt;&lt;span class="c"&gt;# ~/.gitconfig
&lt;/span&gt;
&lt;span class="nn"&gt;[user]&lt;/span&gt;
    &lt;span class="py"&gt;name&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s"&gt;Your Default Name&lt;/span&gt;

&lt;span class="nn"&gt;[includeIf "gitdir:~/Documents/personal/"]&lt;/span&gt;
    &lt;span class="py"&gt;path&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s"&gt;~/Documents/personal/.gitconfig&lt;/span&gt;

&lt;span class="nn"&gt;[includeIf "gitdir:~/Documents/work/"]&lt;/span&gt;
    &lt;span class="py"&gt;path&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s"&gt;~/Documents/work/.gitconfig&lt;/span&gt;

&lt;span class="nn"&gt;[core]&lt;/span&gt;
    &lt;span class="py"&gt;editor&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s"&gt;code --wait&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; The &lt;code&gt;/&lt;/code&gt; at the end of the &lt;code&gt;gitdir&lt;/code&gt; path is crucial. On Windows, use forward slashes (&lt;code&gt;/&lt;/code&gt;).&lt;/p&gt;




&lt;h2&gt;
  
  
  Step 5: Create Specific &lt;code&gt;.gitconfig&lt;/code&gt; Files
&lt;/h2&gt;

&lt;p&gt;Create the local &lt;code&gt;.gitconfig&lt;/code&gt; files referenced in Step 4. These files should only contain the information that overrides the global configuration.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;File: &lt;code&gt;~/Documents/personal/.gitconfig&lt;/code&gt;&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ini"&gt;&lt;code&gt;&lt;span class="nn"&gt;[user]&lt;/span&gt;
    &lt;span class="py"&gt;name&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s"&gt;Your Personal Name&lt;/span&gt;
    &lt;span class="py"&gt;email&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s"&gt;your-personal-email@example.com&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;File: &lt;code&gt;~/Documents/work/.gitconfig&lt;/code&gt;&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ini"&gt;&lt;code&gt;&lt;span class="nn"&gt;[user]&lt;/span&gt;
    &lt;span class="py"&gt;name&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s"&gt;Your Work Name&lt;/span&gt;
    &lt;span class="py"&gt;email&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s"&gt;your-work-email@example.com&lt;/span&gt;
    &lt;span class="c"&gt;# Optional: signingkey = WORK_GPG_KEY
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






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

&lt;p&gt;This approach separates your identities and eliminates common errors when the same computer is used for multiple contexts. You can verify which configuration is active at any time by running:&lt;br&gt;
&lt;/p&gt;

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

&lt;/div&gt;



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