Managing Multiple GitHub Accounts
After trying to host Git repositories on a VPS, I have decided to sign up a private GitHub account for personal use. I tried Gitea first, and then its parent, Gogs. I managed to run Gogs and access its repositories via SSH, but its quality was not satisfactory. Even though the user interface of Gogs tries to mimic that of GitHub, it is poorly implemented. It raises several unnecessary warnings, and it is not responsive. It also lacks a way to authenticate an entire site. It would be nice if it allows only authenticated users to view repositories, but it just supports private repositories.
And all the other self-hosting options don’t look attractive. GitLab CE requires too much resource to run on a VPS. Other solutions are not as polished as GitHub.
I just want a GitHub, i.e. a repository hosting service that looks like GitHub, for making experiments and developing projects in private. GitHub already offers one for $7 / month. It is the GitHub and still inexpensive. It seems to be an obvious solution. Therefore I signed up for it.
I already had a free GitHub account, but instead of upgrading it, I signed up for a new one. I used the previous account for contributions (only little contributions, but any history matters), and I didn’t want mess it with personal experiments. I believe it is a good idea to make experiments in an isolated breakable environment.
Creating a GitHub account
Create a new account on GitHub as everyone does.
Configuration and Usage
SSH configuration for multiple GitHub accounts
In order to interact with multiple GitHub accounts over SSH from one computer, you have to tweak your SSH configuration and also edit your remote repository URLs. This workaround is explained in several places like this blog post or this gist, so I recommend that you should read through one of them.
Basically, you have to generate SSH key pairs for different GitHub accounts and declare them in your ~/.ssh/config
. I put the following content in my SSH client configuration:
# my new github account
Host github.com gist.github.com
IdentityFile ~/.ssh/jingsi
# my original github account
Host github-oss
HostName github.com
IdentityFile ~/.ssh/id_rsa
In this setup, I can access my new GitHub account just as in the normal workflow. In order to push commits to a repository of my previous account, I have to rewrite its remote URL from github.com
to github-oss
. My new account is the default at home.
Using hub
to create a remote repository
hub is a command line program that adds support for GitHub to git
command. You can use it by aliasing git
command to hub
in your shell profile:
alias git=hub
hub
has an additional command create
which creates a repository on GitHub (and add it as a remote of the local repository). Its synopsis is defined as follows:
git create [NAME] [-p] [-d DESCRIPTION] [-h HOMEPAGE]
A few notes on its usage:
- The repository name (
NAME
) is optional. If it is omitted, it defaults to the current working directory name. - You can make the remote repository private by specifying
-p
option.
For details, read the man page of hub.
Making a new repository private by default
During my experiments, I want to make my repositories private by default.
I looked for an option to make -p
(private repository) flag the default, but it turned out that it was not supported.
Instead, I defined an alias in my global git config (~/.config/git/config
):
[alias]
save = "!hub create -p $@"
That is, you can create a private repository using git save
command. In fact, this is a safer way, as you just fail to create a remote repository with this command if the alias is not defined. If you rely on default options, there is a risk of misbehavior on a machine where your configuration file is not installed. Then, repositories are created as public, and you may not notice it for a long time, which can be a little shame… I don’t want such a situation to happen.
Using multiple accounts is not supported by hub
Unfortunately, you cannot create repositories on different GitHub accounts using hub create
from the same computer. “The same computer” means the same user on a single computer in this context. I am looking for a solution/workaround, but I have not reached one. You can anyway create a repository via the web site of GitHub, and you can interact with the GitHub server using plain git
commands after rewriting repository URLs, so this is not a fatal issue.
Conclusion
GitHub is the most popular code hosing service in the world. You can host unlimited public repositories for free, and its paid plan for private repositories is affordable for personal use.
With the SSH workaround, you can even push commits to multiple GitHub accounts from a single computer. However, it does not work when you interact with GitHub using hub
client. In that case, you can stick with workflow with normal git
commands.