Hide your email address on GitHub

  • Gérald Barré

When you create a commit on git, the username and email is associated with the commit. So when someone clones the repository, they can see the list of commits and the associated user. Thus, you can get a list of email addresses. People often use their private email address, so you can be spam. For instance, here's the output of git log on the repo corefx. You can see 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:

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,

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

#Rewrite history

Then, you want to replace your email in the previous commits. git provides functionality to rewrite the history of the repository. If you are on Windows, I advise you to use bash as the escape characters are not the same on both systems. If you have not yet configured bash, read this documentation.

git filter-branch --commit-filter '
    if [ "$GIT_COMMITTER_EMAIL" = "<Your old email address>" ];
        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 "$@";
         git commit-tree "$@";
    fi' HEAD

Then, you have to push your changes to the remote repository. You must use --force to overwrite the remote repository.

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?Buy Me A Coffee💖 Sponsor on GitHub