Skip to content

Instantly share code, notes, and snippets.

@jesugmz
Last active July 17, 2024 10:11
Show Gist options
  • Save jesugmz/f66283f1b6f89dc354652ebd6f692f7b to your computer and use it in GitHub Desktop.
Save jesugmz/f66283f1b6f89dc354652ebd6f692f7b to your computer and use it in GitHub Desktop.
Configure SSH with multiple git accounts

Configure SSH with multiple git accounts

Scenario: same git provider, for this example GitHub (this trick is provider agnostic, has been tested with GitLab as well), and we want to have SSH configured for two different users (or more): jesugmz and myseconduser.

Step 1

Configure SSH config in order to get an alias with different configuration as follow:

$ cat ~/.ssh/config
Host github.com
        PubkeyAuthentication yes
        IdentityFile ~/.ssh/jesugmz_privatekey
        User jesugmz

Host myseconduser.github.com
        HostName github.com # Actually we are telling to SSH this is the real host; host directive act as alias
        PubkeyAuthentication yes
        IdentityFile ~/.ssh/myseconduser_privatekey
        User myseconduser

Step 2

In our second account repos (no extra config needed for the first one), configure git config in order to use our SSH alias:

$ cat .git/config
# ...
[remote "origin"]
	url = [email protected]:myseconduser/myrepo.git
# ...

Based on https://www.ostricher.com/2015/06/how-to-use-ssh-with-multiple-github-accounts/.

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