Git Config Switcher for Multiple GitHub Accounts
git-config-switcher.mdMarkdown
git-config-switcher.md
Git Config Switcher for Multiple GitHub Accounts
Save this in your .zshrc
file to automatically switch between different GitHub accounts based on your project directory.
# Function to check current git configuration
# Returns the current git user.name and user.email
function check-git-config() {
local name=$(git config user.name)
local email=$(git config user.email)
echo "Current git config:"
echo " user.name: $name"
echo " user.email: $email"
# Return values for reuse
echo "$name:$email"
}
# Function to switch to personal GitHub account
# Updates git config and GitHub CLI authentication
function set-git-personal() {
local target_name="YOUR_NAME" # Replace with your name
local target_email="YOUR_PERSONAL_EMAIL" # Replace with your personal email
# Switch to personal GitHub account
gh auth switch --user YOUR_PERSONAL_GH_USERNAME # Replace with your GitHub username
# Set git config
local config_output=$(check-git-config)
local current_config=(${(s/:/)config_output[-1]})
local current_name=$current_config[1]
local current_email=$current_config[2]
if [[ "$current_name" != "$target_name" ]] || [[ "$current_email" != "$target_email" ]]; then
git config user.name "$target_name"
git config user.email "$target_email"
echo "Git config updated: user.name='$target_name', user.email='$target_email'"
fi
}
# Function to switch to work GitHub account
# Updates git config and GitHub CLI authentication
function set-git-work() {
local target_name="YOUR_NAME" # Replace with your name
local target_email="YOUR_WORK_EMAIL" # Replace with your work email
# Switch to work GitHub account
gh auth switch --user YOUR_WORK_GH_USERNAME # Replace with your work GitHub username
# Set git config
local config_output=$(check-git-config)
local current_config=(${(s/:/)config_output[-1]})
local current_name=$current_config[1]
local current_email=$current_config[2]
if [[ "$current_name" != "$target_name" ]] || [[ "$current_email" != "$target_email" ]]; then
git config user.name "$target_name"
git config user.email "$target_email"
echo "Git config updated: user.name='$target_name', user.email='$target_email'"
fi
}
# Auto-switching function that runs when changing directories
# Automatically switches git config based on project directory
function chpwd() {
local current_dir="$PWD"
if [[ -d ".git" ]]; then
if [[ "$current_dir" == "$HOME/path/to/work/projects"* ]]; then # Replace with your work projects path
set-git-work
else
set-git-personal
fi
fi
}
Setup Instructions
-
Copy paste the excerpt to your
.zshrc
file and replace values. -
Replace the placeholder values:
YOUR_NAME
: Your full nameYOUR_PERSONAL_EMAIL
: Your personal GitHub emailYOUR_WORK_EMAIL
: Your work GitHub emailYOUR_PERSONAL_GH_USERNAME
: Your personal GitHub usernameYOUR_WORK_GH_USERNAME
: Your work GitHub username$HOME/path/to/work/projects
: Path to your work projects directory
-
Save the modified code in your
.zshrc
file -
Reload your shell:
source ~/.zshrc
Features
- Automatically switches between work and personal GitHub accounts when changing directories
- Updates both git config and GitHub CLI authentication
- Shows current git configuration when changes are made
- Only runs in git repositories
- Prevents unnecessary config updates when values are already correct
Requirements
- Zsh shell
- GitHub CLI (
gh
) installed and configured with multiple accounts - Git installed
Updated: 2/3/2025