π Managing Multiple GitHub Accounts (Work & Personal) β
This guide outlines the recommended way to manage multiple GitHub accounts (e.g. personal and work) on the same machine using SSH keys, Git configuration, and SSH aliasing.
π§± Prerequisites β
- Git installed (
git --version) - SSH installed (
ssh -V) - GitHub accounts: e.g.,
personalandwork
π Step 1: Generate SSH Keys for Each Account (Choose RSA or ED25519) β
Choose one key type (ED25519 is recommended unless your system doesnβt support it).
β Option 1: ED25519 (recommended) β
bash
# Personal account
ssh-keygen -t ed25519 -C "[email protected]" -f ~/.ssh/id_ed25519_personal
# Work account
ssh-keygen -t ed25519 -C "[email protected]" -f ~/.ssh/id_ed25519_workπ‘οΈ Option 2: RSA (for legacy support) β
bash
# Personal account
ssh-keygen -t rsa -b 4096 -C "[email protected]" -f ~/.ssh/id_rsa_personal
# Work account
ssh-keygen -t rsa -b 4096 -C "[email protected]" -f ~/.ssh/id_rsa_workπ οΈ Step 2: Add SSH Keys to the SSH Agent β
bash
eval "$(ssh-agent -s)"
# For ED25519
ssh-add ~/.ssh/id_ed25519_personal
ssh-add ~/.ssh/id_ed25519_work
# Or for RSA
ssh-add ~/.ssh/id_rsa_personal
ssh-add ~/.ssh/id_rsa_workπ Step 3: Configure ~/.ssh/config β
bash
# ~/.ssh/config
# Personal GitHub
Host github-personal
HostName github.com
User git
IdentityFile ~/.ssh/id_ed25519_personal # or id_rsa_personal
IdentitiesOnly yes
# Work GitHub
Host github-work
HostName github.com
User git
IdentityFile ~/.ssh/id_ed25519_work # or id_rsa_work
IdentitiesOnly yesπ Step 4: Add Public Keys to GitHub β
bash
cat ~/.ssh/id_ed25519_personal.pub # or id_rsa_personal.pub
cat ~/.ssh/id_ed25519_work.pub # or id_rsa_work.pubπ§ Step 5: Clone Repos Using SSH Aliases β
bash
# Personal
git clone git@github-personal:your-username/personal-repo.git
# Work
git clone git@github-work:your-org/work-repo.gitβοΈ Step 6: Set Git User Per Repository β
bash
# Inside personal repo
git config --local user.name "Your Personal Name"
git config --local user.email "[email protected]"
# Inside work repo
git config --local user.name "Your Work Name"
git config --local user.email "[email protected]"Or use a global conditional setup in ~/.gitconfig:
bash
# ~/.gitconfig
[includeIf "gitdir:~/projects/work/"]
path = .gitconfig-work
[includeIf "gitdir:~/projects/personal/"]
path = .gitconfig-personalbash
# ~/.gitconfig-personal
[user]
name = Your Personal Name
email = [email protected]
# ~/.gitconfig-work
[user]
name = Your Work Name
email = [email protected]π§ͺ Test SSH Connections β
bash
ssh -T [email protected]
ssh -T [email protected]