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

  1. First sign in with any one of your accounts
  2. Then click your avatar in the top right Avatar Button
  3. Click the Account Switcher button (looks like left-right arrows) Switcher Button
  4. Click the Add Account button 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

  1. Open a terminal
  2. 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!

  1. brew install git-lfs
  2. 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

  1. Download the 64-bit Git installer https://git-scm.com/downloads/win
  2. Run the installer
  3. 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:
  4. Select Components
    1. Check Windows Explorer Integration
      1. Check Git Bash
      2. Check Open Git GUI Here
    2. Check Git LFS
    3. Check Associate .git* configuration files with the default text editor
    4. Check Associate .sh files to be run with Bash
    5. Check Add a Git Bash Profile to Windows Terminal
    6. Check Scalar Git Setup Select Components
  5. Adjusting your PATH Environment
    1. Check Git from the command-line and also from 3rd-party software
  6. Choosing the SSH Executable
    1. Check Use bundled OpenSSH
  7. Choosing the HTTPS transport backend
    1. Check Use the OpenSSL library
  8. Configuring the terminal emulator to use with Git Bash
    1. Check Use Windows' default console window

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.

  1. Open a terminal and run these one at a time
  2. git config --global user.name "YourFirstName YourLastName"
  3. git config --global user.email "youremail@example.com"
  4. git config --global init.defaultBranch main
  5. git config --global pull.rebase true
  6. git config --global push.autoSetupRemote true
  7. git config --global core.editor nano
  8. 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

  1. First, make sure you can see all hidden files and folders
    1. Open a terminal and run these in order
    2. defaults write com.apple.Finder AppleShowAllFiles true
    3. killall Finder
  2. Go to your user folder
  3. Create a folder called .ssh if not already there
  4. Create a file called config if not already there SshConfig
  5. The contents of ~/.ssh/config should be
    Host *
     UseKeychain yes
     AddKeysToAgent yes
     IdentitiesOnly yes
    
    Refer to my original post if you want to know more about these properties.

Windows

  1. First, make sure you can see all hidden files and folders
    1. Open File Explorer Options
    2. Check Show Hidden Files
    3. Uncheck Hide extensions for known file types File Explorer Options
  2. Go to your user folder
  3. Create a folder called .ssh if not already there Windows SSH Folder

Repo Folder Setup

  1. Create a folder called Developer in your user folder
  2. Create a folder called github in your Developer folder
  3. 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

Mac Repos Folder

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
  • Work organization named bar
    • Work account name: foo-bar
    • Work email: foo-bar@bar.com

⚠️ 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 name bar (i.e. NOT the work account name foo-bar)
  • Anywhere you see the email foo@example.com -> replace with work email foo-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

  1. 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
  2. Login to github.com with your foolio account
    1. 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.
    2. If your foolio account is not currently selected, use the account switcher
  3. Click Settings
  4. Click SSH and GPG Keys
  5. Click New SSH Key New SSH Key Button
  6. Title: <YourMachineName>
  7. Key type: Authentication Key
  8. 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
  9. Click Add SSH key SSH Key Form

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.

  1. Open a terminal
  2. cd ~/Developer/github/foolio
  3. git clone git@github.com:foolio/ugh.git
  4. Now verify your username and email are correct
    1. Make a commit
    2. 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.

  1. Open a terminal
  2. cd ~/Developer/github/foolio/ugh
  3. git remote set-url origin git@github.com:foolio/ugh.git
  4. To test, run git push, you don’t need to make a commit