Skip to content

Instantly share code, notes, and snippets.

@seigtm
Created February 13, 2025 07:20
Show Gist options
  • Select an option

  • Save seigtm/5dae0ff529b5b1f09bb7a4c3d4709cf6 to your computer and use it in GitHub Desktop.

Select an option

Save seigtm/5dae0ff529b5b1f09bb7a4c3d4709cf6 to your computer and use it in GitHub Desktop.
This guide explains how to set up Git to use different email addresses and GPG signing keys based on the remote repository URL.

Git Configuration for Multiple Identities

This guide explains how to set up Git to use different email addresses and GPG signing keys based on the remote repository URL.

Configuration Files

You'll need to create three configuration files:

1. Main Git Config (~/.gitconfig)

[tag]
    gpgsign = true
[commit]
    gpgsign = true

# Select correct user config depending on the remote (work GitLab or personal GitHub)
[includeIf "hasconfig:remote.*.url:git@YOUR_GITLAB_HOST:**"]
    path = .gitconfig-work
[includeIf "hasconfig:remote.*.url:https://YOUR_GITLAB_HOST/**"]
    path = .gitconfig-work
[includeIf "hasconfig:remote.*.url:git@github.com:**"]
    path = .gitconfig-personal
[includeIf "hasconfig:remote.*.url:https://github.com/**"]
    path = .gitconfig-personal

2. Work Git Config (~/.gitconfig-work)

[user]
    email = your.work@email.com
    name = Your Name
    signingkey = YOUR_WORK_GPG_KEY_ID

3. Personal Git Config (~/.gitconfig-personal)

[user]
    email = your.personal@email.com
    name = Your Name
    signingkey = YOUR_PERSONAL_GPG_KEY_ID

How It Works

  • The main .gitconfig contains the global signing settings and URL-based conditions
  • When pushing to work GitLab (either via SSH or HTTPS), the work configuration is used
  • When pushing to GitHub (either via SSH or HTTPS), the personal configuration is used
  • All commits and tags are automatically signed with the appropriate GPG key

Verification

To verify which configuration is being used in a repository:

# Check the active email
git config --show-origin --get user.email

# Check the active GPG key
git config --show-origin --get user.signingkey

# Check the remote URL
git remote -v

URL Patterns

The configuration supports both SSH and HTTPS URLs:

  • SSH format: git@hostname:username/repo.git
  • HTTPS format: https://hostname/username/repo.git

Notes

  • The :** pattern is used for SSH URLs
  • The /** pattern is used for HTTPS URLs
  • Configuration files should be stored in your home directory
  • Make sure your GPG keys are properly set up in both GitLab and GitHub
  • Replace all placeholder values (YOUR_GITLAB_HOST, your.work@email.com, etc.) with your actual information
@sthames42
Copy link

What if you have two remotes on one repo and you want to use different user/email settings for each?

If .gitconfig looks like this:

[includeIf "hasconfig:remote.*.url:git@gitlab.com:**"]
    path = .gitconfig-gitlab
[includeIf "hasconfig:remote.*.url:git@github.com:**"]
    path = .gitconfig-github

and the repo has both remotes, won't the user/email always be read from .gitconfig-github?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment