iTranslated by AI
Making GitHub Commit Email Private, Signing Commits with GPG, and Overwriting All Past Commits
Make GitHub commit email address private
On GitHub, if you do not want to make your commit email address public, you can use a private email address.
Change GitHub settings
- Access GitHub's email settings page
- Check "Keep my email addresses private"
- Check "Block command line pushes that expose my email" (this blocks pushes that use an email other than your private one)
This allows you to obtain a private email address.
The format is <ID>+<username>@users.noreply.github.com.
You can confirm this address on the settings page.
We’ll remove your public profile email and use xxx+xxx@users.noreply.github.com when performing web-based Git operations ...
You can find the address in that part.
Change Git settings
Configure Git to use the private email address.
# To change as a global setting
git config --global user.email "<ID>+<username>@users.noreply.github.com"
# To change for a specific repository only
git config user.email "<ID>+<username>@users.noreply.github.com"
Verify that the setting is applied correctly.
git config user.email
Sign commits with a GPG key
By signing commits with a GPG key, you can prove that the commit is from you. GitHub displays a "Verified" badge on signed commits.
Generate a GPG key
If you don't have a GPG key yet, generate a new one.
# Generate a GPG key
gpg --full-generate-key
Install gpg as appropriate.
I am a Nix user, so I installed gnupg from nixpkgs.
Select/enter the following interactively:
- Key type:
ECC (sign and encrypt)(default) - Elliptic curve:
Curve 25519(default) - Expiration: optional (default is no expiration)
- Real name: GitHub username
- Email address: the GitHub private email address obtained earlier
If all defaults are fine, just keep pressing Enter. I did that.
Set a passphrase
You will be prompted to enter a passphrase when generating the GPG key.
The passphrase is an important element for protecting the GPG private key. Setting a passphrase prevents unauthorized use by third parties even if the private key is leaked.
When setting a passphrase, consider the following points:
- Use a passphrase with sufficient length and complexity
- Avoid easily guessable words or personal information
- Use a password manager to manage it securely
- Store it reliably because forgetting the passphrase will make the private key unusable
You will be asked for the passphrase when signing commits. To reduce the hassle, you can cache the passphrase using gpg-agent.
Check the GPG key ID
Check the ID of the generated key.
gpg --list-secret-keys --keyid-format=long
Example output:
/Users/username/.gnupg/secring.gpg
----------------------------------
sec 4096R/3AA5C34371567BD2 2024-01-01 [expires: 2025-01-01]
uid Your Name <ID+username@users.noreply.github.com>
ssb 4096R/42B317FD4BA89E7A 2024-01-01
The 3AA5C34371567BD2 on the sec line is the key ID.
Register the GPG key on GitHub
Export the public key.
gpg --armor --export <key ID>
# On macOS, you can copy it directly to the clipboard with:
gpg --armor --export <key ID> | pbcopy
Copy the output public key and register it on GitHub's GPG keys settings page.
The public key is the part from -----BEGIN PGP PUBLIC KEY BLOCK----- to -----END PGP PUBLIC KEY BLOCK-----.
Change Git settings
Enable signing in Git.
# Specify the GPG key to use
git config --global user.signingkey <key ID>
# Sign all commits by default
git config --global commit.gpgsign true
Rewrite all past commits
This section explains how to change the email address used in existing commits to a private email address and add GPG signatures.
Using filter-branch
You can use git filter-branch to change author information for all commits.
This operation is repository-specific, so run it in each repository.
git filter-branch -f --commit-filter '
GIT_AUTHOR_EMAIL="<ID+username@users.noreply.github.com>";
GIT_COMMITTER_EMAIL="<ID+username@users.noreply.github.com>";
git commit-tree -S "$@";
' HEAD
Push to remote
Push the changes to the remote repository.
# Verify the changes
git log --show-signature
# Force push (caution!)
git push --force-with-lease origin main
Using --force-with-lease will abort the push if there are unexpected changes on the remote.
Discussion