Skip to content

πŸš€ 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., personal and work

πŸ” 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).

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-personal
bash
# ~/.gitconfig-personal
[user]
  name = Your Personal Name
  email = [email protected]

# ~/.gitconfig-work
[user]
  name = Your Work Name
  email = [email protected]

πŸ§ͺ Test SSH Connections ​

Released under the MIT License.