A simple zsh plugin to switch between multiple GitHub user accounts. It automatically updates both your SSH configuration and GitHub CLI (gh) authentication.
- π Automatically updates SSH config to use the correct identity file
- π€ Switches
ghCLI authentication to the specified user - π Auto-switches based on git repository config (when you
cdinto a directory) - π Appoint users to repositories with full git config setup
- βοΈ Fully configurable user/key mappings
- β Validates user input and provides helpful error messages
- πΎ Creates automatic backups of your SSH config before making changes
- π¦ Follows the Zsh Plugin Standard
-
GitHub CLI (
gh) installed and authenticated for both usersgh auth login
-
SSH keys set up for both users at:
~/.ssh/id_rsa_dipodidae(or your preferred key name)~/.ssh/id_rsa_spend_cloud_tom(or your preferred key name)
-
SSH config (
~/.ssh/config) with a GitHub section:Host github.com HostName github.com User git IdentityFile ~/.ssh/id_rsa_dipodidae
The plugin uses a configurable mapping between GitHub usernames and SSH key paths. You can customize this in your .zshrc before loading the plugin.
If you don't provide a custom configuration, the plugin uses these defaults:
typeset -gA GUS_USER_KEYS GUS_USER_KEYS=( "dipodidae" "~/.ssh/dipodidae" "spend-cloud-tom" "~/.ssh/spend-cloud-tom" )Note: These paths match the repository author's setup. You should customize them to match your own SSH key locations.
To use your own user/key mappings, add this to your .zshrc before the plugin is loaded:
# Define your custom user-to-key mapping typeset -gA GUS_USER_KEYS GUS_USER_KEYS=( "dipodidae" "~/.ssh/dipodidae" "spend-cloud-tom" "~/.ssh/spend-cloud-tom" "another-user" "~/.ssh/another_user_key" ) # Define email-to-username mapping for auto-switching typeset -gA GUS_EMAIL_TO_USER GUS_EMAIL_TO_USER=( "dipodidae@users.noreply.github.com" "dipodidae" "tom@work.com" "spend-cloud-tom" "another@example.com" "another-user" ) # Optional: Disable auto-switching (enabled by default) # typeset -g GUS_AUTO_SWITCH=0 # Then load the plugin (example with Zi) zi light dipodidae/zsh-plugin-git-user-switchConfiguration Options:
GUS_USER_KEYS: Maps GitHub username β SSH key pathGUS_EMAIL_TO_USER: Maps git user.email β GitHub username (for auto-switching)GUS_USER_EMAILS: Maps GitHub username β git user.email (for appointing users)GUS_USER_NAMES: Maps GitHub username β git user.name (for appointing users)GUS_AUTO_SWITCH: Enable/disable auto-switching (1 = enabled, 0 = disabled, default: 1)
Note: The mapping format is:
- Key: GitHub username (must match your
ghCLI authenticated username) - Value: Path to SSH private key (can use
~for home directory)
Using Zi
zi light dipodidae/zsh-plugin-git-user-switchUsing Oh My Zsh
-
Clone the repository:
git clone https://github.com/dipodidae/zsh-plugin-git-user-switch.git \ ${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/plugins/git-user-switch -
Add to your
.zshrc:plugins=(... git-user-switch)
Using zplug
zplug "dipodidae/zsh-plugin-git-user-switch"-
Clone the repository:
git clone https://github.com/dipodidae/zsh-plugin-git-user-switch.git \ ~/.zsh/plugins/git-user-switch -
Source the plugin in your
.zshrc:source ~/.zsh/plugins/git-user-switch/git-user-switch.plugin.zsh
Use the gus-appoint command to set up a user for a specific repository with all necessary git config:
# Navigate to your repository cd ~/projects/my-work-repo # Appoint a user to this repository gus-appoint spend-cloud-tomThis command will:
- Set
git config user.nameanduser.emailfor the repository - Update your
~/.ssh/configto use the correct SSH key - Switch your
ghCLI authentication to the specified user
Perfect for setting up new repositories or switching ownership of existing ones!
For detailed information, see the Repository Appointment Guide.
Switch between users with the gus command:
# Switch to dipodidae account gus dipodidae # Switch to spend-cloud-tom account gus spend-cloud-tomThe plugin will:
- Update your
~/.ssh/configto use the correct SSH key - Switch your
ghCLI authentication to the specified user - Create a backup of your SSH config at
~/.ssh/config.bak
The plugin automatically switches users when you navigate to a git repository, based on the user.email in the git config:
# Set git email in your personal project cd ~/projects/personal-repo git config user.email "dipodidae@users.noreply.github.com" # Set git email in your work project cd ~/projects/work-repo git config user.email "tom@work.com" # Now when you cd between them, the plugin auto-switches! cd ~/projects/personal-repo # π Auto-switches to dipodidae cd ~/projects/work-project # π Auto-switches to spend-cloud-tomThe plugin provides the following commands:
Display brief usage information for all commands.
gus help # Show help gus --help # Also works gus -h # Also worksSwitch to a different GitHub user globally (SSH config and gh CLI).
gus dipodidae # Switch to dipodidae gus spend-cloud-tom # Switch to spend-cloud-tomWhat it does:
- Updates SSH config to use the user's SSH key
- Switches gh CLI authentication
- Updates internal user tracking
Appoint a user to the current git repository with complete configuration.
cd ~/projects/my-repo gus-appoint dipodidae # Set up repository for dipodidaeWhat it does:
- Sets
git config user.namefor the repository - Sets
git config user.emailfor the repository - Updates SSH config to use the user's SSH key
- Switches gh CLI authentication
- Updates internal user tracking
Requires: Must be run inside a git repository
See APPOINT-GUIDE.md for detailed usage.
By default, the plugin expects SSH keys at:
**How it works:** 1. When you `cd` into a directory, the plugin checks if it's a git repository 2. It reads the `git config user.email` value 3. It looks up the corresponding GitHub username in `GUS_EMAIL_TO_USER` 4. If a match is found and it's different from the current user, it auto-switches **To disable auto-switching:** ```zsh # In your .zshrc before loading the plugin typeset -g GUS_AUTO_SWITCH=0 By default, the plugin expects SSH keys at:
~/.ssh/id_rsa_dipodidae~/.ssh/id_rsa_spend_cloud_tom
If your keys are located elsewhere, you can modify the .gus_update_ssh_config function in git-user-switch.plugin.zsh:
case "${username}" in dipodidae) ssh_key_file="${HOME}/.ssh/your_custom_key_name" ;; spend-cloud-tom) ssh_key_file="${HOME}/.ssh/another_key_name" ;; esacInstall the GitHub CLI: https://cli.github.com/
Make sure both users are authenticated:
gh auth loginEnsure your SSH keys exist at the expected locations and have the correct permissions:
chmod 600 ~/.ssh/id_rsa_*Create a ~/.ssh/config file with a GitHub section:
mkdir -p ~/.ssh cat >> ~/.ssh/config << 'EOF' Host github.com HostName github.com User git IdentityFile ~/.ssh/id_rsa_dipodidae EOFThis plugin follows:
. βββ git-user-switch.plugin.zsh # Main plugin file βββ README.md # This file The plugin provides an unload function that can be called by plugin managers:
git_user_switch_plugin_unloadMIT License - feel free to use and modify as needed.
Contributions are welcome! Please feel free to submit a Pull Request.
Tom (@dipodidae)