Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save saulbaizman/360d0eb1a01873356bd0348cf0ed9a69 to your computer and use it in GitHub Desktop.
Save saulbaizman/360d0eb1a01873356bd0348cf0ed9a69 to your computer and use it in GitHub Desktop.
multiple github accounts, custom ssh hosts, and git url aliases

multiple github accounts + custom ssh hosts + git url aliases

As is often the case, this is written for the search engines, because when I tried to find a solution to this problem, I couldn't find one. (Perhaps that was because I wasn't searching with the right keywords, or perhaps I would have found the solution had I been more persistent.) I also understand that very few people will encounter this "problem" (read: require, or desire, this crazy advanced configuration).

Because services like GitHub and BitBucket require each user account to have unique SSH keys, if you have multiple accounts on those services, it's necessary to generate multiple SSH keys.

First, we'll generate the keys.

$ ssh-keygen -t rsa -f github-user1 # key for user1
$ ssh-keygen -t rsa -f github-user2 # key for user2

Then we'll add each public key to each account in the GitHub or BitBucket web interface per usual. Now, we'll have two problems to solve: when we try to pull and push commits to GitHub or BitBucket, SSH doesn't know how to select the correct key file. That's easy: we'll create a user SSH configuration file (~/.ssh/config) and create an entry like the one below.

Host github.com
IdentityFile ~/.ssh/github-user1

But how do we specify the user2 account? SSH allows us to create "fake" hostnames in its user configuration file. Think of them as domain "aliases."

Host user1.github.com
HostName github.com
IdentityFile ~/.ssh/github-user1

Host user2.github.com
HostName github.com
IdentityFile ~/.ssh/github-user2

We've now mapped each domain alias to each key. This comes with a trade-off: our git clone commands and all other references to our GitHub and BitBucket repository URLs needs a slight modification. Here's the typical syntax:

$ git clone [email protected]:user1/repo.git

Below is the new syntax.

$ git clone [email protected]:user1/repo.git

Note the "user1.github.com" as the domain.

But this is cumbersome and inconvenient—and unacceptable. Luckily, git's url aliases come to the rescue. They allow us to substitute some portion of the URL to be cloned for something else. Here's an example:

[url "user1.github.com:user1"]
    insteadOf = github.com:user1

What this does is substitute "github.com:user1" with "user1.github.com:user1" in the URL parameter of our git commands. A command like this:

$ git clone [email protected]:user1/repo.git

...is automagically translated into this:

$ git clone [email protected]:user1/repo.git

Yup, that's the same command we were trying to avoid running above—which we've successfully done. Git URL aliases live in ~/.gitconfig, and you may add as many as you like.

(Power user tip: GitHub and BitBucket allow you to omit the ".git" extension from the repository name.)

@adi1494
Copy link

adi1494 commented Mar 17, 2022

above way of url aliases didn't work for me, i think it keeps on substituting and becomes something like this:

user1.user1.user1.user1.user1.user1.github.com:user1

however, this worked:

[url "[email protected]:user1"]
    insteadOf = [email protected]:user1

@saulbaizman
Copy link
Author

@adi1494 Thanks for your comment. I'm not sure which version of git you're using, or what operating system you're using, but I've had no issues on macOS 10.15 (Catalina) and git 2.3X for a while (a couple years).

Another tip is to create (fake) short hostnames like so:

[url "user1.github.com:user1/"]
    insteadOf = gh1:

For this to work, the entry in .ssh/config needs to be revised:

Host user1.github.com gh1
HostName github.com
IdentityFile ~/.ssh/github-user1

Cloning a repository on the command line is now as easy as this:

$ git clone gh1:repo.git

@adi1494
Copy link

adi1494 commented Mar 18, 2022

@saulbaizman I think its because I'm running windows 10.

Thanks a lot for this post. It really helped me.

@saulbaizman
Copy link
Author

@adi1494 So happy to hear it! (I wish this process were more obvious; many people have multiple accounts.) Please let me know if you have any other comments or feedback. :-)

@15H44N
Copy link

15H44N commented Mar 19, 2022

[url "[email protected]:user1"]
    insteadOf = [email protected]:user1

@adi1494 This worked for me as well on Win11. Thank you 💯

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