Skip to content

Instantly share code, notes, and snippets.

@oanhnn
Last active February 11, 2025 20:16
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
@Mtillmann
Copy link

Thanks a ton for this! I had weird issues where ssh -T git@github.com -vvv and ssh -T git@github-somethingelse -vvv both would say that they use the specified key but I got identified with the my default username on both. The solution was to create a new key and add it to the second account.

@danish9811
Copy link

will the purpose be the same for multiple bitbucket accounts, and what if we have two github and two bitbucket accounts

@zyend
Copy link

zyend commented Dec 22, 2022

Thank you!

@danish9811
Yes, I just add it to the config with the right settings.

@gurcankavakci-esen
Copy link

I had this working for a long time then suddenly today my configs stopped working.

I had to re-run ssh-add ~/.ssh/.... cmds again to properly switch between accounts. Any idea why I had to re-run ssh-add commands?

Same issue.

@abymathewtranzmeo
Copy link

abymathewtranzmeo commented Jan 13, 2023

Thanks man, I think this is the best post about this topic.

@antonga23
Copy link

You're doing the lord's work king 👑

@AlanDeikman
Copy link

Saved my life.

@kstryjewski-1
Copy link

Works, thanks :)

@JeanMeche
Copy link

Thx Bro for writing & sharing that !

@vvrmatos
Copy link

Kudos to the instructions. They work as a charm!!

@sebb-m0
Copy link

sebb-m0 commented Mar 1, 2023

👍 thx for this.

@1manfactory
Copy link

You should use this to collect the public key from GitHub.
ssh-keyscan github.com >> ~/.ssh/known_hosts
No more error: The authenticity of host 'github.com (192.30.252.1)' can't be established.

@awesomeandrey
Copy link

that's marvelous guide - it works

@chhapanbhogi
Copy link

Excellent post .

@chenxiao218
Copy link

chenxiao218 commented Mar 12, 2023

git remote set-url origin git@github-personal:user/repo-name.git

This works to me, thanks.

@gabrielbissey
Copy link

Very helpful. Thanks!

@webThreeBuilder
Copy link

If you already have the repo set up, after the ssh config instructions, just do:

  • Remote is called probably origin
  • I have work and personal accounts, for this I used git@github-personal as host from your SSH config
  • user/repo-name.git for the Github repository

$ git remote set-url origin git@github-personal:user/repo-name.git

Hi, thanks for your good tips 👍, its worked for my case by your great comment, after following the steps above, we also have to reset the remote origin url with our custom "git-acount-host" in the config

@abdeljabarTaoufikallahPro

Thanks man

@akhill4054
Copy link

Thank you man! You are a life saver!

@datatravelandexperiments

Thank you! Note that if you have ssh multiplexing set up globally, especially with persistence, you may want to disable it for github, since multiplexing works per-machine and not per Host entry. Add ControlMaster no to each Host configuration.

@silicakes
Copy link

This is golden, thanks!

@aicals
Copy link

aicals commented Jun 2, 2023

Pure gold! Thanks!

@mahe113vsp
Copy link

thank you!

@cognivator
Copy link

@jdvivar Many thanks. This host customization works great for tools like SourceTree in which you don't enter the ssh host alias directly.

@kqfu
Copy link

kqfu commented Jun 17, 2023

Great post. This is by far the cleanest solution I've found.

@aacassandra
Copy link

great! thankyou sir

@rohanrmallya
Copy link

Neat! 🔥 Thanks for this. :)

@odooerpdevelopers
Copy link

odooerpdevelopers commented Aug 16, 2023

$

Thanks bro, I had problems with this configuration since I work with docker and docker recommends these settings to use remote connections, but in github I had to uncomment those lines and it works fine :)) (~/.ssh/config)
#ControlMaster auto
#ControlPath ~/.ssh/control-%C
#ControlPersist yes

@ImadMAKS
Copy link

Thank you for this post; very clear and helpful!
I have a question regarding the host, though. When I change the origin host on my local repo, shouldn't I change it remotely in my GitHub account? I can't figure out how, though.
Maybe I am missing something, but I tried changing it only locally, and I tried to fetch I get from the same repo and I still get:

ERROR: Repository not found.
fatal: Could not read from remote repository.
Please make sure you have the correct access rights, and the repository exists.

I'm wondering what I'm missing.

My config file contains the following:

Host git@github.com
        HostName github.com
	User git
	IdentityFile ~/.ssh/X.pub
	IdentitiesOnly yes
Host git@github.com-work
	HostName github.com
	User git
	IdentityFile ~/.ssh/Y.pub
	IdentitiesOnly yes

I changed my local git repo's URL to git@github.com-work:username/an-example.git

@oanhnn
Copy link
Author

oanhnn commented Aug 18, 2023

@ImadMAKS
Please using Host github.com and Host githut.com-work instead of Host git@github.com and Host git@github.com-work in your config.
After that, you need change remote URL of all repository for work.
You can use below command:

$ git remote set-url origin git@github.com-work:username/an-example.git

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