Hide your email address on GitHub

 
 
  • Gérald Barré

When you create a Git commit, your username and email are stored with it. Anyone who clones the repository can view the commit history and see those details. This means your private email address can be harvested and used to send you spam. For example, here is the output of git log on the corefx repo, showing some email addresses:

#Configure git

GitHub provides a noreply address for every user. For instance, my email is meziantou@users.noreply.github.com. You can find it on the settings page: https://github.com/settings/emails.

Then, you can change your git configuration:

Shell
git config --global user.email meziantou@users.noreply.github.com
git config --global user.name meziantou

If you want to configure only a single repo,

Shell
cd "path to the git repository"
git config user.email meziantou@users.noreply.github.com
git config user.name meziantou

#Rewrite history

You may also want to replace your email in existing commits. Git provides a way to rewrite repository history. If you are on Windows, use bash instead of cmd, as escape characters differ between the two. If you have not yet configured bash, read this documentation.

Shell
git filter-branch --commit-filter '
    if [ "$GIT_COMMITTER_EMAIL" = "<Your old email address>" ];
    then
        GIT_COMMITTER_NAME="<Your name>";
        GIT_AUTHOR_NAME="<Your name>";
        GIT_COMMITTER_EMAIL="<Your noreply email address>";
        GIT_AUTHOR_EMAIL="<Your noreply email address>";
        git commit-tree "$@";
    else
         git commit-tree "$@";
    fi' HEAD

Next, push your changes to the remote repository. You must use --force to overwrite the remote history.

Shell
git push --force

#Change GitHub settings to block commits containing your email address

Finally, you can configure GitHub to block commits that contain your actual email address. Go into the settings/email section and check the box Block command line pushes that expose my email

Do you have a question or a suggestion about this post? Contact me!

Follow me:
Enjoy this blog?