Managing Work and Personal GitHub Accounts on the same machine
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 gitStep 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_personalStep 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_personalMore 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.pubStep 5: Create SSH Config File
To differentiate between the two GitHub accounts, create a `config` file in the `~/.ssh/` directory:
touch ~/.ssh/configEdit 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_personalStep 6: Update Git Configurations
Option 1: The Simple Method (Per-Repository Configuration)
You set a global identity once, and then you configure your work identity only inside your work-related projects.
- Set your Personal Account as the Global Default:
Run this command once. This will be your default identity for any new project.
git config --global user.name "Your Personal Name"
git config --global user.email "your.personal.email@example.com"2. Set your Work Account for Work Repositories:
Now, whenever you are inside a work-related repository, run the following command without the — global flag. This setting applies only to that specific repository.
# First, navigate into your work repository
cd path/to/your/work-repo
# Now, set the local user for this repo
git config user.name "Your Work Name"
git config user.email "your.work.email@example.com"This local configuration will always override your global one, ensuring your work commits are correctly authored.
Option 2: The Automated Method (Using includeIf)
This is a powerful “set it and forget it” solution if you keep your work and personal projects in separate parent folders (e.g., all work projects are inside ~/dev/work/).
- Organize Your Folders:
First, make sure your projects are organized. For example:
- Personal projects:
~/dev/personal/ - Work projects:
~/dev/work/
2. Edit Your Global .gitconfig File:
Open your main Git configuration file located at ~/.gitconfig and add an includeIf section. It should look like this:
# This is your default (personal) user info
[user]
name = Your Personal Name
email = your.personal.email@example.com
# This line tells Git to include another config file
# ONLY if the repository is located inside the ~/dev/work/ directory.
[includeIf "gitdir:~/dev/work/"]
path = ~/.gitconfig-work3. Create a Work-Specific Git Config:
Now, create the new file mentioned in the path above (~/.gitconfig-work) and add your work identity to it:
# This is the content for ~/.gitconfig-work
[user]
name = Your Work Name
email = your.work.email@example.comWith this setup, Git will automatically switch to your work identity anytime you’re working inside the ~/dev/work/ folder.
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.gitConclusion:
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
