Prabhakar Kasi iOS Developer, Front End Developer

How to Switch Git Accounts on Mac with Different GPG Keys

4 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

How to find the GPG key ID

Run this command to list your secret keys:

gpg --list-secret-keys --keyid-format LONG

Example output:

sec   rsa4096/XXYYXXYYXX 2026-05-23 [SC]

      0000000000000000000000000000000000000000A

uid                 [ultimate] FirstName LastName <your-email@example.com>

ssb   rsa4096/00000000000A 2026-05-23 [E]

The key ID is the value after rsa4096/ in the sec line:

XXYYXXYYXX

Delete an old GPG key before regenerating

If Git is trying to sign commits with an old key, or if you want to regenerate your GPG key, delete the old secret key first:

gpg --delete-key XXYYXXYYXX

Then delete the public key:

gpg --delete-key XXYYXXYYXX

After deleting it, generate a new key:

gpg --full-generate-key


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 few 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 function to switch identities globally

If you prefer switching manually, you can add this function to your ~/.zshrc file. It switches Git name, email, GPG signing key, and SSH key together. This is more flexible. I personally prefer this.

# GitHub GPG keys alias
gitprofile() {
  case "$1" in
    personal_account1)
      git config --global user.name "FirstName LastName (email1)"
      git config --global user.email ""
      git config --global user.signingkey ""
      ssh-add -D 2>/dev/null
      ssh-add ~/.ssh/account1_id_ed25519
      echo "Switched to account1 identity and SSH key"
      ;;

    personal_account2)
      git config --global user.name "FirstName LastName (email2)"
      git config --global user.email "email2"
      git config --global user.signingkey ""
      ssh-add -D 2>/dev/null
      ssh-add ~/.ssh/account2_id_ed25519
      echo "Switched to account2 identity and SSH key"
      ;;

    personal_account3)
      git config --global user.name "FirstName LastName (email3)
      git config --global user.email "email3"
      git config --global user.signingkey ""
      ssh-add -D 2>/dev/null
      ssh-add ~/.ssh/account3_id_ed25519
      echo "Switched to account3 identity and SSH key"
      ;;
    *)
      echo "Usage: gitprofile {personal_account1|personal_account2|personal_account3}"
      ;;
  esac
}

Reload your shell config:

source ~/.zshrc

Then switch profiles like this:

gitprofile personal_account1
Prabhakar Kasi iOS Developer, Front End Developer