<?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: Hanzala Ansari</title>
    <description>The latest articles on DEV Community by Hanzala Ansari (@asimarise).</description>
    <link>https://dev.to/asimarise</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%2F1024904%2Fce045486-478a-49f8-81fc-4581f681e23d.jpg</url>
      <title>DEV Community: Hanzala Ansari</title>
      <link>https://dev.to/asimarise</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/asimarise"/>
    <language>en</language>
    <item>
      <title>Multiple GitHub accounts on the same computer without SSH?</title>
      <dc:creator>Hanzala Ansari</dc:creator>
      <pubDate>Fri, 27 Sep 2024 13:50:19 +0000</pubDate>
      <link>https://dev.to/asimarise/multiple-github-accounts-on-the-same-computer-2k15</link>
      <guid>https://dev.to/asimarise/multiple-github-accounts-on-the-same-computer-2k15</guid>
      <description>&lt;blockquote&gt;
&lt;p&gt;Have you ever struggled with managing multiple GitHub accounts (work and personal) on single same machine?&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Juggling multiple GitHub accounts can be a hassle, especially when contributing to both personal and professional projects with a single same machine. This configuration provides a seamless solution to manage your personal and professional GitHub accounts using the GitHub CLI (gh) and Git, with automatic context switching based on the current directory.&lt;/p&gt;

&lt;p&gt;Suppose, you have a &lt;strong&gt;~/WORK/&lt;/strong&gt;** directory in your file system inside which for every directories or repositories you must want to use your work &lt;em&gt;Git config&lt;/em&gt; and &lt;em&gt;GitHub credentials&lt;/em&gt;.&lt;br&gt;
And for the directories or repositories which are outside the &lt;strong&gt;~/WORK/&lt;/strong&gt;** directory you want to use your personal &lt;em&gt;Git config&lt;/em&gt; and &lt;em&gt;GitHub credentials&lt;/em&gt;. (OR vice-versa)&lt;/p&gt;

&lt;p&gt;We can achieve this by using &lt;a href="https://cli.github.com" rel="noopener noreferrer"&gt;gh-cli&lt;/a&gt; and some lesser known git configuration.&lt;/p&gt;
&lt;h2&gt;
  
  
  Set Up Automatic Git config Switching
&lt;/h2&gt;

&lt;blockquote&gt;
&lt;p&gt;⚠️⚠️⚠️ Before we move forward, it is a generous advice to backup all your config files. (such as ~/.gitconfig, ~/.zshrc, ~/.bashrc, etc.,)&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;You can configure Git to use different usernames and emails based on the folder by setting up a global configuration for your personal GitHub account and an override configuration file for your work GitHub account in the specific folder.&lt;/p&gt;
&lt;h2&gt;
  
  
  Here are the steps to setup:
&lt;/h2&gt;
&lt;h4&gt;
  
  
  1. Install &lt;a href="https://git-scm.com/downloads" rel="noopener noreferrer"&gt;Git&lt;/a&gt;
&lt;/h4&gt;
&lt;h4&gt;
  
  
  2. &lt;strong&gt;Global configuration (for personal account):&lt;/strong&gt;
&lt;/h4&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;git config &lt;span class="nt"&gt;--global&lt;/span&gt; user.name &lt;span class="s2"&gt;"personal-username"&lt;/span&gt;
git config &lt;span class="nt"&gt;--global&lt;/span&gt; user.email &lt;span class="s2"&gt;"personal@email.com"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;This sets the personal Git configuration for any folder unless overridden by a local configuration.&lt;/p&gt;
&lt;h4&gt;
  
  
  3. &lt;strong&gt;Work configuration (for work account inside &lt;code&gt;~/WORK/&lt;/code&gt;):&lt;/strong&gt;
&lt;/h4&gt;

&lt;p&gt;You need to create a &lt;code&gt;.gitconfig-work&lt;/code&gt; file inside &lt;code&gt;~/.config/git/&lt;/code&gt; and set the username and email specifically for this path.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Create a &lt;code&gt;.gitconfig-work&lt;/code&gt; file:
&lt;/li&gt;
&lt;/ul&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; ~/.config/git/.gitconfig-work
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;ul&gt;
&lt;li&gt;Add the following content to this file:
&lt;/li&gt;
&lt;/ul&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;"work-username"&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;"work@email.com"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;h4&gt;
  
  
  4. &lt;strong&gt;Set up a conditional include:&lt;/strong&gt;
&lt;/h4&gt;

&lt;p&gt;Now, we need to include this &lt;code&gt;~/.config/git/.gitconfig-work&lt;/code&gt; when you're inside &lt;code&gt;~/WORK/**&lt;/code&gt;.&lt;br&gt;
Add this to your global &lt;code&gt;~/.gitconfig&lt;/code&gt;:&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 &lt;span class="nt"&gt;--global&lt;/span&gt; includeIf.gitdir:~/WORK/.path ~/.config/git/.gitconfig-work
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This will ensure that when you are working inside any folder within &lt;code&gt;~/WORK/**&lt;/code&gt;, Git will use your work credentials.&lt;/p&gt;

&lt;h2&gt;
  
  
  Set Up Automatic GitHub context Switching:
&lt;/h2&gt;

&lt;h4&gt;
  
  
  1. Install &lt;a href="https://cli.github.com" rel="noopener noreferrer"&gt;gh-cli&lt;/a&gt;.
&lt;/h4&gt;

&lt;h4&gt;
  
  
  2. Authenticate Git with your GitHub credentials twice using &lt;strong&gt;gh-cli&lt;/strong&gt;:
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;gh auth login &lt;span class="c"&gt;# Once with your work account&lt;/span&gt;
gh auth login &lt;span class="c"&gt;# Again with your personal account&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fkvo1zuc2n7gu2369jaei.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fkvo1zuc2n7gu2369jaei.png" alt=" " width="687" height="214"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h4&gt;
  
  
  3. Set up automatic Context Switching for GitHub Auth token:
&lt;/h4&gt;

&lt;blockquote&gt;
&lt;p&gt;These command with automatically update the gh context for you based on the pwd in your shell:&lt;/p&gt;
&lt;/blockquote&gt;

&lt;ul&gt;
&lt;li&gt;~/.bashrc:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="c"&gt;# Current active GitHub username (if needed)&lt;/span&gt;
gh_active_user&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    gh auth status &lt;span class="nt"&gt;-a&lt;/span&gt; 2&amp;gt;/dev/null | &lt;span class="nb"&gt;awk&lt;/span&gt; &lt;span class="s1"&gt;'/Logged in to github.com account/ {print $7}'&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"No User"&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;

&lt;span class="c"&gt;# Switch GitHub user silently&lt;/span&gt;
switch_gh_user&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="o"&gt;[[&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="nv"&gt;$PWD&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="nv"&gt;$HOME&lt;/span&gt;&lt;span class="s2"&gt;/WORK"&lt;/span&gt;&lt;span class="k"&gt;*&lt;/span&gt; &lt;span class="o"&gt;]]&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="k"&gt;then
        &lt;/span&gt;gh auth switch &lt;span class="nt"&gt;-u&lt;/span&gt; work-username 2&amp;gt;/dev/null
    &lt;span class="k"&gt;else
        &lt;/span&gt;gh auth switch &lt;span class="nt"&gt;-u&lt;/span&gt; personal-username 2&amp;gt;/dev/null
    &lt;span class="k"&gt;fi&lt;/span&gt;
    &lt;span class="c"&gt;# Uncomment the following line to check if user changed&lt;/span&gt;
    &lt;span class="c"&gt;# echo $(gh_active_user)&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;

&lt;span class="c"&gt;# Run switch_gh_user function before each prompt&lt;/span&gt;
&lt;span class="nv"&gt;PROMPT_COMMAND&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"switch_gh_user; history -a; &lt;/span&gt;&lt;span class="nv"&gt;$PROMPT_COMMAND&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;For ~/.zshrc:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="c"&gt;# Current active GitHub username (if needed)&lt;/span&gt;
gh_active_user&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    gh auth status &lt;span class="nt"&gt;-a&lt;/span&gt; 2&amp;gt;/dev/null | &lt;span class="nb"&gt;awk&lt;/span&gt; &lt;span class="s1"&gt;'/Logged in to github.com account/ {print $7}'&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"No User"&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;

&lt;span class="c"&gt;# Switch GitHub user silently&lt;/span&gt;
switch_gh_user&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="o"&gt;[[&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="nv"&gt;$PWD&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="nv"&gt;$HOME&lt;/span&gt;&lt;span class="s2"&gt;/WORK"&lt;/span&gt;&lt;span class="k"&gt;*&lt;/span&gt; &lt;span class="o"&gt;]]&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="k"&gt;then
        &lt;/span&gt;gh auth switch &lt;span class="nt"&gt;-u&lt;/span&gt; work-username 2&amp;gt;/dev/null
    &lt;span class="k"&gt;else
        &lt;/span&gt;gh auth switch &lt;span class="nt"&gt;-u&lt;/span&gt; personal-username 2&amp;gt;/dev/null
    &lt;span class="k"&gt;fi&lt;/span&gt;
    &lt;span class="c"&gt;# Uncomment the following line to check if user changed&lt;/span&gt;
    &lt;span class="c"&gt;# echo $(gh_active_user) # Uncommenting this line with make the shell response slower as _gh_active_user_ function searches string&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;

precmd_functions+&lt;span class="o"&gt;=(&lt;/span&gt;switch_gh_user&lt;span class="o"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;These commands help you to automatically switch GitHub auth profile providing you a seamless transition from your personal project to work project OR vice-versa.&lt;br&gt;
The above setup works with &lt;strong&gt;&lt;em&gt;vscode source control&lt;/em&gt;&lt;/strong&gt; as well as the shell (zsh, bash).&lt;/p&gt;

&lt;p&gt;Thank you!&lt;/p&gt;

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