This content originally appeared on DEV Community and was authored by Madhan Gannarapu
How to set up and manage personal and work Git accounts on your local machine.
Introduction
Many developers work with both personal and work GitHub accounts. Here’s how to manage them on a Mac…
What You’ll Learn
Use SSH to separate identities
Automatically apply correct Git user config based on folder
Clean directory structure
Directory Setup
~/
├── .ssh/
│ ├── work/
│ │ ├── id_ed25519_work
│ │ ├── id_ed25519_work.pub
│ ├── personal/
│ │ ├── id_ed25519_personal
│ │ ├── id_ed25519_personal.pub
│ └── config # SSH config file
├── .gitconfig
├── .gitconfig-work # Workspace-specific Git config
├── .gitconfig-personal # Personal Git config
├── work/ # All workspace (work) repositories
└── personal/ # All personal repositories
Set Up Personal and Work
mkdir personal work
personal
: Use this folder for:
- Your own apps, portfolios
- Open source contributions
- Learning new tech, coding experiments
- Resume or portfolio projects
- Hackathons or fun builds
work
: Use this folder for:
- company apps/repos
- poc apps
- Anything related to employment
Generate SSH Keys for Each Git Account
Generate a unique SSH key for each GitHub or GitLab account:
# Personal
ssh-keygen -t ed25519 -C "your-email@personal.com" -f ~/.ssh/personal/id_ed25519_personal
# Work
ssh-keygen -t ed25519 -C "your-email@company.com" -f ~/.ssh/work/id_ed25519_work
Now Add the SSH keys to the ssh-agent:
# Personal
ssh-add ~/.ssh/personal/id_ed25519_personal
# Work
ssh-add ~/.ssh/work/id_ed25519_work
Add SSH Keys to Your GitHub Accounts (Company & Personal)
Once you’ve generated the SSH keys, you need to add the public keys to your respective GitHub accounts so they can recognize and authorize your machine
To view the SSH keys in terminal run
# Personal
cat ~/.ssh/personal/id_ed25519_personal.pub
# work
cat ~/.ssh/work/id_ed25519_work.pub
Configure the SSH Config File
Create or update your ~/.ssh/config file:
touch ~/.ssh/config
open ~/.ssh/config
add:
# Personal
Host github.com-personal
HostName github.com
User git
IdentityFile ~/.ssh/personal/id_ed25519_personal
# Work
Host github.com-work
HostName github.com
User git
IdentityFile ~/.ssh/work/id_ed25519_work
Use github.com-personal
or github.com-work
as the remote URL host.
## Verification SSH with GitHub accounts
To verify SSH works:
ssh -T git@github-work
ssh -T git@github-personal
Expect messages like: “Hi ! You’ve successfully authenticated…”
Configure the Global.gitconfig
File
Create two custom Git configs:
# Personal Git config
touch ~/.gitconfig-personal
# Work Git config
touch ~/.gitconfig-work
Open and fill them like this:
~/.gitconfig-personal
[user]
name = Madhan Gannarapu
email = your-email@personal.com
~/.gitconfig-work
[user]
name = Madhan Gannarapu
email = your-email@company.com
Use Conditional Includes in Global .gitconfig
Create or update your ~/.ssh/config
file:
touch ~/.ssh/config
open ~/.ssh/config
Paste this:
[user]
name = Default Madhan
email = default@example.com
[includeIf "gitdir:~/work/**"]
path = ~/.gitconfig-work
[includeIf "gitdir:~/personal/**"]
path = ~/.gitconfig-personal
This config tells Git to automatically use different identity files depending on which directory the repo is in.
Clone Repos with Correct SSH Host
Use the matching host from your ~/.ssh/config
file
# Personal
git clone git@github.com-personal:username/repo.git ~/Projects/personal/repo
# Work
git clone git@github.com-work:company/repo.git ~/Projects/work/repo
This content originally appeared on DEV Community and was authored by Madhan Gannarapu