Managing Multiple Accounts for GitHub and GitLab on the Same Machine
How-to manage multiple accounts for GitHub and GitLab on the same Linux machine with different SSH keys and e-mail addresses
Yes, I know you have more than a single Git account on the same machine… and I bet it turns you mad often. So do I: there are at least two different methods to manage them. Below, I’ll show you both (and I’ll explain which I prefer and why); especially if you need to handle multiple sub-repositories in a Git Flow way. It doesn’t really matter your workflow, neither your company role, but you may seriously consider to separate your local configuration. Otherwise, it will be confusing… and make you loose lots of precious time.
As a freelancer, I’ve never considered this use case. I used to work by myself on my clients’ projects, so I didn’t face the problem to split my GitHub account in two to separate my personal code from my company’s. Now that I’m working for a startup, and I have at least two colleagues at a time, I need to secure my SSH keys: plus, my company chose GitLab instead. That’s lucky, because the two platforms suggest different encoding algorithms and I don’t have to save keys with a potential naming conflict on Linux boxes.
GitHub + GitLab
By default, they set different names for their key pairs. You may want to use the same algorithm for every platform and this won’t change much, but you’ll need an extra step. While GitHub shows how-to generate an RSA key under Linux, etc. GitLab does the same with ED25519. In Bash, commands are quite identical, but you’ll get different encodings and names: the goal here is to have two different pairs connected to two different e-mail address. I guess you don’t use the same address for both your personal and work accounts.
I bet you use to configure your Git profile globally. I mean, who (on the same machine) need to do it locally? Lots of people, actually. If you’re into a BYOD company model, then your Linux box doesn’t change from home to the workplace and you store several account credentials there. Below, I’ll show you an example on how-to set your name and e-mail address locally to fit your needs. Remember that different combinations of them need different key pairs: you can’t use the same keys for both, when the e-mail address changes.
$ cd path/to/git/local/repository/
$ git config --local user.name "John Doe"
$ git config --local user.email "email@example.com"
Repeat this for every repository of yours, changing the e-mail address accordingly. Notice that this behaviour doesn’t affect your global Git configuration: for example, you may configure your primary account with the
global flag and the secondary with the
local flag where needed. This is useful if you have a few personal and lots of working projects (or the other way around). Otherwise, you’d turn mad to manage each configuration; I had three personal archive and just one working archive, so this was enough.
Without any flag, Git add your settings just locally. Remember that your key pair must be generated once with the same name and e-mail address you configured at first or it will fail. In these examples I assume that yours doesn’t expire: an expiration date makes it even harder and I think that removing the public key from online services is enough to prevent security issues — in my case, at least. Below, you can see what I did to pull and push from and to remote repositories with different accounts registered on GitHub and GitLab.
$ git config --global user.name "John Doe"
$ git config --global user.email "firstname.lastname@example.org"
$ ssh-keygen -t rsa -C "John Doe <email@example.com>" # GitLab
$ cd path/to/git/local/repository/
$ git config --local user.email "firstname.lastname@example.org"
$ ssh-keygen -t ed25519 -C "John Doe <email@example.com>"
Yes, I didn’t set
user.name twice, because it doesn’t change at all. I bet you have the same name and surname for both your personal and working life, but I could be wrong! Git falls back to the global configuration if a variable isn’t set locally. It won’t fail if you specify your full name here too: this is up to you. As I said, this solution is useful only if your repositories’ ratio is around 3:1, but if it’s closer to 1:1 this simply doesn’t work for you. That’s why I suggest a slightly different way to achieve the same result in more complex situations.
What if your remote repositories share the same algorithm? I mean… if you have, for hypothesis, two or more accounts on the same platform (it doesn’t matter which)? Here’s where the global
.gitconfig file does the trick. Instead of executing several commands, you can just edit it, filling all of your profiles details at once. I think it fits the needs of most among you: it’s even easier to maintain across different machines. Let’s see how-to manage multiple SSH keys this way, solving the problem definitively. The syntax is very simple too.
$ nano ~/.gitconfig [user]
name = John Doe
email = firstname.lastname@example.org [includeIf "gitdir:/path/to/git/local/repository/"]
path = /path/to/git/local/repository/.gitconfig$ nano /path/to/git/local/repository/.gitconfig [user]
email = email@example.com
Of course, the two e-mail addresses should be different from each other or it simply doesn’t make sense. But, as you may have noticed, I spoke about SSH… and I’ve updated just the Git configuration yet. While we got two public and two private keys earlier, there’s a chance a key share the same algorithm between multiple platforms — and naming become essential. So, assuming we have
id_rsa-2.pub, how is it possible to manage all of them in one place? Bet it has something to do with the
$ nano ~/.ssh/config Host alias-1
IdentitiesOnly yes Host alias-2
IdentitiesOnly yes$ git remote add alias-1 firstname.lastname@example.org:username-1/repository.git
$ git remote add alias-2 email@example.com:username-2/repository.git
Once you get how this schema works, you can play with different profiles and platforms. I’m a huge fan of the second option I’ve mentioned above, because most of my projects belongs to my work account and just a few to my personal’s right now. Anyway, I tried to summarise three useful approaches here. It would be interesting to start a flame (it was meant to be a strike-through text)… well, to know which of the available platforms do you prefer. I’m a big GitHub supporter, while my company chose to use GitLab instead.
So, I hope this could help you to better understand how-to get well with two or more Git accounts. I’m OK with these solutions and I hope you would be glad too: I also linked the main sources on StackOverflow where I got inspiration to solve my personal issues, because I think that years ago some colleagues suggested what made me happier in using these tools. Feel free to add more hints on the subject! Next time I’d want to share something about how I deal with different code editors and extensions for the same purpose.