DEV Community

Cover image for Multiple GitHub accounts on the same computer?
Hanzala Ansari
Hanzala Ansari

Posted on

Multiple GitHub accounts on the same computer?

Have you ever struggled with managing multiple GitHub accounts (work and personal) on single same machine?

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.

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

We can achieve this by using gh-cli and some lesser known git configuration.

Set Up Automatic Git config Switching

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

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.

Here are the steps to setup:

1. Install Git

2. Global configuration (for personal account):

git config --global user.name "personal-username"
git config --global user.email "personal-email@personal.com"
Enter fullscreen mode Exit fullscreen mode

This sets the personal Git configuration for any folder unless overridden by a local configuration.

3. Work configuration (for work account inside ~/WORK/):

You need to create a .gitconfig file inside ~/WORK/ and set the username and email specifically for this path.

  • Create a .gitconfig-work file:
touch ~/.config/git/.gitconfig-work
Enter fullscreen mode Exit fullscreen mode
  • Add the following content to this file:
[user]
    name = "Your Work GitHub Username"
    email = "yourworkemail@example.com"
Enter fullscreen mode Exit fullscreen mode

4. Set up a conditional include:

Now, we need to include this ~/.config/git/.gitconfig-work when you're inside ~/WORK/**.
Add this to your global ~/.gitconfig:

git config --global includeIf.gitdir:~/WORK/.path ~/.config/git/.gitconfig-work
Enter fullscreen mode Exit fullscreen mode

This will ensure that when you are working inside any folder within ~/WORK/**, Git will use your work credentials.

Set Up Automatic GitHub context Switching:

1. Install gh-cli.

2. Authenticate Git with your GitHub credentials twice using gh-cli:

Image description

gh auth login # Once with your work account
gh auth login # Again with your personal account
Enter fullscreen mode Exit fullscreen mode

3. Set up automatic Context Switching for GitHub Auth token:

These command with automatically update the gh context for you based on the pwd in your shell:

  • ~/.bashrc:
# Function to get the current active GitHub username (if needed)
gh_active_user() {
    gh auth status -a 2>/dev/null | awk '/Logged in to github.com account/ {print $7}' || echo "No User"
}

# Function to check current directory and switch GitHub user silently
switch_gh_user() {
    if [[ "$PWD" == "$HOME/WORK"* ]]; then
        gh auth switch -u work-username >/dev/null 2>&1
    else
        gh auth switch -u personal-username >/dev/null 2>&1
    fi
    # Uncomment the following line to check if it's working
    # echo $(gh_active_user)
}

# Run switch_gh_user function before each prompt
PROMPT_COMMAND="switch_gh_user; history -a; $PROMPT_COMMAND"
Enter fullscreen mode Exit fullscreen mode
  • For ~/.zshrc:
# Function to get the current active GitHub username (if needed)
gh_active_user() {
    gh auth status -a 2>/dev/null | awk '/Logged in to github.com account/ {print $7}' || echo "No User"
}

# Function to check current directory and switch GitHub user silently
switch_gh_user() {
    if [[ "$PWD" == "$HOME/WORK"* ]]; then
        gh auth switch -u work-username >/dev/null 2>&1
    else
        gh auth switch -u personal-username >/dev/null 2>&1
    fi
    # Uncomment the following line to check if it's working 
    # echo $(gh_active_user) # Uncommenting this line with make the shell response slower as _gh_active_user_ function searches string
}

precmd_functions+=(switch_gh_user)
Enter fullscreen mode Exit fullscreen mode

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.
The above setup works with vscode source control as well as the shell (zsh, bash).

Thank you!

Top comments (3)

Collapse
 
gregharis profile image
Grëg Häris

Welcome to Dev Hanzala.

Great contribution. Keep up the amazing job 🦾

Collapse
 
asimaries profile image
Hanzala Ansari

Thank you 😄

Collapse
 
asimaries profile image
Hanzala Ansari

This is my first blog, any improvement, or more convenient ideas towards the topic or my blog writing would be appreciated...