Skip to content

Instantly share code, notes, and snippets.

@oanhnn
Last active October 3, 2024 11:21
Show Gist options
  • Save oanhnn/80a89405ab9023894df7 to your computer and use it in GitHub Desktop.
Save oanhnn/80a89405ab9023894df7 to your computer and use it in GitHub Desktop.
Using multiple github accounts with ssh keys

Problem

I have two Github accounts: oanhnn (personal) and superman (for work). I want to use both accounts on same computer (without typing password everytime, when doing git push or pull).

Solution

Use ssh keys and define host aliases in ssh config file (each alias for an account).

How to?

  1. Generate ssh key pairs for accounts and add them to GitHub accounts.

  2. Edit/Create ssh config file (~/.ssh/config):

    # Default github account: oanhnn
    Host github.com
       HostName github.com
       IdentityFile ~/.ssh/oanhnn_private_key
       IdentitiesOnly yes
       
    # Other github account: superman
    Host github-superman
       HostName github.com
       IdentityFile ~/.ssh/superman_private_key
       IdentitiesOnly yes
    

    NOTE: If you use any account frequently, you should use the default hostname (github.com).

  3. Add ssh private keys to your agent:

    $ ssh-add ~/.ssh/oanhnn_private_key
    $ ssh-add ~/.ssh/superman_private_key
  4. Test your connection

    $ ssh-keyscan github.com >> ~/.ssh/known_hosts
    $ ssh -T git@github.com
    $ ssh -T git@github-superman

    If everything is OK, you will see these messages:

    Hi oanhnn! You've successfully authenticated, but GitHub does not provide shell access.
    Hi superman! You've successfully authenticated, but GitHub does not provide shell access.
  5. Now all are set, you need remeber

    git@github-superman:org/project.git => user is superman
    git@github.com:org/project.git.     => user is oanhnn
    
  • If you need clone a repository, just do:
$ git clone git@github-superman:org1/project1.git /path/to/project1
$ cd /path/to/project1
$ git config user.email "superman@example.com"
$ git config user.name  "Super Man"
  • If you already have the repo set up, after the ssh config instructions, you need change the URL of origin, just do:
$ cd /path/to/project2
$ git remote set-url origin git@github-superman:org2/project2.git
$ git config user.email "superman@example.com"
$ git config user.name  "Super Man"
  • If you are creating a new repository on local:
$ cd /path/to/project3
$ git init
$ git remote add origin git@github-superman:org3/project3.git
$ git config user.email "superman@example.com"
$ git config user.name  "Super Man"
$ git add .
$ git commit -m "Initial commit"
$ git push -u origin master

Done! Goodluck!

Addon:

The bash script that prompts for your git account. Thank @davorpa

#!/bin/bash

# silent prompt
read -p 'GIT profile: ' profile

# switch
case $profile in
  superman)
    git config user.email "superman@example.com"
    git config user.name "superman" 
    git config user.signingKey "superman_gpg_public_key"
    ;;
  oanhnn)
    git config user.email "oanhnn@example.com"
    git config user.name "oanhnn" 
    git config user.signingKey "oanhnn_gpg_public_key"
    ;;
  # default case: raise error
  *)
    >&2 echo "ERR: Unknown profile: $profile"
    exit 1
esac
@David7ce
Copy link

David7ce commented Apr 3, 2024

@IbroRebronja yes that was the issue, thanks for explanation

@IvanildoBarauna
Copy link

Thanks @oanhnn!

@mobeigi
Copy link

mobeigi commented Aug 8, 2024

Seems like an okay solution, although its a bit annoying to have aliases like github_personal when it should clearly be github.com.

I believe other alternatives exist which may be nicer:

  • Aliases or commands to load / unload the right SSH key into your SSH agent
  • Aliases or commands to export GIT_SSH_COMMAND variable (see here)

That way, usage would be something like:

git_work   // trigger alias to set GIT_SSH_COMMAND to work SSH key
git clone git@github.com:Company/Project
git_me  // trigger alias to set GIT_SSH_COMMAND to personal SSH key
git clone git@github.com:Personal/Project

All the repos have the correct hostname, your .ssh config is correct and all you need to do is remember to trigger the alias as you hop between different projects. If you get it wrong you get permission denied or not found so its simple to then remember and switch to the right one. Feels more correct to me.

@oanhnn
Copy link
Author

oanhnn commented Aug 9, 2024

@mobeigi Great idea.
You can make a bash script git-me.sh

#!/usr/bin/env bash

SSH_PRIVATE_KEY=~/.ssh/personal_private_key.pem

GIT_SSH_COMMAND="ssh -i $SSH_PRIVATE_KEY -o IdentitiesOnly=yes" git $@

And use it for personal projects.

git clone git@github.com:Company/Project.git
git-me.sh clone git@github.com:Personal/Project.git

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment