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

Madhan Gannarapu @madhan_gannarapu

About: A Software Developer passionate about building intuitive, scalable, and high-performance digital experiences. With a strong foundation in both mobile and web application development

Location:
Hyderabad, India
Joined:
Aug 8, 2022

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

Publish Date: Jul 4
1 0

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

Comments 0 total

    Add comment