Managing Work and Personal GitHub Accounts on the same machine

AADI JAIN
2 min readDec 31, 2023
Source: Tecadmin

Hey my friend, if you are here that means you faced the need I faced sometime back, i.e. to configure multiple accounts on the same PC.

In software development, having separate GitHub accounts for work and personal projects is a common practice. This helps maintain a clear distinction between professional and personal contributions, ensuring privacy and organization. In this guide, I’ll walk through the step-by-step process of configuring two GitHub accounts on the same MacBook.

Step 1: Install Git

Before diving into GitHub configurations, ensure that Git is installed on your MacBook. You can download it from the official website or use a package manager like Homebrew:

brew install git

Step 2: Generate SSH Keys

To securely connect to GitHub, I’ll use SSH keys. Generate keys for both your work and personal accounts:

cd ~/.ssh
# Work account
ssh-keygen -t rsa -C "your.work.email@example.com" -f ~/.ssh/id_rsa_work
# Personal account
ssh-keygen -t rsa -C "your.personal.email@example.com" -f ~/.ssh/id_rsa_personal

Step 3: Add SSH Keys to SSH Agent

Add the generated keys to the SSH agent to manage your keys:


# Add work key
ssh-add --apple-use-keychain ~/.ssh/id_rsa_work
# Add personal key
ssh-add --apple-use-keychain ~/.ssh/id_rsa_personal

More details on adding keys to the SSH agent are here.

Step 4: Import all the public keys on the corresponding GitHub accounts

Follow the exact steps mentioned here to create the SSH key on your GitHub account. Below are the commands you’ll be using to copy your locally generated SSH key.

$ pbcopy < ~/.ssh/id_rsa_work.pub
$ pbcopy < ~/.ssh/id_rsa_personal.pub
$ pbcopy < ~/.ssh/github-ibrahimweynodey.pub

Step 5: Create SSH Config File

To differentiate between the two GitHub accounts, create a `config` file in the `~/.ssh/` directory:

touch ~/.ssh/config

Edit the file with the following content:


# Work account
Host github.com-work
HostName github.com
User git
IdentityFile ~/.ssh/id_rsa_work
# Personal account
Host github.com-personal
HostName github.com
User git
IdentityFile ~/.ssh/id_rsa_personal

Step 6: Update Git Configurations

Configure Git with your email and name for each account:

# Work account
git config — global user.email “your.work.email@example.com”
git config — global user.name “Your Work Name”
# Personal account
git config - global user.email "your.personal.email@example.com"
git config - global user.name "Your Personal Name"

Step 7: Clone Repositories

When cloning repositories, use the configured aliases:


# Clone a work repository
git clone git@github.com-work:username/work-repo.git

# Clone a personal repository
git clone git@github.com-personal:username/personal-repo.git

Conclusion:

Following these steps, you can seamlessly manage your work and personal GitHub accounts on the same machine. This setup ensures that your commits are associated with the correct GitHub account, maintaining a clean separation between your professional and personal projects. Happy coding!
Let me know for any doubts or questions. Also, thanks for reading, and would love to hear your feedback

--

--

AADI JAIN

A learner, who learn things and try to express my learning by writing it down. Trying to be a good engineer :)