Setup personal Git on mac with work git

If you are trying to setup personal git and your org’s git in a single mac, here are the brief steps.

To prevent your work (Azure ADO) and personal (Gmail) Git accounts from overlapping on the same Mac, you need to separate them using SSH keys and Git Conditional Configurations.

This setup ensures that:

  1. Authentication is separated (different keys for ADO vs. GitHub/GitLab).
  2. Your commit author email automatically switches based on which folder you are working in (no more accidentally committing to a personal repo with your work email).

Here is the step-by-step guide to setting this up:

Step 1: Organize Your Folders

Create a strict folder structure to separate your work and personal code. Open your Mac Terminal and run:

mkdir -p ~/Work/Projects ~/Personal/Projects

We will tell Git to use your work email for anything inside ~/Work and your personal email for anything inside ~/Personal.

Step 2: Generate a Personal SSH Key

Assuming you already have an SSH key for your work (Azure ADO), let’s create a new one specifically for your personal account. In the Terminal:

# Generate a new SSH key for your personal email
ssh-keygen -t ed25519 -C "your.personal@gmail.com" -f ~/.ssh/id_ed25519_personal

Press Enter when prompted for a passphrase (or add one if you prefer). This creates two files: id_ed25519_personal (private key) and id_ed25519_personal.pub (public key).

Step 3: Add the Personal Key to GitHub/GitLab

  1. Copy the public key to your clipboard:pbcopy < ~/.ssh/id_ed25519_personal.pub
  2. Go to your personal Git host (GitHub, GitLab, Bitbucket) -> Settings -> SSH and GPG Keys.
  3. Click “New SSH key”, paste the key, and save.

Step 4: Configure the SSH Agent

You need to tell your Mac which SSH key to use for which host. Open your SSH config file:

nano ~/.ssh/config

Add the following configuration (if you already have a Host for ADO, just add the GitHub part):

# --- Work Account (Azure ADO) ---
Host ssh.dev.azure.com
  HostName ssh.dev.azure.com
  User git
  IdentityFile ~/.ssh/id_ed25519 # (Use whatever your work key is named)
  IdentitiesOnly yes

# --- Personal Account (GitHub) ---
Host github.com
  HostName github.com
  User git
  IdentityFile ~/.ssh/id_ed25519_personal
  IdentitiesOnly yes

Note: If your personal repo is on GitLab, change Host github.com to Host gitlab.com.
Save and exit (Ctrl+O, Enter, Ctrl+X).

Step 5: Setup Git Conditional Configuration

Now we tell Git to automatically switch your name and email based on the folder you are in.

First, remove your global user.email if you have one set (so Git doesn’t accidentally use it everywhere):

git config --global --unset user.name
git config --global --unset user.email

Open your global Git config file:

nano ~/.gitconfig

Replace its contents with this:

…

# Default settings
[init]
    defaultBranch = main
[core]
    editor = code --wait

# Work Profile (Azure ADO)
[user]
    name = Your Work Name
    email = your_work_email@yourorg.com

# If inside ~/Work folder, use Work profile
[includeIf "gitdir:~/Work/"]
    path = ~/.gitconfig-work

# If inside ~/Personal folder, use Personal profile
[includeIf "gitdir:~/Personal/"]
    path = ~/.gitconfig-personal

…

Now, create the work config file:

nano ~/.gitconfig-work
[user]
    email = your_work_email@yourorg.com

Create the personal config file:

nano ~/.gitconfig-personal
[user]
    email = your.personal@gmail.com

Step 6: How to use this in VS Code

  1. For your personal repo: Open VS Code, open the Terminal (Ctrl + ~), and clone your personal repo using the SSH URL (not HTTPS) into the ~/Personal/Projects folder. cd ~/Personal/Projects git clone git@github.com:your-username/your-repo.git
  2. Verify it worked: Go into the cloned folder and check the Git config: cd your-repo git config user.emailIt should output your.personal@gmail.com.
  3. For your work repo: Continue doing what you do for ADO, but ensure those repositories are saved inside the ~/Work/Projects folder.

Wrapping up

  • Because of the SSH config, when you push to ADO, it uses your work key. When you push to GitHub, it uses your personal key.
  • Because of the includeIf Git config, any commit you make inside ~/Personal will automatically be authored by your Gmail address, and any commit inside ~/Work will be authored by your work email. They will never cross over.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top