A command-line tool for managing multiple GitHub accounts on the same machine. Seamlessly switch between different Git configurations, SSH keys, and GitHub CLI authentication contexts.
- Installation
- Quick Start
- Features
- Usage
- Configuration
- SSH Setup
- Auto-Switching
- Troubleshooting
- Requirements
- License
Install globally via npm:
Get started in less than 2 minutes:
That's it! The tool handles:
- ✅ Git config (user.name, user.email)
- ✅ SSH key management
- ✅ GitHub CLI authentication (optional)
Optional: Enable auto-switching based on directory:
- Git Configuration Management - Automatically sets user.name and user.email
- SSH Key Switching - Manages SSH keys in ssh-agent
- GitHub CLI Integration - Authenticates gh CLI when tokens are provided
- Directory-Based Auto-Detection - Automatically switches accounts based on current directory
- Interactive Account Selection - User-friendly menu for account switching
- Shell Integration - Optional auto-switching on directory change
- Safe SSH Config Management - Non-destructive updates to ~/.ssh/config
When you run ghswap add, you'll be prompted for:
- Account name (e.g., "work", "personal")
- Git username (e.g., "John Doe")
- Git email (e.g., "[email protected]")
- GitHub username (e.g., "johndoe")
- Project directory (e.g., "~/projects/work")
The tool will then:
- Generate an SSH keypair automatically
- Add the account to your configuration
- Update your SSH config with the host alias
- Create directory mapping for auto-switching
- Display the public key for adding to GitHub
- (macOS only) Copy the public key to clipboard
Configuration is stored in ~/.ghswap.json:
- accounts - Array of GitHub account configurations
- name - Unique identifier for the account
- gitName - Git user.name value
- gitEmail - Git user.email value
- sshKey - Path to SSH private key
- ghToken - (Optional) GitHub CLI authentication token
- directoryMappings - Maps directories to accounts for auto-switching
- defaultAccount - Account to use when no directory mapping matches
Generate SSH configuration entries for all accounts:
This command will:
- Generate SSH config entries for each account
- Back up your existing ~/.ssh/config
- Add a managed block to your SSH config
- Display clone URLs for each account
The generated SSH config uses host aliases:
This approach is safe to run multiple times - only the managed block is replaced, preserving your other SSH configurations.
Check which SSH keys are missing:
Or generate keys manually:
Add the public keys to their respective GitHub accounts:
Simple Method (Recommended):
Just switch to your account first, then use normal GitHub URLs:
Advanced: Using Host Aliases (Optional)
If you prefer, you can use host aliases to specify the account in the clone URL:
This is useful if you want to clone repos for multiple accounts without switching, but most users won't need this.
Switch accounts based on the current directory:
For automatic switching when changing directories:
-
Generate the shell hook:
-
Add the output to your shell configuration:
# For zsh echo "$(ghswap setup)" >> ~/.zshrc # For bash echo "$(ghswap setup)" >> ~/.bashrc -
Reload your shell:
source ~/.zshrc # or source ~/.bashrc
Now accounts will switch automatically:
Problem: SSH key not working
Solution:
Problem: Git config not switching
Solutions:
- Verify you're in a git repository for local config changes
- Check current settings: git config --list
- Try switching with global scope explicitly
Problem: Directory-based auto-switching not working
Solutions:
- Ensure directory mappings use absolute paths or ~ prefix
- Verify shell hooks are properly installed
- Reload your shell after adding hooks: source ~/.zshrc
- Check that directory mappings in ~/.ghswap.json match your project paths
Problem: Permission denied when accessing SSH keys
Solution:
- Node.js >= 14.0.0
- Git - For git configuration management
- SSH - For SSH key management
- GitHub CLI (optional) - For gh authentication
-
Protect your configuration file:
echo ".ghswap.json" >> ~/.gitignore_global -
Never commit tokens - The ghToken field is optional and should be used with caution
-
Use strong SSH keys - The tool generates ed25519 keys by default, which are secure and modern
-
Regular key rotation - Periodically update your SSH keys and remove old ones from GitHub
Issues and pull requests are welcome at github.com/shubhamV123/ghswap.
MIT License - see LICENSE file for details.
Shubham Verma
- Report issues: GitHub Issues
- Documentation: GitHub Repository
.png)


