Multi-account GitHub Workflow on Mac and Windows
If you have multiple github.com accounts, such as a personal and work account, and you want the correct account to be used automatically without signing out and signing in, then this post may interest you.
This is a spiritual successor to an old Medium post of mine that was specific to Mac. I figured out how to add Windows support so that’s covered here too.
This update was made possible by a couple ergonomic improvements that have been made over the years
- GitHub’s website now supports an account switcher
- Git’s config option
includeIf
There’s a lot of ground to cover, so let’s get into it.
Switching Accounts on github.com
The first thing you’ll want to do is sign into all of your accounts on github.com
- First sign in with any one of your accounts
- Then click your avatar in the top right
- Click the Account Switcher button (looks like left-right arrows)
- Click the Add Account button
You’ll need to use the account switcher later to repeat chunks of this guide for each account you want to setup.
OS and Software Versions
This guide was written using the following versions:
- Mac: macOS Sequoia 15.3
- Windows: Windows 11 24H2
- Git: 2.48.1
One-Time Machine Setup
⚠️ These steps only need to be done once per machine ⚠️
This is the general shape of what we will do:
- Organize your folders on disk similarly to how github.com organizes accounts i.e.
https://github.com/{account-name | org-name}/repo-name
- Create an ssh key pair on each machine for each account
- Use your ssh keys + folder layout + the
includeIf
config option to automatically choose the correct account based on the repo’s folder
Currently, multiple ssh keys is the only way to make this work.
An ssh key is essentially a user/pass in the form of an encrypted file. Git has no built-in way to select accounts, but it does have built-in ssh support. So we’re going to take advantage of that by leveraging ssh as an automated account selector.
Your User Folder
I’m going to refer to “your user folder” a lot in this guide.
On Mac and Windows that means these folders:
- Mac:
/Users/<YourUserName>
- Windows:
C:\Users\<YourUserName>
From a Mac terminal or Windows PowerShell, the ~
character is an alias for your user folder:
cd ~
For example:
mike@mikes-mbp14 Developer % cd ~
mike@mikes-mbp14 ~ % pwd
/Users/mike
mike@mikes-mbp14 ~ %
Git CLI Setup
Install Git
Let’s first walk through installing the Git CLI to support multiple accounts.
Mac
- Open a terminal
brew install git
Since I work in games, I also install Git LFS. Even if you don’t, adding support for LFS couldn’t hurt!
brew install git-lfs
git lfs install
That’s it, Mac is easy since SSH is well supported on Mac. You’ll configure Git in the next section after the Windows setup.
Windows
- Download the 64-bit Git installer https://git-scm.com/downloads/win
- Run the installer
- There are a lot of pages in this installer, so for the most part, you’ll want to leave defaults as-is and choose the things that are called out here:
- Select Components
- Check
Windows Explorer Integration
- Check
Git Bash
- Check
Open Git GUI Here
- Check
- Check
Git LFS
- Check
Associate .git* configuration files with the default text editor
- Check
Associate .sh files to be run with Bash
- Check
Add a Git Bash Profile to Windows Terminal
- Check
Scalar
- Check
- Adjusting your PATH Environment
- Check
Git from the command-line and also from 3rd-party software
- Check
- Choosing the SSH Executable
- Check
Use bundled OpenSSH
- Check
- Choosing the HTTPS transport backend
- Check
Use the OpenSSL library
- Check
- Configuring the terminal emulator to use with Git Bash
- Check
Use Windows' default console window
- Check
You’ll do remaining Git config from a terminal in the next section.
Configure Git
We’re going to setup your global default Git config settings. This means settings that are used when we have not setup an account to use for a specific folder.
This is actually mostly optional, so you can skip this section if you don’t like my config. These steps are the same for Mac and Windows.
- Open a terminal and run these one at a time
git config --global user.name "YourFirstName YourLastName"
git config --global user.email "youremail@example.com"
git config --global init.defaultBranch main
git config --global pull.rebase true
git config --global push.autoSetupRemote true
git config --global core.editor nano
git config --global core.fsmonitor true
All of these settings end up in your .gitconfig
file in your user folder. My .gitconfig
looks like this for reference.
Mac ~/.gitconfig
[filter "lfs"]
clean = git-lfs clean -- %f
smudge = git-lfs smudge -- %f
process = git-lfs filter-process
required = true
[user]
name = Mike Schweitzer
email = mschweitzer@gmail.com
[init]
defaultBranch = main
[pull]
rebase = true
[push]
autoSetupRemote = true
[core]
editor = nano
[credential]
helper =
helper = /usr/local/share/gcm-core/git-credential-manager
[credential "https://dev.azure.com"]
useHttpPath = true
Windows ~/.gitconfig
[filter "lfs"]
clean = git-lfs clean -- %f
smudge = git-lfs smudge -- %f
process = git-lfs filter-process
required = true
[user]
name = Mike Schweitzer
email = mschweitzer@gmail.com
[init]
defaultBranch = main
[pull]
rebase = true
[push]
autoSetupRemote = true
[core]
editor = nano
.ssh
Folder Setup
By convention, the ~/.ssh
folder is where you store ssh keys.
Mac
- First, make sure you can see all hidden files and folders
- Open a terminal and run these in order
defaults write com.apple.Finder AppleShowAllFiles true
killall Finder
- Go to your user folder
- Create a folder called
.ssh
if not already there - Create a file called
config
if not already there - The contents of
~/.ssh/config
should be
Refer to my original post if you want to know more about these properties.Host * UseKeychain yes AddKeysToAgent yes IdentitiesOnly yes
Windows
- First, make sure you can see all hidden files and folders
- Open File Explorer Options
- Check
Show Hidden Files
- Uncheck
Hide extensions for known file types
- Go to your user folder
- Create a folder called
.ssh
if not already there
Repo Folder Setup
- Create a folder called
Developer
in your user folder - Create a folder called
github
in yourDeveloper
folder - Create folders for each of your account or organization names, reminder that you want your folders to look like your github URLs
For example, if you have accounts foo
and bar
, you’d have something like this
Repeated Per-Account Setup
⚠️ These steps need to be repeated for each of your accounts ⚠️
Let’s say we are working with this GitHub setup:
- Personal account named
foolio
- Personal email:
foo@example.com
- Personal email:
- Work organization named
bar
- Work account name:
foo-bar
- Work email:
foo-bar@bar.com
- Work account name:
⚠️ You will need to run all of the steps in the following sections for both accounts. ⚠️
For simplicity, I will use foolio
and foo@example.com
in the following steps.
⚠️ Note for Organizations / Work Accounts ⚠️
When setting up an org / work account, there is a trick:
- Anywhere in the following steps when you see the account name
foolio
-> replace with org namebar
(i.e. NOT the work account namefoo-bar
) - Anywhere you see the email
foo@example.com
-> replace with work emailfoo-bar@bar.com
In other words, when you setup your org-based / work account: replace the personal account name with the org name and the personal email with your work email.
Create an SSH Key Pair
When creating an ssh key, you create a “key pair”, meaning 2 files (1) a private key file and (2) a public key file.
The private key stays on your machine and the public key gets uploaded to your github.com account.
⚠️ We will be giving our ssh key pair readable names that incorporate the word “github” followed by the account/org name. One of the most common points of confusion I see in ssh setup guides is leaving the default ssh key file names, which is typically the name of the encryption algorithm used to make the key. That’s not useful to you as an end user, so we’re going to avoid that problem. ⚠️
NOTE: You will be given the option to password-protect your ssh keys. This is like password-protecting a pdf. I don’t do that and don’t recommend it, but it’s up to you.
Mac
Open a new terminal, and just copy what you see on the right hand side of each prompt of this terminal block into your own terminal.
mike@mikes-mbp14 ~ %
mike@mikes-mbp14 ~ % cd ~/.ssh
mike@mikes-mbp14 .ssh % ssh-keygen -t ed25519 -C "foo@example.com"
Generating public/private ed25519 key pair.
Enter file in which to save the key (/Users/mike/.ssh/id_ed25519): github_foolio
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in github_foolio
Your public key has been saved in github_foolio.pub
The key fingerprint is:
SHA256:2YGzaeGnJnHvt11w9610IEEsBsMy1Cxmti09u/HbKm8 foo@example.com
The key's randomart image is:
+--[ED25519 256]--+
| ..+o. .. |
| B +oo.. |
| + B+.... |
| o.+B . . |
| ..Soo . o o|
| +o+ . ++|
| . o+. . =|
| oo.E..o + |
| +=+o.o |
+----[SHA256]-----+
mike@mikes-mbp14 .ssh % ssh-add github_foolio
Identity added: github_foolio (foo@example.com)
mike@mikes-mbp14 .ssh %
Windows
⚠️ You MUST use a Git Bash terminal in this step ⚠️. In all other steps, you can use the default PowerShell terminal.
Open a Git Bash terminal and copy what you see at each prompt.
Mike@zephyrus-m16 MINGW64 ~
$ cd ~/.ssh
Mike@zephyrus-m16 MINGW64 ~/.ssh
$ ssh-keygen -t ed25519 -C "foo@example.com"
Generating public/private ed25519 key pair.
Enter file in which to save the key (/c/Users/Mike/.ssh/id_ed25519): github_foolio
Enter passphrase for "github_foolio" (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in github_foolio
Your public key has been saved in github_foolio.pub
The key fingerprint is:
SHA256:BW6bt56HIt2X8f3tzUPd/TGib14MSCtBnLss2HCYjs4 foo@example.com
The key's randomart image is:
+--[ED25519 256]--+
| o.. |
| ..+ |
| o o.o. |
| + o =o o |
| o = S.oo . +|
| . o o +....o++|
| o ......+o++|
| E . o.oo+.oo=|
| . .oo+o .O|
+----[SHA256]-----+
Key Pair Files
After the above steps, you should have two files:
- Private key:
~/.ssh/github_foolio
(stays on your machine) - Public key:
~/.ssh/github_foolio.pub
(will upload contents to github.com in next section)
Restart your machine now.
This is the simplest, most reliable way I’ve found to get an SSH private key to be recognized on either Mac or Windows after much experimentation.
Copy the Public SSH Key to github.com
- First, find your machine name
- Mac: System Settings > General > Name
- Windows: Settings > System > About > Device name
- NOTE: You’ll need this to give your public ssh key a sensible name in your github.com account
- Login to github.com with your
foolio
account- If you’re logging in to an account you made for work in order to access repos in a work org (e.g. your work account is named
foo-bar@bar.com
or something), go ahead and login to that. We won’t be using the name of your work github account elsewhere in this guide. - If your
foolio
account is not currently selected, use the account switcher
- If you’re logging in to an account you made for work in order to access repos in a work org (e.g. your work account is named
- Click
Settings
- Click
SSH and GPG Keys
- Click
New SSH Key
- Title:
<YourMachineName>
- Key type:
Authentication Key
- You have to copy/paste your public key (located at
~/.ssh/github_foolio.pub
) into the Key field.- This is SUPER touchy since you have to paste the contents without any extra whitespace. To do this, open a terminal, then:
- Mac:
pbcopy < ~/.ssh/github_foolio.pub
- Windows:
cat ~/.ssh/github_foolio.pub | clip
- Click
Add SSH key
Use a Specific Account in an Account Subfolder
Let’s assume you have a folder to hold repos from your foolio
account.
~/Developer/github/foolio
And you want to use this as your user identity:
Name: Foo Baloo
Email: foo@example.com
Go to your user folder in Finder/Explorer. Open ~/.gitconfig
in a text editor. Add this entry to the bottom:
Mac
[includeIf "gitdir:~/Developer/github/foo/"]
path = ~/.gitconfig_foolio
Windows
[includeIf "gitdir:C:/Users/<YourUserName>/Developer/github/foo/"]
path = ~/.gitconfig_foolio
⚠️ A common Windows mistake is to omit the last slash in the gitdir
path ⚠️
Still in your user folder, create a new file called .gitconfig_foolio
, and make the contents look like this:
[core]
sshcommand = ssh -i ~/.ssh/github_foolio
[user]
name = Foo Baloo
email = foo@example.com
Clone a Repo in an Account Subfolder
The setup is actually done now, so you’ll want to test by cloning a repo.
Let’s assume you have a private repo ugh
in your foolio
account. Make sure the repo is private so you can test that your ssh credentials are actually being used.
- Open a terminal
cd ~/Developer/github/foolio
git clone git@github.com:foolio/ugh.git
- Now verify your username and email are correct
- Make a commit
- Do a
git log
to verify the username and email
Change an Existing Repo in an Account Subfolder
Let’s assume the ugh
repo in your foolio
account was already cloned before you did all this setup, and you want this repo to start using the account-specific config we just made.
- Open a terminal
cd ~/Developer/github/foolio/ugh
git remote set-url origin git@github.com:foolio/ugh.git
- To test, run
git push
, you don’t need to make a commit