Managing multiple GitHub accounts on a single machine can be tricky, but it's often necessary for developers who maintain separate personal and work accounts. This guide will walk you through the process of setting up multiple GitHub accounts using SSH keys, scoped gitconfig, and ZSH with Oh My Zsh for autoloading SSH keys.
Prerequisites
- Git installed on your machine
- Access to multiple GitHub accounts
- Basic knowledge of terminal/command line operations
- ZSH installed (preferred shell)
- Oh My Zsh installed (or another ZSH plugin manager of your choice)
Important: Backup Your Existing Configurations
Before proceeding with any changes, it's crucial to back up your existing configurations. This will allow you to revert changes if needed and prevent accidental loss of important settings.
- Create a backup directory:
mkdir ~/config_backups
- Backup your existing files:
cp ~/.zshrc ~/config_backups/.zshrc_backup cp ~/.gitconfig ~/config_backups/.gitconfig_backup cp ~/.ssh/config ~/config_backups/ssh_config_backup
- If you have existing SSH keys, back them up as well:
cp -r ~/.ssh/*.pub ~/config_backups/ cp -r ~/.ssh/id_* ~/config_backups/
Remember to keep these backups in a safe place. You can also consider using version control or a cloud storage solution for additional safety.
Step 1: Generate SSH Keys
For each GitHub account, you need to generate a unique SSH key.
- Open your terminal.
- Generate an SSH key for your first account:
ssh-keygen -t ed25519 -C "your-email@example.com" -f ~/.ssh/id_ed25519_github_account1
- Repeat the process for your second account:
ssh-keygen -t ed25519 -C "your-other-email@example.com" -f ~/.ssh/id_ed25519_github_account2
Step 2: Configure ZSH and Oh My Zsh for SSH Key Management
- Open your ZSH configuration file:
nano ~/.zshrc
- Add the ssh-agent plugin to your Oh My Zsh plugins list:
plugins=(... ssh-agent ...)
- Configure the ssh-agent plugin to manage your SSH keys. Add the following lines after the plugins list:
zstyle :omz:plugins:ssh-agent identities id_ed25519_github_account1 id_ed25519_github_account2 zstyle :omz:plugins:ssh-agent lifetime 4h
This configuration tells the ssh-agent plugin to automatically load your SSH keys and keep them in memory for 4 hours. - Save the file and exit the editor.
- Reload your ZSH configuration:
source ~/.zshrc
Now, your SSH keys will be automatically loaded when you start a new terminal session.
Step 3: Add SSH Keys to GitHub Accounts
- Copy the public key for the first account:
cat ~/.ssh/id_ed25519_github_account1.pub | pbcopy
- Log in to your first GitHub account.
- Go to Settings > SSH and GPG keys > New SSH key.
- Paste the copied key and save.
- Repeat steps 1-4 for your second account using
id_ed25519_github_account2.pub
.
Step 4: Create SSH Config File
- Create or edit the SSH config file:
nano ~/.ssh/config
- Add the following content:
# Account 1 Host github.com-account1 HostName github.com User git IdentityFile ~/.ssh/id_ed25519_github_account1 # Account 2 Host github.com-account2 HostName github.com User git IdentityFile ~/.ssh/id_ed25519_github_account2
Step 5: Set Up Scoped GitConfig
- Create a directory structure for your different Git accounts:
mkdir -p ~/git/personal mkdir -p ~/git/work
- Create a gitconfig file for each account:
touch ~/git/personal/.gitconfig touch ~/git/work/.gitconfig
- Edit the personal gitconfig (
~/git/personal/.gitconfig
):[user] name = Your Personal Name email = your-personal-email@example.com [github] user = your-personal-github-username [core] sshCommand = ssh -i ~/.ssh/id_ed25519_github_account1
- Edit the work gitconfig (
~/git/work/.gitconfig
):[user] name = Your Work Name email = your-work-email@example.com [github] user = your-work-github-username [core] sshCommand = ssh -i ~/.ssh/id_ed25519_github_account2
- Modify your global gitconfig (
~/.gitconfig
) to use these scoped configs:[includeIf "gitdir:~/git/personal/"] path = ~/git/personal/.gitconfig [includeIf "gitdir:~/git/work/"] path = ~/git/work/.gitconfig
Step 6: Clone Repositories
When cloning repositories, make sure to clone them into the appropriate directory:
# For personal projects
cd ~/git/personal
git clone git@github.com:personal_github_account/repo.git
# For work projects
cd ~/git/work
git clone git@github.com:company_github_account/repo.git
Git will automatically use the correct configuration based on the directory.
Step 7: Verify Setup
Test your SSH connections:
ssh -T git@github.com-account1
ssh -T git@github.com-account2
You should see a success message for each account.
Additional Tips
- You can still use the global gitconfig method for repository-specific configurations:
[url "git@github.com-account1:personal_github_account/"] insteadOf = git@github.com:personal_github_account/ [url "git@github.com-account2:company_github_account/"] insteadOf = git@github.com:company_github_account/
Add this to your global~/.gitconfig
if you need to override the SSH host for specific repositories. - Remember to always check which directory you're in when creating new repositories or cloning existing ones to ensure you're using the correct GitHub account.
- If you're using a different ZSH plugin manager, consult its documentation for the equivalent setup to autoload SSH keys.
- You can add aliases to your
~/.zshrc
file to quickly switch between personal and work directories:alias cdp="cd ~/git/personal" alias cdw="cd ~/git/work"
- Regularly backup your configuration files, especially after making significant changes or before system updates.
By following these steps, you can effectively manage multiple GitHub accounts on a single machine using SSH keys, scoped gitconfig, and ZSH with Oh My Zsh. This setup allows you to keep your work and personal projects separate while maintaining secure access to your repositories, using the correct user information for each account, and automatically loading your SSH keys for a smoother workflow.
Remember, if you encounter any issues during setup, you can always refer back to your backups to restore your previous configuration.