Developers around the world is probably already familiar with Git and the most popular Git platform, GitHub. As a developer, I have my own personal GitHub account. But oftentimes, my workplace requires me to have a work-dedicated account.
On my laptop, I set up my work account first, and everything works flawlessly. When I want to switch my work account to my personal account, Git seems to only support having one account in one machine.
You have to add and remove the GitHub SSH keys for each account manually when you want to switch. I searched for solutions on the internet. But from what I read, it seems that sometimes those solutions work and sometimes they don't.
I tried using GitHub Desktop (yeah, I’m that desperate) but it doesn't support this feature yet. Even the GitHub team seems to intentionally leave this issue unsolved. Why though!?
Feature: Options > Git > manage multiple accounts · Issue #3707 · desktop/desktop
It would be great to be able to manage multiple git credentials in the options. Additionally it'd be nice to set an git credential override for each repository. Otherwise when a user works with personal and work related accounts they alw...
github.com
In the end, I found 3 ways to use multiple Git account in one computer. First, you need to configure your accounts.
Before you start
Add your first GitHub Account
In this example, I’ll add my work account first, then I’ll add my personal Git account. You can follow the guide here:
Generating a new SSH key and adding it to the ssh-agent - GitHub Docs
If you don't already have an SSH key, you must generate a new SSH key to use for authentication. If you're unsure whether you already have an SSH key, you can check for existing keys. For more information, see " Checking for existing SSH keys."
docs.github.com
I created the SSH key for my work account using ed25519 algorithm, like this:
$ ssh-keygen -t ed25519 -C "[email protected]"Which creates id_ed25519 and id_ed25519.pub files in your ~/.ssh directory by default.
And then, copy and add the contents of the public key onto your GitHub account settings, explained by this docs:
Adding a new SSH key to your GitHub account - GitHub Docs
Before adding a new SSH key to your account on GitHub.com, you should have: After adding a new SSH key to your account on GitHub.com, you can reconfigure any local repositories to use SSH. For more information, see " Switching remote URLs from HTTPS to SSH."
docs.github.com
Add your other GitHub Account
For this other account, you roughly need to follow the steps same as above. You should name the generated file with a different name from the first account, though.
I created the key for my personal account using other algorithm, RSA, like this:
$ ssh-keygen -t rsa -b 4096 -C "[email protected]"And then save them with other name. I chose id_rsa_personal. Don't forget to add the key into your GitHub account settings.
Now what?
So now I have two SSH keys in my computer and I have added them into GitHub:
- My work GitHub account key (
[email protected]) located at~/.ssh/id_ed25519 - My personal GitHub account key (
[email protected]) located at~/.ssh/id_rsa_personal
Below these section is the many options you can choose on how to use your multiple GitHub accounts.
Option 1: Switch account based on current working directory
For this option, you have to place all your work-related files in ~/Work and you need to make sure you don't need to use your work account everywhere else. You’d want to access your project everywhere else using your personal account.
Editing .gitconfig
We need to create separate Git config files for each account. One for work account and one for personal account:
[user]
name = personal-account
email = [email protected]
[core]
sshCommand = ssh -i ~/.ssh/id_rsa_personal -F /dev/null[user]
name = work-account
email = [email protected]
[core]
sshCommand = ssh -i ~/.ssh/id_ed25519 -F /dev/nullIn your ~/.gitconfig, add something like this:
[includeIf "gitdir/i:~/"]
path = ~/.gitconfig.personal
[includeIf "gitdir/i:~/Work/"]
path = ~/.gitconfig.workExplanation:
.gitconfig.personaland.gitconfig.worktells Git to use your configured name, email, andsshCommandto execute when authentication is required. ThissshCommandadds the corresponding GitHub SSH key everytime we need authentication.- The
.gitconfigtells Git to use the config file~/.gitconfig.personalwhen your current working directory is on your home (~/) directory and its subfolders. It also tells Git to use the config file~/.gitconfig.workwhen your current working directory is on~/Work.gitdir/i:tells Git to read the directory name as case-insensitive.
Usage
Just cd into your work folder and your account will be switched accordingly when you use Git. See this example:
Similarly, if you want to clone a private repo from your personal GitHub account, it will only work if you're on a directory outside of ~/Works. Easy right?
Pros
- Probably this is the best option for you if you separate your work and personal project files
Cons
- Your account only works on your specified directory. Less “flexibility”
- Doesn't work well if your work and personal files are messy and scattered everywhere. But c'mon, just tidy it up!
Option 2: Switch Git account based on custom host
SSH config
Open the file ~/.ssh/config and edit it to be like this:
Host github.com-personal
IdentityFile ~/.ssh/id_rsa_personal
HostName github.com
PreferredAuthentications publickey
UseKeychain yes
AddKeysToAgent yes
Host github.com-work
IdentityFile ~/.ssh/id_ed25519
HostName github.com
PreferredAuthentications publickey
UseKeychain yes
AddKeysToAgent yesHere, we can see the two identity files we created before, under two different hosts. Which means:
- When accessing
github.com-personal, use the key~/.ssh/id_rsa_personal - Otherwise, when accessing
github.com-work, use the key~/.ssh/id_ed25519 - Add the key to the SSH agent if it's not added yet
- After doing so, resolve the actual host
github.com
Git will use different SSH files automatically when you use the corresponding host. You can also add a fallback using Host *. Let's say I want to use my personal GitHub account when the URL requested is neither github.com-work nor github.com-personal, just add this section:
Host *
IdentityFile ~/.ssh/id_rsa_personal
HostName github.com
PreferredAuthentications publickey
UseKeychain yes
AddKeysToAgent yesUsage
When you want to use your personal account:
$ git clone ssh://[email protected]/personal-account/personal-private-repoSimilarly, if you want to use your work account, do this:
$ git clone ssh://[email protected]/work-account/work-private-repoYou will also need to update your origin URLs in your existing repos to use them with your preferred account, like this:
# Change the 'origin' remote's URL
$ git remote set-url origin ssh://[email protected]/personal-account/personal-private-repo
# Now it will automatically use your personal account when doing actions in the local repo
$ git remote -v
origin ssh://[email protected]/personal-account/personal-private-repo (fetch)
origin ssh://[email protected]/personal-account/personal-private-repo (push)Pros
- Can be configured per-repository folder
Cons
- Well, you'll have to change all of your repo's remote URLs to use one of the configured host
- Might accidentally use the wrong account if you forgot to change the host
Option 3: Manually switch account using command line alias
Note that this option will only work if you have done the steps explained in Option 2 above. If you're not a fan of the options above and just want a really, really simple way to switch account whenever you want it, you can make an alias in your shell to do this.
In your .bashrc or .zshrc or whatever your shell's config file is, you can add these line:
alias use-personal-git="ssh-add -D && ssh-add -K ~/.ssh/id_rsa_personal &>/dev/null && ssh -T [email protected]"
alias use-work-git="ssh-add -D && ssh-add -K ~/.ssh/id_ed25519 &>/dev/null && ssh -T [email protected]"Basically we remove all SSH keys and then add the corresponding GitHub SSH key that we want everytime we want to use that account. Then, we call ssh -T to make sure the account can be used.
Usage
I think it's self-explanatory already. If you want to use your personal account, you run this:
$ use-personal-git
All identities removed.
Hi personal-account! You've successfully authenticated, but GitHub does not provide shell access.Similarly, if you want to use your work account, do this:
$ use-work-git
All identities removed.
Hi work-account! You've successfully authenticated, but GitHub does not provide shell access.Pros
- Pretty straightforward
- You use the selected account only when you need it
Cons
- Since this also needs #2 to be configured, why not just use #2? Or even better, #1?
Conclusion
So those are 3 ways you can choose to use multiple Git accounts in a single computer. I hope it can be helpful for newcomers or even experts who need to use two or more accounts in one machine.
I’m not really doing something new here. I actually compiled this article after reading a bunch of Stackoverflow questions and other people’s blog posts.
Git is already >15 years old now. It’s annoying that things as simple as account switching is not possible without “hacks”. I hope 5 years from now this post will become obsolete 🙂