Configure Multiple Github Account With SSH

Generating SSH Keys

  • Generate SSH keys for the number of accounts with different name and email.
# Personal
ssh-keygen -t rsa -b 4096 -f ~/.ssh/<github_username>_github_personal -C "<[email protected]>"

# Work
ssh-keygen -t rsa -b 4096 -f ~/.ssh/<github_username>_github_work -C "<[email protected]>"
  • This will create public and private keys,

    • Personal Keys: ~/.ssh/<github_username>_github_personal.pub and ~/.ssh/<github_username>_github_personal
    • Works Keys: ~/.ssh/<github_username>_github_work.pub and ~/.ssh/<github_username>_github_work

Add public keys to GitHub

  • Login to github account
  • Copy your public ssh key (.pub extenion) and add it in your gihub account

    pbcopy < ~/.ssh/<github_username>_github_personal.pub
  • Create New SSH Key on Github and paste copied key
  • Repeat above two steps for all your github accounts

Add SSH Keys

ssh-add ~/.ssh/<github_username>_github_personal
ssh-add ~/.ssh/<github_username>_github_work
  • Delete all cached keys that are added by ssh-add

    ssh-add -D
  • Check all saved keys

    ssh-add -l

Modify/Create SSH Config file

  • If you don't have ~/.ssh/config file then you can create and set proper permission

    touch ~/.ssh/config
    chmod 644 ~/.ssh/config
  • Add following configuration in config file,

    Host personal.github.com
        HostName github.com
        User git
        PreferredAuthentications publickey
        IdentityFile <absolute_path_to_your_personal_github_private_ssh_key>
        UseKeychain yes # Comment this other than macOS user
        AddKeysToAgent yes
    
    Host work.github.com
        HostName github.com
        User git
        PreferredAuthentications publickey
        IdentityFile <absolute_path_to_your_work_github_private_ssh_key>
        UseKeychain yes # Comment this other than macOS user
        AddKeysToAgent yes
  • You can now use git remote URL by changing [email protected] by [email protected]
  • Every time you clone git repo using different git account then you need to use like this,

    git clone [email protected]:<username>/repository-name.git
    cd repo
    git config user.email "<email_address>"
  • You have to do this for every new repository. For existing repo,

    git remote set-url origin [email protected]:<username>/repository-name.git
  • If you clone repo like this, git clone [email protected]:<username>/repository-name.git then it will use your public ssh key id which will use personal email address.

Setting user.name and user.email for all projects

  • You will need specific folder for all your work repo and personal/OSS repo. For e.g., ~/personal/ and ~/work/
  • Modify git config file,

        # ...
        # Personal gitconfig
        [includeIf "gitdir:~/personal/"]
            path = ~/.gitconfig-personal
        # Work gitconfig
        [includeIf "gitdir:~/work/"]
            path = ~/.gitconfig-work
  • Create ~/.gitconfig-personal and ~/.gitconfig-work files and add,

        [user]
            email = <personal_email>
            name = <Name>

Alternate Way For Above Step

  • Set alias in ~/.gitconfig file,

    [alias]
    setworkmail = "config user.email '<[email protected]>'"
    setpersonalmail = "config user.email '<[email protected]>'"
  • From above alias we can do git setworkmail to change email this project only.

Single command to switch after cloning repo?

  1. Clone repo with normal URL that github provides
  2. Change remote host and email

    • To change remote host add alias in ~/.gitconfig

      [alias]
          changeremotehost = !sh -c \"git remote -v | grep '$1.*fetch' | sed s/..fetch.// | sed s/$1/$2/ | xargs git remote set-url\"

      Final alias in ~/.gitconfig file,

      [alias]
      setworkmail = "config user.email '<[email protected]>'"
      setpersonalmail = "config user.email '<[email protected]>'"
      changeremotehost = !sh -c \"git remote -v | grep '$1.*fetch' | sed s/..fetch.// | sed s/$1/$2/ | xargs git remote set-url\"
      work = !sh -c \"git changeremotehost github.com work.github.com && git setworkmail\"
      personal = !sh -c \"git changeremotehost github.com personal.github.com && git setpersonalmail\"

Output

git clone [email protected]:username/repo.git
cd repo
git work

Credits

Cheers!