DEV Community

Anydigital
Anydigital

Posted on • Edited on

Set-and-Forget Git Privacy in 5 Minutes: Auto-Switch No-Reply Emails for GitHub/GitLab

Accidental exposure of your email(s) is a common privacy risk when committing to multiple Git repositories on platforms like GitHub and GitLab.

The perfect, robust solution is using Git's Conditional Includes ([includeIf]) within your single global Git configuration file (~/.gitconfig) in combination with automatically-generated private commit emails. This forces Git to use the correct no-reply email based purely on the repository's remote URL. Magic!

The Core Principles

  1. Private Commit Emails. Never commit with your personal or primary work email. Both GitHub and GitLab provide unique no-reply commit email addresses that hide your identity while still correctly attributing contributions to your profile.

  2. Enforced Git configuration. Set useconfigonly = true as your privacy guardrail. It prevents Git from falling back to your system username/hostname (e.g., user@laptop.local), which prevents email exposure if no config is found. If no email is set, the commit simply fails, prompting you to fix it.

  3. Automatic switching. Use [includeIf] with **/*hostname.com/** as powerful glob pattern to match both HTTPS (https://) and SSH (git@) remote URLs for the respective hosts, simplifying the configuration to one line per service.

Final Raw Content for ~/.gitconfig

Copy and paste this content into your ~/.gitconfig file, and replace the placeholders!

# =====================================================================
# Global Config
# =====================================================================
[user]
    name = YOUR_NAME_SURNAME

    # CRITICAL: Prevents accidental system email exposure if no
    # specific email is found in the conditional blocks below.
    useconfigonly = true

# =====================================================================
# Conditional Emails
# =====================================================================
[includeIf "hasconfig:remote.*.url:**/*github.com/**"]
    [user]
        email = YOUR_GITHUB_ID+USERNAME@users.noreply.github.com
# =====================================================================
[includeIf "hasconfig:remote.*.url:**/*gitlab.com/**"]
    [user]
        email = YOUR_GITLAB_ID-USERNAME@users.noreply.gitlab.com

Enter fullscreen mode Exit fullscreen mode

How to Verify

  1. Clone a repository from GitHub/GitLab.

  2. Run git config user.email. It will show your respective GitHub/GitLab no-reply email.

This simple, single-file solution ensures your privacy is protected and your commits are correctly attributed, regardless of which hosting platform you're working on. Smart!

Top comments (0)