DEV Community

Madhan Gannarapu
Madhan Gannarapu

Posted on • Edited on

Managing Multiple Git Accounts on One Machine(Mac/Linux)

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
Enter fullscreen mode Exit fullscreen mode

Set Up Personal and Work

mkdir personal work
Enter fullscreen mode Exit fullscreen mode

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

Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

Configure the SSH Config File

Create or update your ~/.ssh/config file:

touch ~/.ssh/config
open ~/.ssh/config
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

Open and fill them like this:
~/.gitconfig-personal

[user]
  name = Madhan Gannarapu
  email = your-email@personal.com
Enter fullscreen mode Exit fullscreen mode

~/.gitconfig-work

[user]
  name = Madhan Gannarapu
  email = your-email@company.com
Enter fullscreen mode Exit fullscreen mode

Use Conditional Includes in Global .gitconfig

Create or update your ~/.ssh/config file:

touch ~/.ssh/config
open ~/.ssh/config
Enter fullscreen mode Exit fullscreen mode

Paste this:

[user]
  name = Default Madhan
  email = default@example.com

[includeIf "gitdir:~/work/**"]
  path = ~/.gitconfig-work

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

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
Enter fullscreen mode Exit fullscreen mode

Top comments (0)