Prabhakar Kasi iOS Developer, Front End Developer

How to Switch Git Accounts on Mac with Different GPG Keys

3 min read

Switching between Githuh Accounts when using GPG Key In mac


Managing multiple Git accounts can be tricky, especially if you want to keep your commits signed and avoid conflicts. This guide will show you how to safely switch between Git accounts when using GPG keys, step by step.

Previously I have written about how to configure multiple accounts with SSH keys, but this workflow needs to change if you have GPG Key setup to make your commit to show “verified” tag.

Why You Need GPG Keys for Git

GPG keys allow you to digitally sign your commits, proving that they are genuinely from you. This is particularly important if:

  • You contribute to both personal and work repositories.
  • You maintain a public GitHub/GitLab profile with verified commits.
  • You want to avoid merge conflicts or accidental commits from the wrong account.

Step 1: Generate a GPG Key for Each Account

  1. Open your terminal.
  2. gpg doesn’t come installed in mac
  3. Install it with brew
    brew install gpg
  4. Generate a new key with:
    gpg –full-generate-key
  5. Choose your preferences (usually RSA and 4096 bits is safe).
  6. Add your email corresponding to the Git account.
  7. List your keys with:
    gpg –list-secret-keys –keyid-format LONG
  8. Copy your key ID; you’ll need it for Git configuration.
  9. There are other steps that is needed for this to work. Please check this guide


Step 2: Configure Git to Use Your GPG Key

For current account, run:

git config --global user.name "Your Name"
git config --global user.email "your-email@example.com"
git config --global user.signingkey <YOUR_KEY_ID>
git config --global commit.gpgsign true

Tip: If you want different keys for different repositories, skip –global and run inside the repo folder.


Step 3: Use SSH or HTTPS for Multiple Accounts

SSH: Create a unique SSH key for each account and configure ~/.ssh/config:

Host github-work
    HostName github.com
    User git
    IdentityFile ~/.ssh/id_rsa_work

Host github-personal
    HostName github.com
    User git
    IdentityFile ~/.ssh/id_rsa_personal


Step 4: Automate Switching with Git Aliases and GPG Keys


You can create aliases that switch name, email, and GPG key at the same time:

# Work account
git config --global alias.switch-work "!git config user.name 'Work Name' && git config user.email 'work@example.com' && git config user.signingkey WORK_KEY_ID"

# Personal account
git config --global alias.switch-personal "!git config user.name 'Personal Name' && git config user.email 'personal@example.com' && git config user.signingkey PERSONAL_KEY_ID"


Then simply run:

git switch-work
git switch-personal


✅ This ensures that your commits are signed with the correct GPG key for each account.

There are other options for switching between accounts

Option 1 — Easiest & Best: Per-repository identity

Set your name, email, and GPG signing key only in each repo instead of global:

In your work repo:

git config user.name "Work Name"
git config user.email "[email protected]"
git config user.signingkey ABC123WORK

In your personal repo:

git config user.name "Personal Name"
git config user.email "[email protected]"
git config user.signingkey XYZ456PERSONAL  

This gives automatic identity switching based on which repo you are inside. No global settings needed.

Option 2 — Automatic switching using includeIf (recommended)

Your ~/.gitconfig can automatically switch identity based on folder.

Example:

# ~/.gitconfig
[user]
  signingkey = PERSONAL_KEY_ID
  email = [email protected]
  name = Personal Name

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

Then create ~/.gitconfig-work:

# ~/.gitconfig-work
[user]
  name = Work Name
  email = [email protected]
  signingkey = WORK_KEY_ID

Now:

  • Any repo under ~/work/ → uses work identity
  • Everything else → uses personal identity

Super clean.


Option 3 — Shell shortcuts to switch identities globally
If you prefer toggling manually:

#~/.zshrc

alias use_work="git config --global user.name 'Work Name' \
  && git config --global user.email '[email protected]' \
  && git config --global user.signingkey WORK_KEY_ID"

alias use_personal="git config --global user.name 'Personal Name' \
  && git config --global user.email '[email protected]' \
  && git config --global user.signingkey PERSONAL_KEY_ID"


Run :

# to switch to work
use_work 

# to switch to personal
use_personal


Prabhakar Kasi iOS Developer, Front End Developer

Leave a Reply