Skip to content

Instantly share code, notes, and snippets.

@Trung-DV
Created February 20, 2025 04:35
Show Gist options
  • Save Trung-DV/6b0a2ee89f0264c1ae09838009055e2d to your computer and use it in GitHub Desktop.
Save Trung-DV/6b0a2ee89f0264c1ae09838009055e2d to your computer and use it in GitHub Desktop.
Use multiple ssh keys for multiple GitHub accounts

Use multiple SSH keys for multiple GitHub accounts seamlessly

1. Create and add SSH keys for each account

Follow guidelines from GitHub: https://docs.github.com/en/authentication/connecting-to-github-with-ssh/generating-a-new-ssh-key-and-adding-it-to-the-ssh-agent

2. Modify ssh and git config

  • Add ssh config
# Default GitHub
Host github.com
  HostName github.com
  User git
  IdentityFile ~/.ssh/id_rsa

# For cilium repositories
Host github-cilium
  HostName github.com
  User git
  IdentityFile ~/.ssh/cilium_id_rsa

# For Trung-DV repositories
Host github-trungdv
  HostName github.com
  User git
  IdentityFile ~/.ssh/trungdv_id_rsa
  • Add config to ~/.gitconfig
; Default gitaccount
[url "github.com:"]
  insteadOf = https://github.com
; Specific account for cilium's repositories
[url "github-cilium:cilium"]
  insteadOf = https://github.com/cilium
; Specific account for personal repositories
[url "github-trungdv:trungdv"]
  insteadOf = https://github.com/trung-dv

or

git config --global url."github.com:".insteadOf "https://github.com"
git config --global url."github-cilium:cilium".insteadOf "https://github.com/cilium"

From now on, you can clone repositories by using https url without worrying about the account you are using.

 $ git clone https://github.com/trung-dv/private
Cloning into 'private'...
remote: Enumerating objects: 928, done.
remote: Counting objects: 100% (748/748), done.
remote: Compressing objects: 100% (439/439), done.
remote: Total 928 (delta 439), reused 558 (delta 270), pack-reused 180 (from 1)
Receiving objects: 100% (928/928), 3.96 MiB | 7.34 MiB/s, done.
Resolving deltas: 100% (553/553), done.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment