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.com"
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-work
file inside ~/.config/git/
and set the username and email specifically for this path.
- Create a
.gitconfig-work
file:
touch ~/.config/git/.gitconfig-work
- Add the following content to this file:
[user]
name = "work-username"
email = "work@email.com"
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
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:
gh auth login # Once with your work account
gh auth login # Again with your personal account
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:
# 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"
}
# Switch GitHub user silently
switch_gh_user() {
if [[ "$PWD" == "$HOME/WORK"* ]]; then
gh auth switch -u work-username 2>/dev/null
else
gh auth switch -u personal-username 2>/dev/null
fi
# Uncomment the following line to check if user changed
# echo $(gh_active_user)
}
# Run switch_gh_user function before each prompt
PROMPT_COMMAND="switch_gh_user; history -a; $PROMPT_COMMAND"
- For ~/.zshrc:
# 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"
}
# Switch GitHub user silently
switch_gh_user() {
if [[ "$PWD" == "$HOME/WORK"* ]]; then
gh auth switch -u work-username 2>/dev/null
else
gh auth switch -u personal-username 2>/dev/null
fi
# Uncomment the following line to check if user changed
# 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)
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 (7)
It is slightly different approach, but I prefer to use it when I should switch between work and own projects. Generally, I worked through ssh.
@asimaries simplest solution for this case was for me to have installed Visual Studio Code and Visual Studio Code - Insiders. Each connected to different Github account.
That's convenient too
I use to have a similar setup where I was managing 2 wsl distros
Welcome to Dev Hanzala.
Great contribution. Keep up the amazing job 🦾
Thank you 😄
This is my first blog, any improvement, or more convenient ideas towards the topic or my blog writing would be appreciated...