DEV Community

Cover image for Managing Two GitHub Accounts on Your Mac
Jalaj
Jalaj

Posted on

Managing Two GitHub Accounts on Your Mac

If you find yourself juggling between work and personal GitHub accounts on your Mac, you're not alone. This guide will walk you through the steps to set up and manage two separate GitHub accounts with ease. I've tested these steps on MacOS Ventura M1, but they should work for most Mac users. If you're using Linux, you may need to adapt the commands accordingly. Apologies to Windows users as this guide is tailored for macOS.

Quick Tip: For a summary of the steps, scroll to the bottom for a TL;DR.

Setting up SSH Keys

We need to set up two SSH keys, one for work and another one for the personal account. Give them clear names, like jalaj-work-github and jalaj-personal-github.

Here's how you can create SSH keys for both accounts:

ssh-keygen -t rsa -C "jalaj@work.com" -f "jalaj-work-github"
Enter fullscreen mode Exit fullscreen mode

After running this command, you'll be prompted to enter a passphrase. You can choose something memorable or leave it empty to avoid entering it every time you push or pull.

Similarly, create an SSH key for your personal account:

ssh-keygen -t rsa -C "jalaj@personal.com" -f "jalaj-personal-github"
Enter fullscreen mode Exit fullscreen mode

After running these commands, navigate to the SSH directory by using the command:

cd ~/.ssh
Enter fullscreen mode Exit fullscreen mode

Run the ls command to verify that you have four files:

jalaj-work-github
jalaj-work-github.pub
jalaj-personal-github
jalaj-personal-github.pub
Enter fullscreen mode Exit fullscreen mode

One is a public file, and the other is private. Now, copy the content of the .pub file and add it to the SSH keys section of the respective GitHub account.

Here's how to copy the content to your clipboard:

pbcopy < jalaj-work-github.pub
Enter fullscreen mode Exit fullscreen mode

Now, go to GitHub and set up the SSH key. You can do that by following the steps mentioned in the github docs to add SSH key to your account

After completing this step, add the private keys to the Apple Keychain. Assuming you are still in the ~/.ssh directory, use the following command:

ssh-add --apple-use-keychain jalaj-work-github
Enter fullscreen mode Exit fullscreen mode

Replace "jalaj-work-github" with the actual file path.

Do the same for the other account.

Configuring SSH

Now, create a configuration file for SSH to know which configuration to use based on the origin.

In the ~/.ssh directory, create a file named config if it doesn't already exist:

touch config && code config # or vi config
Enter fullscreen mode Exit fullscreen mode

Add the following content to the config file:

Host github.com-work
  HostName github.com
  User git
  IdentityFile ~/.ssh/jalaj-work-github

Host github.com-personal
  HostName github.com
  User git
  IdentityFile ~/.ssh/jalaj-personal-github
Enter fullscreen mode Exit fullscreen mode

After adding this, whenever you pull a repo or create a new repo, you need to update the remote URL for the repo to match the host value in the config.

Here's a one-liner to do just that:

git remote set-url origin git@github.com-personal:username/reponame.git
Enter fullscreen mode Exit fullscreen mode

Breaking it down:

  • "git@github.com-personal" is the host added in the config file.
  • "username" is your GitHub username.
  • "reponame" is your GitHub repository name.

Setting Up User Information

To let Git know your email and username to attach to the commits, you have two options: manual setup for each repo or using Git config files.

For the manual setup for each repo, run these commands:

git config --local user.email "jalaj@personal.com"
git config --local user.name "jalaj"
Enter fullscreen mode Exit fullscreen mode

You need to remember to do this for every repo on your system.

To make this easier, you can set a global config for work/personal GitHub accounts and manually configure the other one:

git config --global user.email "jalaj@work.com"
git config --global user.name "jalaj"
Enter fullscreen mode Exit fullscreen mode

For an even easier approach, you can use Git config files.

First, open or create a ~/.gitconfig file in the root directory, and create two other files: ~/.gitconfig-work and ~/.gitconfig-personal:

touch ~/.gitconfig ~/.gitconfig-work ~/.gitconfig-personal
Enter fullscreen mode Exit fullscreen mode

Open ~/.gitconfig-work in your favorite editor and add the following values:

[user]
 name = jalaj
 email = jalaj@work.com
Enter fullscreen mode Exit fullscreen mode

Do the same for the ~/.gitconfig-personal file.

Now, open ~/.gitconfig and add the following condition:

[includeIf "gitdir:~/projects/personal/"]
  path = ~/.gitconfig-personal
[includeIf "gitdir:~/projects/work/"]
  path = ~/.gitconfig-work
Enter fullscreen mode Exit fullscreen mode

This configuration loads the correct config file based on the directory where your project resides. Feel free to change the directory to match your work and personal projects.

With all these steps, you can now use both personal and work GitHub accounts on a single system, and everything will work automatically due to the one-time setup you've completed.

Summary

SSH Key Setup

  • Generate two SSH keys: one for work and one for personal accounts.
    • ssh-keygen -t rsa -C "jalaj@work.com" -f "jalaj-work-github"
    • ssh-keygen -t rsa -C "jalaj@personal.com" -f "jalaj-personal-github"
  • Copy the content of the .pub files to your GitHub accounts.

Keychain Configuration

  • Add the private keys to Apple Keychain.
    • ssh-add --apple-use-keychain jalaj-work-github
    • Repeat for the other account.

SSH Configuration

  • Create a config file in ~/.ssh with host-specific settings.
    • Define Hosts for both work and personal GitHub accounts.
    • Specify the IdentityFile for each.

Update Git Remotes

  • For existing repositories, update the remote URL to match the host value in the config file.
    • git remote set-url origin git@github.com-personal:username/reponame.git

User Information

  • Set your email and username for commits.
    • For each repository, use:
    • git config --local user.email "jalaj@personal.com"
    • git config --local user.name "jalaj"
    • Or set a global config:
    • git config --global user.email "jalaj@work.com"
    • git config --global user.name "jalaj"

Advanced Git Config (Optional)

  • Create and configure ~/.gitconfig, ~/.gitconfig-work, and ~/.gitconfig-personal files to manage user information for different projects based on their directory.

With these steps, you can effectively manage both your personal and work GitHub accounts on your Mac. If you have any questions or doubts, feel free to reach out, and you can also contact me on Twitter. Thanks for reading!

Top comments (0)